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

I have a JPA project and I would like to insert some initial data only for development, so I can check if everything is working quietly.

My research led me to search for only a solution with a direct SQL script, but this is wrong. If I use a framework for abstract database data, why should I create a script for a specific database?

In the world of ruby ​​on rails, we have the "rake db: seed" command, which simply executes a file called seed.rb, which has the function of adding raw data to the database that invokes the abstraction layer. Is there something similar in java?

The ideal solution that I can think of is to fulfill the maven goal, which will execute the java class, is there an easy way or maven plugin to do this?

+6
source share
5 answers

I feel your pain, I went to a Java project for all the Rails privileges.

There is no reason to use direct SQL. This approach just requires trouble. As your database schema changes during development, all fragile SQL breaks. It is easier to manage data if it is mapped to JPA models that abstract the interaction of SQL with the database.

What you have to do is use your JPA models to sow your data. Create a component that can create the required models and save them. In my current project, we use Snake YAML to serialize our models as Yaml. To sow our database, we deserialize the yaml models for JPA and save.

If the models change (variable types change, columns are deleted, etc.), you must make sure that the serialization data will still be properly deserialized in JPA models. Using Yaml's user-readable format, you can easily update serialized models.

To actually run your seed data, boot your system, but you can. As @GeoorgeMcDowd said, you can use the servlet. I personally prefer to create a command line tool by creating uberjar with Class.main . Then you just need to create a script to set up your classpath and call Class.main to start the seed.

Personally, I love Maven as project metadata, but I find this a complex build tool. To execute the java class, you can use the following:

 mvn exec:java -Dexec.mainClass="com.package.Main" 
+4
source

Just create a class and method that creates objects and stores data. When you launch the application, run the method that you created in init. Server You can load your servlet with the following web.xml configuration.

 <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.

+2
source

You can simulate your project using Maven and write a simple test to initialize the seed data, so you just need to run the "mvn test".

+1
source

Like the idea of ​​amuniz: see dbunit . This is a JUnit extension for distributing test data in db. For this, it uses a simple XML format without a schema. And running it through a test class using "mvn test" is a simple thing.

0
source

I would suggest Liquibase http://www.liquibase.org/ for this. It has many plugins and allows you to define rollback logic for each set of changes (and in some cases detect rollback).

In this case, it is also important to think about production servers and how the source data will be transferred to production.

-1
source

Source: https://habr.com/ru/post/911436/


All Articles