Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How To Generate A Schema Creation Script With Hibernate 4, JPA And Maven

DZone's Guide to

How To Generate A Schema Creation Script With Hibernate 4, JPA And Maven

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

The scenario is trivial – you want to generate a database schema creation script while building your application (and then execute the script on the target database). This was relatively easy with Hibernate 3, as there was the hibernate3-maven-plugin, but it is not compatible with Hibernate 4. And for every new project you should start with Hibernate 4, of course. So what to do? It’s relatively simple, but takes some time to research and test. The idea is to use the SchemaExport tool. But it’s a bit tricky because it only supports native Hibernate configuration and not JPA.

First, you create a command-line application that handles the export. Note that Ejb3Configuration is deprecated, but it is deprecated for external use – hibernate is uses it internally frequently. So it is a properly working class:

@SuppressWarnings("deprecation")
public class JpaSchemaExport {

	public static void main(String[] args) throws IOException {
		execute(args[0], args[1], Boolean.parseBoolean(args[2]), Boolean.parseBoolean(args[3]));
	}

	public static void execute(String persistenceUnitName, String destination, boolean create, boolean format) {
		System.out.println("Starting schema export");
		Ejb3Configuration cfg = new Ejb3Configuration().configure(persistenceUnitName, new Properties());
		Configuration hbmcfg = cfg.getHibernateConfiguration();
		SchemaExport schemaExport = new SchemaExport(hbmcfg);
		schemaExport.setOutputFile(destination);
		schemaExport.setFormat(format);
		schemaExport.execute(true, false, false, create);
		System.out.println("Schema exported to " + destination);
	}
}

Note here, that we are not directly deploying the file to the target database. (The 2nd argument to .execute is false.) This is because we don’t have our database connection properties in persistence.xml – they are external. Deploying the schema file is done later in the maven build, but this is beyond the scope of this post.

Then we have to just invoke this class from the maven build. I initially tried creating it as an ant task to run it with the antrun plugin, but it has classpath and classloader problems (doesn’t find the entities and persistence.xml). That’s why I used the exec-maven-plugin, which invokes the application in the same JVM as the build is running:

<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>exec-maven-plugin</artifactId>
	<version>1.1</version>
	<executions>
		<execution>
			<phase>${sql.generation.phase}</phase> <!-- this is process-classes in our case currently -->
			<goals>
				<goal>java</goal>
			</goals>
		</execution>
	</executions>
	<configuration>
		<mainClass>com.yourcompany.util.JpaSchemaExport</mainClass>
		<arguments>
			<argument>core</argument>
			<argument>${project.build.directory}/classes/schema.sql</argument>
			<argument>true</argument>
			<argument>true</argument>
		</arguments>
	</configuration>
</plugin>

Then you can use the sql-maven-plugin to deploy the schema.sql file to the target database (you will need to have the externalized db properties loaded by maven, which is done by the properties-maven-plugin).

 

 

 

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}