views:

227

answers:

1

What is the best way to fill tables create with jpa at application startup/shutdown of application ? like but with data manipulation language instead data definition language. i'm using HibernatePersistence as persistence provider.

+1  A: 

If you're also using spring, then this answer will work.

If you're not using Spring, then you must run the DDL code manually. Note that Hibernate has only limited support to modify the database (it can basically only create tables). So if you need anything else, you must use custom SQL.

[EDIT] If you don't use Spring, then you find a way to get at the Hibernate Session. Invoke the doWork() method which allows to run arbitrary SQL.

A different approach is to create the domain objects and persist them. I prefer the first approach, though, since you can fixate the IDs of generated objects this way and do other things that are more complex using the JPA interface.

It you don't want to write lots of insert statements, then insert the data into the DB and export it with an SQL tool like SquirrelSQL which can create the insert statements for you. Put them into an extra file, read the file at startup, split it at ; and execute each piece.

Aaron Digulla
I believe OP is asking about populating tables with data, not altering DB schema.
ChssPly76
yes, and i'm not using spring for this project :)
davideconsonni
ok, now i'm using an external sql file that i load after the database creation.
davideconsonni