JPA scheme receiver, is there such a thing?

you can specify hibernate or any JPA implementation to create the schema for you, the question is, what if you want to populate the table with the default source records ?.

to solve, when you start the application, you can check if the table is empty and then create records, but I think that if the listener will be more attractive, called after creating the scheme.

+4
source share
2 answers

If you use Hibernate, you can create an import.sql file (in the src / main / resources file) with your source data. When the application starts, the file is selected and executed by Hibernate.

You can also use a specialized tool for a preliminary (and empty) database, for example DBUnit or Arkillian renewal .

As for the original question - AFAIK, you have access only to Entity's life cycle events in JPA. Theoretically, they can be used to check or modify the contents of a table, but this is a bad idea.

EDIT: There is no JPA standard available for sowing a database. Even the Pro JPA 2 book advises seeding with JDBC setup. I think you are better off with the above tools. I am not aware of any seeding help for EclipseLink such as Hibernate import.sql , but there are some DIY tips for a similar workaround . Hope this helps.

+4
source

In EclipseLink, you can use the SessionEventListener to receive an event when the save block starts.

You can set the SessionEventListener using the save unit property, "eclipselink.session-event-listener" You can use the postLogin event to populate your tables or run your own DDL.

See, http://www.eclipse.org/eclipselink/api/2.4/org/eclipse/persistence/sessions/SessionEventAdapter.html

+1
source

Source: https://habr.com/ru/post/1433552/


All Articles