gpt4 book ai didi

hibernate - 如何使用Hibernate将初始数据导入数据库?

转载 作者:行者123 更新时间:2023-12-03 05:34:34 27 4
gpt4 key购买 nike

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

但是,有时我还需要向数据库插入一些初始数据,例如 root 用户。有没有办法通过 hibernate 和某种加载文本文件来实现此目的?

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

最佳答案

我通过搜索“Hibernate Fixes”找到了这个:

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

尝试一下,让我们知道它是否有效!

关于hibernate - 如何使用Hibernate将初始数据导入数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/673802/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com