사용자 도구

사이트 도구


java:jpa:schema_generation

문서의 이전 판입니다!


JPA Schema Generation

  • Hibernate를 Provider로 사용할 경우의 예에다. 프로그램 코드와 Maven을 통한 생성이 가능하다.
  • $CLASSPATH/META-INF/persistence.xml 파일이 존재한다고 가정한다.
  • persistence.xml에 hibernate.dialect 가 잘 정의돼 있어야 한다.

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) {
		Ejb3Configuration cfg = new Ejb3Configuration();
		HashMap<String, String> props = new HashMap<String, String>();
		props.put("hibernate.format_sql", "true");
		Ejb3Configuration configured = cfg.configure("퍼시스턴스유닛이름", props);
		SchemaExport se = new SchemaExport(configured.getHibernateConfiguration());
		se.setDelimiter(";");
		se.create(true, false);
	}
}

Maven Hibernate3 플러그인을 통한 생성

  • 2012년 1월 현재 Maven Hibernate3 2.2 플러그인은 의존성(dependency) 문제가 많다. 그래서 플러그인에 대한 의존성을 따로 명시해 주어야만 한다. 안그러면 다음과 같은 오류를 만나게 되며, core의 의존성을 해결하더라도 xerces, validator등의 의존성까지도 명시하지 않으면 계속해서 다른 오류가 발생한다.
    Caused by: org.apache.maven.plugin.PluginExecutionException: Execution default of goal org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2ddl failed: sun.reflect.annotation.EnumConstantNotPresentExceptionProxy
<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>archetype</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>
java/jpa/schema_generation.1326604447.txt.gz · 마지막으로 수정됨: 2012/01/15 14:14 저자 kwon37xi