Create DDL script when creating MAVEN with Hibernate4 / JPA 2.1

It seems that the hibernate3-maven-plugin used to create DDL create / delete scripts is no longer compatible 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 is migrated to the new package: org.hibernate.internal.util.ReflectHelper

However, I did not find a clear way to keep the DDL script generation in the MAVEN assembly.

There is no hibernate4-maven-plugin or any other official way to do this.

So what? Isn't that the main function that should be supported? How to do it?

+8
maven ddl hibernate3-maven-plugin
source share
2 answers

Since Hibernate 4.3+ now implements JPA 2.1 , the appropriate way to generate DDL scripts is to use the following set of JPA 2.1 properties:

 <property name="javax.persistence.schema-generation.scripts.action" value="create"/> <property name="javax.persistence.schema-generation.create-source" value="metadata"/> <property name="javax.persistence.schema-generation.scripts.create-target" value="target/jpa/sql/create-schema.sql"/> 

A good summary of the other 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 the official specifications of JPA 2.1 are here: https://jcp.org/aboutJava/communityprocess/final/jsr338/index.html

Since this will be generated at runtime, you might want to do this DDL generation during assembly .

The following is a JPA 2.1 approach to creating this script programmatically:

 import java.io.IOException; import java.util.Properties; import javax.persistence.Persistence; import org.hibernate.jpa.AvailableSettings; public class JpaSchemaExport { public static void main(String[] args) throws IOException { execute(args[0], args[1]); System.exit(0); } public static void execute(String persistenceUnitName, String destination) { System.out.println("Generating DDL create script to : " + destination); final Properties persistenceProperties = new Properties(); // XXX force persistence properties : remove database target persistenceProperties.setProperty(org.hibernate.cfg.AvailableSettings.HBM2DDL_AUTO, ""); persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_DATABASE_ACTION, "none"); // XXX force persistence properties : define create script target from metadata to destination // persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_CREATE_SCHEMAS, "true"); persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_SCRIPTS_ACTION, "create"); persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_CREATE_SOURCE, "metadata"); persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_SCRIPTS_CREATE_TARGET, destination); Persistence.generateSchema(persistenceUnitName, persistenceProperties); } } 

As you can see, it is very simple!

Now you can use this in AntTask or MAVEN, like this (for MAVEN):

 <plugin> <artifactId>maven-antrun-plugin</artifactId> <version>1.7</version> <executions> <execution> <id>generate-ddl-create</id> <phase>process-classes</phase> <goals> <goal>run</goal> </goals> <configuration> <target> <!-- ANT Task definition --> <java classname="com.orange.tools.jpa.JpaSchemaExport" fork="true" failonerror="true"> <arg value="${persistenceUnitName}" /> <arg value="target/jpa/sql/schema-create.sql" /> <!-- reference to the passed-in classpath reference --> <classpath refid="maven.compile.classpath" /> </java> </target> </configuration> </execution> </executions> </plugin> 

Note that the official hibernate-maven-plugin also may or may not perform the trick in some way:

 <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-maven-plugin</artifactId> <version>4.3.1.Final</version> </dependency> 

Enjoy! :)

+20
source share

I may be a little late, but Andrew Thompson has another solution that is not related to persistence.xml or a real database, although it relates to Hibernate.

http://jandrewthompson.blogspot.com/2009/10/how-to-generate-ddl-scripts-from.html

Unfortunately, this is due to the small amount of code, but very flexible.

+1
source share

All Articles