如何使用 Java JPA 加载初始数据(或种子数据)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9827380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:13:48  来源:igfitidea点击:

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

javajakarta-eejpamaven

提问by Filipe Giusti

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.

我有一个 JPA 项目,我想在开发时插入一些初始数据,这样我就可以检查一切是否顺利运行。

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?

我的研究使我只能找到使用直接 SQL 脚本的解决方案,但这是不对的。如果我使用框架来抽象数据库详细信息,为什么要为特定数据库创建脚本?

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?

在 ruby​​ on rails 世界中,我们有命令“rake db:seed”,它简单地执行一个名为 seed.rb 的文件,该文件具有在调用抽象层的数据库上添加初始数据的功能。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?

我能想到的理想解决方案是执行一个 maven 目标来执行一个 java 类,有没有简单的方法或 maven 插件来做到这一点?

采纳答案by mguymon

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

我感受到了你的痛苦,我想要一个 Java 项目以获得 Rails 的所有好处。

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.

话虽如此,没有理由直接使用 SQL。这种方法只是自找麻烦。随着您的数据库架构在开发过程中发生变化,所有脆弱的 SQL 都会中断。如果将数据映射到 JPA 模型,将更容易管理数据,这将抽象与数据库的 SQL 交互。

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 YAMLto serialize our Models as Yaml. To seed our database we deserialize the yaml to JPA models and persist.

你应该做的是使用你的 JPA 模型来播种你的数据。创建一个组件,该组件可以执行您需要的模型的创建并将它们持久化。在我当前的项目中,我们使用Snake YAML将我们的模型序列化为 Yaml。为了给我们的数据库做种子,我们将 yaml 反序列化为 JPA 模型并持久化。

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.

如果模型更改(变量类型更改、删除列等),您必须确保序列化数据仍然能够正确地反序列化为 JPA 模型。使用人类可读的 Yaml 格式可以轻松更新序列化模型。

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.mainto run the seed.

要实际运行您的种子数据,请尽可能引导您的系统。正如@GeoorgeMcDowd 所说,您可以使用 Servlet。我个人更喜欢通过使用Class.main. 然后你只需要创建一个脚本来设置你的类路径并调用Class.main来运行种子。

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:

就我个人而言,我喜欢 Maven 作为项目元数据,但发现它很难作为构建工具。以下可用于执行 java 类:

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

回答by FAtBalloon

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 init 中创建的方法。您可以使用以下 web.xml 配置加载您的 servlet。

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

编辑:将 web.xml 格式设置为对读者更友好。

回答by amuniz

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

您可以使用 Maven 为您的项目建模并编写一个简单的测试来初始化种子数据,因此您唯一需要做的就是运行“mvn test”。

回答by nansen

Similar to amuniz's idea: have a look at dbunit. It is a JUnit extension for pupulating test data into a db. It uses a simple schema-less xml format for that. And running it through a test class using "mvn test" is a simple thing to do.

类似于 amuniz 的想法:看看dbunit。它是一个 JUnit 扩展,用于将测试数据放入数据库中。为此,它使用了一种简单的无模式 xml 格式。使用“mvn test”通过测试类运行它是一件简单的事情。

回答by user5739283

I would suggest liquibase http://www.liquibase.org/for this . It has many plugin and allows you to define the rollback logic for every change set (and detect the rollback in some cases).

为此,我建议使用 liquibase http://www.liquibase.org/。它有许多插件,并允许您为每个更改集定义回滚逻辑(并在某些情况下检测回滚)。

In this case it is important also to think about the production servers and how the seed data will be moved to production.

在这种情况下,考虑生产服务器以及如何将种子数据移动到生产环境也很重要。