There is a nice Maven plugin for JPA/Hibernate that makes it possible to quickly generate the database schema (SQL) and save it in a file.
The artifactId of this plugin is hibernate3-maven-plugin.
It will scan all JPA annotations in the class files of the entities and generate the corresponding SQL queries.
A persistence.xml file is required.
- With version 2.2 :
- With version 3.0-SNAPSHOT
Content of the pom.xml :
<build> <plugins> ... <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>hibernate3-maven-plugin</artifactId> <version>2.2</version> <configuration> <components> <component> <name>hbm2ddl</name> <implementation>jpaconfiguration</implementation> </component> </components> <componentProperties> <drop>true</drop> <create>true</create> <export>false</export> <format>true</format> <outputfilename>schema-${DataBaseUser}-${DatabaseName}.sql</outputfilename> <persistenceunit>myPU</persistenceunit> <propertyfile>src/main/resources/database.properties</propertyfile> </componentProperties> </configuration> <dependencies> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> <version>10.2.0.2.0</version> </dependency> </dependencies> </plugin> </plugins> </build>
It is important to set the export property to false, so that the plugin does not drop and recreate the database.
Here is the content of the database.properties file:
hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver hibernate.connection.url=jdbc:oracle:thin:@123.45.678.9:1521:myDataBase hibernate.connection.username=user1 hibernate.connection.password=pwd1 hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
Use the following goal :
compile hibernate3:hbm2ddl
The file will be created in the target folder. More exactly in /target/hibernate3/sql
There is a more recent version of the plugin (3.0-SNAPSHOT) but it is a SNAPSHOT version, so it is not recommended to use it in projects for various reasons (for instance it would be a problem to generate releases).
Anyways, here is the configuration that worked for me, for this version of the plugin :
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>hibernate3-maven-plugin</artifactId> <version>3.0-SNAPSHOT</version> <configuration> <components> <component> <name>hbm2ddl</name> <implementation>jpaconfiguration</implementation> </component> </components> <hibernatetool> <classpath> <path location="${project.build.directory}/classes" /> <path location="${project.basedir}/src/main/resources/META-INF/" /> </classpath> <jpaconfiguration persistenceunit="myPU" propertyfile="src/main/resources/database.properties" /> <hbm2ddl create="true" export="false" drop="true" outputfilename="schema1.sql" format="true" console="true"/> </hibernatetool> </configuration> </plugin>
This version does not need the presence of the database driver as a direct dependency of the plugin.
The file will be created in the target folder. More exactly in /target/sql/hibernate3 (not the same folder hierarchy as with the version 2.2 of the plugin).
Links :
http://mojo.codehaus.org/maven-hibernate3/hibernate3-maven-plugin/hbm2ddl-mojo.html
You must be logged in to post a comment.