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();
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> <java classname="com.orange.tools.jpa.JpaSchemaExport" fork="true" failonerror="true"> <arg value="${persistenceUnitName}" /> <arg value="target/jpa/sql/schema-create.sql" /> <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! :)
Donatello
source share