2009-06-30 3 views
6

데이터베이스에 연결하는 데 사용할 수있는 최대 절전 모드 utitliy가 있습니다. 모든 테이블을 나열하고 "테이블 + 데이터 생성"을위한 sql 스크립트를 생성하고 * .sql 파일로 저장 하시겠습니까? SQL 등의 데이터베이스에서SQL 스크립트를 생성

답변

7

데이터 내보내기

사용 liquibase 오픈 소스 프로젝트

LiquiBase는 오픈 소스 (LGPL), 추적, 관리 및 데이터베이스 변경을 적용하기위한 데이터베이스 독립적 인 라이브러리를. 모든 데이터베이스 변경 (구조 및 데이터)은 XML 기반 서술 방식으로 저장되고 소스 제어에 체크인됩니다. 그냥 모든 엔티티 클래스와이 클래스를 구성하고/dropTableScript 만들 전화 :

만들고 주어진 JPA 엔티티

우리는 드롭을 생성하고 문을 만들려면이 코드를 사용하기위한 스크립트를 드롭 생성합니다.

필요하다면 persitence.xml과 persitance unit name을 사용할 수 있습니다. 그냥 라고 말하면 코드도 게시합니다.

 
import java.util.Collection; 
import java.util.Properties; 

import org.hibernate.cfg.AnnotationConfiguration; 
import org.hibernate.dialect.Dialect; 
import org.hibernate.ejb.Ejb3Configuration; 

/** 
* SQL Creator for Tables according to JPA/Hibernate annotations. 
* 
* Use: 
* 
* {@link #createTablesScript()} To create the table creationg script 
* 
* {@link #dropTablesScript()} to create the table destruction script 
* 
*/ 
public class SqlTableCreator { 

    private final AnnotationConfiguration hibernateConfiguration; 
    private final Properties dialectProps; 

    public SqlTableCreator(final Collection> entities) { 

     final Ejb3Configuration ejb3Configuration = new Ejb3Configuration(); 
     for (final Class entity : entities) { 
      ejb3Configuration.addAnnotatedClass(entity); 
     } 

     dialectProps = new Properties(); 
     dialectProps.put("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect"); 

     hibernateConfiguration = ejb3Configuration.getHibernateConfiguration(); 
    } 

    /** 
    * Create the SQL script to create all tables. 
    * 
    * @return A {@link String} representing the SQL script. 
    */ 
    public String createTablesScript() { 
     final StringBuilder script = new StringBuilder(); 

     final String[] creationScript = hibernateConfiguration.generateSchemaCreationScript(Dialect 
       .getDialect(dialectProps)); 
     for (final String string : creationScript) { 
      script.append(string).append(";\n"); 
     } 
     script.append("\ngo\n\n"); 

     return script.toString(); 
    } 

    /** 
    * Create the SQL script to drop all tables. 
    * 
    * @return A {@link String} representing the SQL script. 
    */ 
    public String dropTablesScript() { 
     final StringBuilder script = new StringBuilder(); 

     final String[] creationScript = hibernateConfiguration.generateDropSchemaScript(Dialect 
       .getDialect(dialectProps)); 
     for (final String string : creationScript) { 
      script.append(string).append(";\n"); 
     } 
     script.append("\ngo\n\n"); 

     return script.toString(); 
    } 
} 

+2

이 경우 SQL 스크립트로 변환하려면 도메인 객체가 있어야합니다. 내가 원하는 건. 그냥 데이터베이스 서버에 연결하고 모든 테이블을 나열하고 테이블 생성 스크립트를 생성합니다. 가능한? – cometta

+0

오른쪽 엔티티가 필요합니다 (perstiance.xml 또는리스트에서 > 그렇지 않으면 주어진 JDBC 연결에서 ddl 스키마를 포함하는 데이터를 내보내는 liquibase를 사용할 수 있습니다. 더 많은 생각을 할 수 있습니다 (스키마 diffing, 패치 ...). 다른 방법은 우리의 absoulte 좋아하는 직장에서 스위스 군용 칼 DBVisualizer를 사용하는 것입니다 (개인용 버전에서 무료!, java로 작성된 JDBC로 작성) – H2000

+0

안녕하세요, dbvisualizer를 사용하려고했는데 볼 수 있습니다. -> 테이블,보기 "내 데이터베이스에 대한 테이블 및 데이터를 SQL 스크립트로 내보내는 방법 – cometta

관련 문제