如何使用 Hibernate 将初始数据导入数据库? [英] How to import initial data to database with Hibernate?

查看:22
本文介绍了如何使用 Hibernate 将初始数据导入数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在部署应用程序时,我经常使用 Hibernate 的能力来创建数据库模式以简化部署.这可以通过配置 hibernate.hbm2ddl.auto 属性轻松实现.

When deploying applications, I often use Hibernate’s capacity to create database schema in order to simplify the deployment. This is easily achievable by configuring hibernate.hbm2ddl.auto property.

但是,有时我还需要向数据库插入一些初始数据,例如 root 用户.有没有一种方法可以通过使用某种加载文本文件的休眠来实现这一点?

However, on occasion I also need to insert some initial data to database, for example root user. Is there a way I could achieve this via hibernate with some kind of load textual file?

我知道我可以轻松地编写代码来实现这一点,但只是想知道是否已经有一些实用程序可以帮助我通过配置实现相同的目标?

I know I could easily program the code that will do so, but just wondering if there is already some utility that can help me achieve the same via configuration?

推荐答案

我通过搜索Hibernate fixtures"找到了这个:

I found this by doing a search on "Hibernate fixtures" :

Hibernate 会创建数据库当实体管理器工厂是创建(实际上是当 Hibernate 的SessionFactory 是由实体管理器工厂).如果一个文件名为 import.sql 存在于类路径('/import.sql')Hibernate 将执行 SQL之后从文件中读取的语句数据库模式的创建.重要的是要记住在 Hibernate 创建架构之前清空它(删除所有表,约束,或任何其他数据库将要创建的对象构建模式的过程).

Hibernate will create the database when the entity manager factory is created (actually when Hibernate's SessionFactory is created by the entity manager factory). If a file named import.sql exists in the root of the class path ('/import.sql') Hibernate will execute the SQL statements read from the file after the creation of the database schema. It is important to remember that before Hibernate creates the schema it empties it (delete all tables, constraints, or any other database object that is going to be created in the process of building the schema).

来源:http://www.velocityreviews.com/forums/t667849-hibernate-quotfixturesquot-or-database-population.html

试试看,让我们知道它是否有效!

Give it a try and let us know if it works!

这篇关于如何使用 Hibernate 将初始数据导入数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆