In a Java8 project which uses JPA (with Hibernate4), it is non-trivial to get the create and update DDL statements. With the following configuration, it is possible to do this using:
mvn hibernate3:hbm2ddl
My project contains this persistence.xml file.
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"> <persistence-unit name="fluxtock"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:/ZzzDS</jta-data-source> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect" /> </properties> </persistence-unit> </persistence> |
Unfortunately, this configuration cannot be used as it does not directly define the connection. Also, this file references a Hibernate4 specific dialect. The maven plugin for Hibernate tools only supports Hibernate3, so is cannot handle this dialect. As solution, I put a hibernate.cfg.xml file in the test resources. This configuration is only used by the maven plugin.
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- DataSource & Connection info. --> <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property> <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/zzz</property> <property name="hibernate.connection.username">zzz</property> <property name="hibernate.connection.password">zzz</property> <property name="hibernate.connection.pool_size">1</property> <!-- All our Hibernate mapping XML files. --> <mapping package="my.package.domain" /> <mapping class="my.package.DomainClass1" /> <mapping class="my.package.DomainClass2" /> </session-factory> </hibernate-configuration> |
You need to define a mapping line for each domain object and possibly also for the package (to assure that the package-info file is read, this can contain type mappings etc). Remember to add and remove domain classes when needed.
Using this configuration, You can include the maven plugin. You have to add a suitable Hibernate version as dependency. In my case (being on Java8) I also needed to switch javassist to a java8 capable version (to complicate things, the groupId had changed).
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>hibernate3-maven-plugin</artifactId> <version>3.0</version> <configuration> <hibernatetool> <hbm2ddl create="true" export="false" outputfilename="create-ddl.sql" format="true" console="true"> <configuration configurationfile="src/test/resources/hibernate.cfg.xml"/> </hbm2ddl> <hbm2ddl update="true" export="false" outputfilename="update-ddl.sql" format="true" console="true"> <configuration configurationfile="src/test/resources/hibernate.cfg.xml"/> </hbm2ddl> </hibernatetool> </configuration> <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>3.6.10.Final</version> <exclusions> <exclusion> <groupId>javassist</groupId> <artifactId>javassist</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.18.1-GA</version> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.3-1100-jdbc41</version> </dependency> </dependencies> </plugin> |
Leave a Reply