openxava / documentation / Course with AI - Lesson 2: Database population

×News: OpenXava with AI - Refine the UI (Part 2) - December 1 · Read more

This is the second lesson of a course where we are creating a car insurance policy management application using OpenXava and artificial intelligence.

Video

In this video, we generate test data and insert it into our application's database. Both the data and the code to insert the data are generated by AI, allowing us to complete the work easily in just a few minutes.

Problems with the lesson? Ask in the forum

Code

If you follow the course, you don't need to write any code, the AI will write it for you. And your code doesn't have to be exactly like the one in the video. However, you can find the code generated in this video in a GitHub repository, in case you're interested in examining it.

Transcript

Introduction

Hi, I'm Mónica. In this second lesson, we will use artificial intelligence to populate the database with test data. That way, our application will look better by displaying realistic data. All of this with little effort. Let's go.

Recap of the first lesson

Remember that in the first lesson we wrote a simple sentence asking it to generate the entities for our application. Windsurf generated Vehicle, Policy, Customer and Claim. The application is up and running. Let's remember what it looked like.

Current state of the application

Our application has the four modules ready to use, but without data. We can go to the vehicle list, and it's empty. We can also go to customers. We open the list, and it's also empty. The application feels deserted. Without data, it doesn't seem real. The purpose of this lesson is to fix that. We'll try asking our AI to generate some test data.

Generating data with AI

Once in the IDE, we use the right-hand side, Cascade, to tell the AI that we want to fill our database with some test data. We do it in English, but you can also use your own language without any problem. Also, we're going to be specific, saying that we want a Java class with a main method. We also tell it to use JDBC and to get the SQL statements from a text file.

AI project analysis

Just a few seconds and it's already working for us. The first thing it does is analyze the project to find out which JDBC driver we're using. Then it analyzes our entities to understand the structure of our data. Next, it examines persistence.xml and context.xml to find the database connection URL. This thing knows where to look — it's no dummy.

Code generation

And finally, it starts generating the Java code we asked for — a Java class called DatabasePopulator. There it is. Now it creates an SQL file with the test data. A little patience while it finishes working. The video is in real time, unedited, because we want you to see how agile this really is — no tricks.

Reviewing the generated code

It's done, and as always, it gives us a summary of what it did. We accept it, and let's take a look at the code with our own eyes. We can see it has created a util package, and inside it placed the DatabasePopulator class. At the top, we have the connection strings. Then the main method. Typical JDBC code, catching potential exceptions. It also created helper methods to make the code clearer. Everything seems in place.

Execution and error correction

Let's run it and see what happens. We scroll up looking for the main. There it is. We click on Run, and it fails. Without overthinking it, we copy the error message and complain to Windsurf. It replies that the class expected an argument, but it's no problem. It will update the class so it works without one. What it's doing is hardcoding the path to the SQL file with the data directly into the code. Which works perfectly for us.

Verifying results

Done. We accept the code, and Windsurf gives us a summary of what it did. Let's try the new code and see if we have better luck this time. We click on Run for the main method. And yes, now it says "Database populated successfully," which sounds pretty good. Let's go back to the browser, hit refresh, and see if we have data now.

Exploring the generated data

And yes, there is data. Let's click on the first client, on John. He has believable data, even some policies. There are also vehicles. And claims. We also have several policies. Let's take a look at one of them. As you can see, in just a few minutes and with hardly any effort, we have our application filled with believable and interconnected test data. The app looks way better now.

The secret to getting good results

But to achieve this, there's a little secret we're about to learn. The key to getting a good result quickly and effectively is: to be specific. In our case, we said we wanted a Java class with a main method, that used JDBC and read from a text file. That way we forced the AI to create a solution that we know works well, and is also tailored to our preferences. The more specific we are, the less we'll need to refine later.

Tips for working with AI

On the other hand, if we ask the AI to do the work without giving many details, without telling it how, the AI will still do it—but the result might not be very good, because the AI doesn't know how to do things as well as we do, and it most likely won't match our preferences. The problem with being specific is that we need to have experience with the technology and the domain.

If that's not the case—if we're new to Java and don't know what a class is, what a main method is, what JDBC and SQL are—then it's more difficult. But it's still possible, we can be vague, we can simply say: do something to populate our database. And with patience and persistence, the AI will get there. If we go that route, then what we need to do is learn—so that next time, when we face a similar case, we'll be able to be specific.

Conclusion

I hope you enjoyed the video. Notice that in just a few minutes, without writing any code, we managed to populate our application's database with pretty good test data. All we had to do was write a class asking it to do so, and be specific about how it should be done. It's true that it didn't work the first time, but just by copying and pasting the error into the chat, Windsurf was able to fix it.

Next lesson

I'll see you in the next video, where we'll learn how to use artificial intelligence to model our OpenXava code and achieve a better user interface. See you next time.