It seems like the hibernate3-maven-plugin
used to generate DDL create/drop scripts is not compatible any more with Hibernate 4.3
and newer versions (using JPA 2.1
).
I use this plugin configuration :
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>3.0</version>
<executions>
<execution>
<id>generate-sql-schema</id>
<phase>process-sources</phase>
<goals>
<goal>hbm2ddl</goal>
</goals>
<configuration>
<hibernatetool>
<jpaconfiguration persistenceunit="${persistenceUnitName}" />
<hbm2ddl update="true" create="true" export="false"
outputfilename="src/main/sql/schema.sql" format="true"
console="true" />
</hibernatetool>
</configuration>
</execution>
</executions>
</plugin>
But I get the following error :
[ERROR] Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:3.0:hbm2ddl (generate-sql-schema) on project my-project: There was an error creating the AntRun task.
An Ant BuildException has occured: java.lang.NoClassDefFoundError: org/hibernate/util/ReflectHelper: org.hibernate.util.ReflectHelper -> [Help 1]
This class as migrated to a new package : org.hibernate.internal.util.ReflectHelper
However i found no clear way to keep generating DDL create scripts at MAVEN build.
There is no hibernate4-maven-plugin
, or any other official way to do it.
So what ? Isn't it a main feature that should be supported ? How to do it ?
As
Hibernate 4.3+
now implementsJPA 2.1
the appropriate way to generate DDL scripts is to use following set of JPA 2.1 properties :A nice summary of others properties and context of schema generation in JPA 2.1 can be found here : https://blogs.oracle.com/arungupta/entry/jpa_2_1_schema_generation
And official JPA 2.1 specifications here : https://jcp.org/aboutJava/communityprocess/final/jsr338/index.html
As this will be generated at runtime, you may want to execute this DDL generation at build.
Here is the JPA 2.1 approach to generate this script programmatically :
As you can see it's very simple !
Now you can use this in an AntTask, or MAVEN build like this (for MAVEN) :
Note that the official hibernate-maven-plugin also may, or may not, do the trick in some way :
Enjoy ! :)