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"
source share