如何在运行Maven / Hibernate项目时加载.sql文件? [英] How can I load a .sql file when running a Maven/Hibernate project?
问题描述
我使用hbm2ddl来创建我的表,它工作得很好,但我也希望hibernate每次运行我的项目时都会加载一个.sql文件来填充数据库。
我发现Hibernate应该这样做,如果我的.sql文件在类路径中,但它似乎不工作。
I am using hbm2ddl to create my tables and it works just fine but I also want hibernate to load a .sql file to populate the database each time I run my project. I found that Hibernate should do this if my .sql file is in the classpath but it doesn't seem to work.
我不确定该文件是否在类路径中,而我不知道如何找到它。
目前我的load.sql是在其他来源/ csr / main / resources /
I am not sure if the file is in the classpath and I dont know how to find that out. Currently my load.sql is in Other Sources/csr/main/resources/
我搜索了一下,我可以添加一个文件到类路径中我的项目的属性/库菜单,但似乎Maven项目没有该部分。
I googled and read that I could add a file to te class path in the properties/libraries menu of my project but it seems Maven project doesn't have that section.
我使用Mysql与JDBC和Netbeans 7.1.2。
I am using Mysql with JDBC and Netbeans 7.1.2.
推荐答案
您必须指定要在您的hibernate.xml中加载哪个SQL文件
You have to specify which SQL file you want to load in your hibernate.xml
<hibernate-configuration>
<session-factory>
... some other stuff comes here
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<property name="hibernate.hbm2ddl.import_files">
/import1.sql, /import2.sql
</property>
</session-factory>
<hibernate-configuration>
只有当hbm2ddl.auto = create-drop时才加载脚本。在所有其他选项中忽略导入文件
请注意,此文件中只允许使用单行语句。
The script is only loaded if hbm2ddl.auto = create-drop. The import files are ignored on all other options Note that only single line statements are allowed in this file.
这两个属性在 Hibernate Configuration,第3.4章
这篇关于如何在运行Maven / Hibernate项目时加载.sql文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!