0

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.

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/

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.

I am using Mysql with JDBC and Netbeans 7.1.2.

3
  • 1
    I don't have an answer but I am curious why you would want to rebuild your DB every time you run the app. Commented Sep 27, 2012 at 21:11
  • Sounds more like testing in this case but i'm not sure... Commented Sep 28, 2012 at 7:17
  • @foampile Example: We have a development configuration which does this (because developers tend to corrupt data hehe). Of course the configuration for the live system does not rebuild the database. Commented Oct 22, 2012 at 10:46

2 Answers 2

3

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>

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.

Those two properties are described in the Hibernate Configuration, Chapter 3.4

Sign up to request clarification or add additional context in comments.

1 Comment

Script is also loaded if hbm2ddl.auto=create
1

You can use the sql-maven-plugin to do such things. This can run sql scripts etc. The hbm2ddl creates SQL scripts which can be executed by sql-maven-plugin.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.