We help IT Professionals succeed at work.

Feed database with persistance file in JPA

numtech
numtech asked
on
Hello,
when i deploy my JAR with my persistance.xml it remove the database and recreate it :
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>

Open in new window

But i also want to auto-feed the database after re-create it.
For the moment i have sql file that i insert manually in the database after each deploy...
How can i automatize it directly with the deploiment phase?
Can i put sql-insert in my persistance.xml?
Thanks
Comment
Watch Question

Commented:
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.
      
Adding import.sql to the class path works great, hbm2ddl checks if the file exists and executes it. The only additional detail is that every sql command most be on one line, otherwise it will fail to execute.

Author

Commented:
I dont understand what you mean by "classpath".
I used Maven to build my Jar, so maybe i have to set an option in the build phase?
Commented:
If you are using maven, you need to put the "import.sql" file in your [project name]/src/main/resources directory.

Author

Commented:
Nice job!
Its works great, thanks

Author

Commented:
perfect.

Commented:
You are welcome ;)