Reputation: 11
I have a Java Project in which I use JPA for persistence. Now I want to integrate LiquiBase in my project, but I don't know how to change the order JPA/Liquibase are executed.
For clarity: Until now my persistence.xml looks like this:
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value=.../>
<property name="javax.persistence.jdbc.user" value=.../>
<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
I now tried to set:
<property name="eclipselink.ddl-generation" value="none"/>
so that liquibase handles all the database-stuff.
But now when I run my Code, JPA throws an Error because some tables are missing, although these tables should be created by liquibase, which leads me to believe that JPA is running before liquibase.
How do I change that order? Until now, Liquibase is executed in code via this lines:
java.sql.Connection connection = openConnection(); //your openConnection logic here
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
Liquibase liquibase = new liquibase.Liquibase("path/to/changelog.xml", new ClassLoaderResourceAccessor(), database);
liquibase.update(new Contexts(), new LabelExpression());
I read somewhere that this could be achieved via Liquibase Servlet listener, but that requires the database data-source, and I dont know how to get that.
Liquibase itself works fine.
Upvotes: 1
Views: 1625
Reputation: 38132
If you want to run Liquibase as part of your application I recommend to use one of the options documented in the "Automated" section:
http://www.liquibase.org/documentation/running.html
If you're developing a Java EE application, first create a data source: https://docs.oracle.com/javaee/7/tutorial/resource-creation002.htm
Note that this is application server specific.
Since Java EE 6 there is now also an application server independent annotation which can be used: http://docs.oracle.com/javaee/7/api/javax/annotation/sql/DataSourceDefinition.html
In your persistence.xml I suggest to use transaction-type="JTA"
and reference the data source using:
<jta-data-source>jdbc/someDB</jta-data-source>
Omit the part:
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value=.../>
<property name="javax.persistence.jdbc.user" value=.../>
Upvotes: 1