Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load initial data (or seed data) using Java JPA?

I have a JPA project and I would like to insert some initial data just on development, so I can check if everything is running smoothly easy.

My research lead me to find only solution with direct SQL script, but that isn't right. If I'm using a framework to abstract database details why would I create script for an specific database?

In the ruby on rails world we have the command "rake db:seed" that simple executes a file named seed.rb that has the function to add the initial data on the database calling the abstraction layer. Is there something like that on java?

The ideal solution I can think of would be to execute a maven goal that would execute a java class, is there an easy way or a maven plugin to do it?

like image 490
Filipe Giusti Avatar asked Mar 22 '12 17:03

Filipe Giusti


3 Answers

I feel your pain, I have gone wanting in a Java project for all of the perks Rails has.

That being said, there is no reason to use straight SQL. That approach is just asking for trouble. As your database schema changes during development, all the brittle SQL breaks. It is easier to manage data if it is mapped to JPA Models, which will abstract the SQL interaction with the database.

What you should do is use your JPA models to seed your data. Create a component that can execute the creation of models you require and persist them. In my current project, we use Snake YAML to serialize our Models as Yaml. To seed our database we deserialize the yaml to JPA models and persist.

If the models change (variable types change, remove columns, etc), you have to make sure that the serialize data will still be able to correctly deserialize into the JPA models. Using the human readable format of Yaml makes it easy to update the serialized models.

To actually run your seed data, bootstrap your system however you can. As @GeoorgeMcDowd said, you can use a Servlet. I personally prefer to create a command line tool by creating a uberjar with Class.main. Then you just need to create a script to setup your classpath and call the Class.main to run the seed.

Personally, I love Maven as project meta data but find it difficult as a build tool. The following can be used to exec a java class:

mvn exec:java -Dexec.mainClass="com.package.Main"
like image 113
mguymon Avatar answered Oct 19 '22 23:10

mguymon


Just create a class and method that creates the objects and persists the data. When you fire up your application, run the method that you created in a servlet init.You can load your servlet up with the following web.xml config.

<servlet>
   <servlet-name>MyServlet1</servlet-name>
   <servlet-class>com.example.MyServlet1</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>

Edit: Format web.xml to be more reader friendly.

like image 31
FAtBalloon Avatar answered Oct 20 '22 01:10

FAtBalloon


You could model your project with Maven and write a simple test to initialize the seed data, so the only thing you will need to do is to run "mvn test".

like image 35
amuniz Avatar answered Oct 20 '22 00:10

amuniz