목차

JPA Schema Generation

Gradle

JPA 2.1

Java 코드를 통한 DDL 생성

import java.util.HashMap;
 
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
 
public class JPASchemaExport {
	public static void main(String[] args) {
		String persistenceUnitName = args[0];
		String outputFile = args[1];
 
		Ejb3Configuration cfg = new Ejb3Configuration();
		HashMap<String, String> props = new HashMap<String, String>();
		Ejb3Configuration configured = cfg.configure(persistenceUnitName, props);
		SchemaExport se = new SchemaExport(configured.getHibernateConfiguration());
		se.setOutputFile(outputFile);
		se.setFormat(true|false);
		se.setDelimiter(";");
		se.create(true, false);
	}
}

Maven Hibernate3 플러그인을 통한 생성

<plugin>
  <!-- run "mvn hibernate3:hbm2ddl" to generate a schema -->
  <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>
      <persistenceunit>[[PERSISTENCE_UNIT_NAME]]</persistenceunit>
      <outputfilename>schema.ddl</outputfilename>
      <drop>true</drop>
      <create>true</create>
      <export>false</export>
      <format>true</format>
    </componentProperties>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>xerces</groupId>
      <artifactId>xercesImpl</artifactId>
      <version>2.9.1</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>3.6.0.Final</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>3.6.0.Final</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>4.1.0.Final</version>
    </dependency>
  </dependencies>
</plugin>

읽어보기