2011-02-23 2 views
1

Hibernate가 자동으로 테이블을 업데이트 할 때 SQL 업데이트 스크립트를 얻는 방법을 찾고 있습니다.Hibernate 업데이트에서 SQL 스크립트 얻기

개발 환경에서만 hibernate.hbm2ddl.auto=update을 사용하고 있으며 프로덕션 용 테이블을 업데이트하는 SQL 스크립트가 필요합니다.

이러한 SQL 스크립트를 개정 및 잠재적 편집을 위해 txt 형식으로 원합니다.

어떻게이 작업을 수행 할 수 있습니까? 조언 해 주셔서 감사합니다.

답변

2

몇 가지 제안과 일반적인 토론이 있습니다. here.

간단히 말해서

, 당신은 (표준 출력) 로깅을 설정 할 수 있습니다

hibernate.show_sql=true 

을 양자 택일로, 당신은 log4j를 사용하는 경우, 당신은 당신의 log4j.properties 파일에이를 추가 할 수 있습니다

log4j.logger.org.hibernate.SQL=DEBUG 

두 접근법 모두 Hibernate의 준비된 구문을 매개 변수와 함께 출력 할 것입니다 (따라서 매개 변수 값 자체는 인라인이 아닙니다). 이 문제를 해결하려면 P6Spy과 같은 인터셉터를 사용할 수 있습니다. 그 세부 사항은 here입니다. reqiured 업데이트 스크립트를 생성 무엇

public java.lang.String[] generateSchemaUpdateScript(Dialect, DatabaseMetadata) 


:

+0

감사 (나는 우리가 외부 도메인 모델을 가지고 있기 때문에 서비스로입니다 implent했다), 내가 알고하지 않았다 Hibeernate 로그 그 이것도. –

0

org.hibernate.cfg.Configuration 클래스는 메소드가 있습니다.

난 그냥 Grails의이 구현했습니다

configuration = new DefaultGrailsDomainConfiguration(
        grailsApplication: grailsApplication, 
        properties: props) 
//this extends hibernate config 

Connection c = SessionFactoryUtils.getDataSource(sessionFactory).getConnection(props.'hibernate.connection.username', props.'hibernate.connection.password') 
<br/>md = new DatabaseMetadata(c, DialectFactory.buildDialect(props.'hibernate.dialect')) 

configuration.generateSchemaUpdateScript(DialectFactory.buildDialect(props.'hibernate.dialect'), md) 
) 
check SchemaExport script in grails, for further information, it uses hibernate to generate schema. 


관련 문제