2012-03-10 50 views
3

我更新了Hibernate到4.1.1.Final版本。據the documentation 有2種方式來生成一個數據庫模式:如何使用org.hibernate.tool.EnversSchemaGenerator生成Envers數據庫模式?

  1. Ant任務org.hibernate.tool.ant.EnversHibernateToolTask
  2. 從Java運行org.hibernate.tool.EnversSchemaGenerator

Hibernate-tools不支持Hibernate-4.1.1.Final。它有一個blocking bug

我只找到了release notes和一個test case。 那麼我怎樣才能用我的persistence.xml和Maven來使用org.hibernate.tool.EnversSchemaGenerator

更新:

找到相關thread on the Hibernate forum。看來我的問題還沒有答案。

回答

8

Juplo創建了Maven plugin for Hibernate 4。該插件支持模式導出,包括Envers。下面的工作示例。檢查official plugin configuration documentation以獲取有關使用選項的解釋。

該插件生成schema.sql文件,在目標的Maven /target目錄中。 或者您可以手動運行hibernate4:export目標來更新文件。

<project> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>de.juplo</groupId> 
       <artifactId>hibernate4-maven-plugin</artifactId> 
       <version>1.0.3</version> 
       <executions> 
        <execution> 
         <goals> 
          <goal>export</goal> 
         </goals> 
        </execution> 
       </executions> 
       <configuration> 
        <envers>true</envers> 
        <format>true</format> 
        <delimiter>;</delimiter> 
        <force>true</force> 
        <type>CREATE</type> 
        <target>SCRIPT</target> 
        <hibernateDialect>org.hibernate.dialect.PostgreSQL9Dialect</hibernateDialect> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 
+0

非常感謝,它非常簡單。 – 2016-04-06 04:10:01

0

我有同樣的問題。現在有一個Hibernate 4 Tools版本:

<dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-tools</artifactId> 
     <version>4.0.0-CR1</version> 
    </dependency> 

但這種螞蟻片段不導出審覈表中,只有「基本」表:

<target name="schema-export"> 
    <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.EnversHibernateToolTask" classpathref="classpath"/> 
    <hibernatetool destdir="sql"> 
     <classpath refid="classpath"/> 
     <jpaconfiguration persistenceunit="persistenceUnit"/> 
     <hbm2ddl export="false" create="true" drop="true" format="true" outputfilename="schema.sql"/> 
    </hibernatetool> 
</target> 

同樣使用此代碼:只有「基本」 ,沒有「_aud」表格:

public static void main(String[] args) { 
    Ejb3Configuration jpaConfiguration = new Ejb3Configuration().configure("persistenceUnit", null); 
    Configuration hibernateConfiguration = jpaConfiguration.getHibernateConfiguration(); 
    AuditConfiguration.getFor(hibernateConfiguration); 
    EnversSchemaGenerator esg = new EnversSchemaGenerator(hibernateConfiguration); 
    org.hibernate.tool.hbm2ddl.SchemaExport se = esg.export(); 
    se.setOutputFile("sql/schema.sql"); 
    se.setFormat(true); 
    se.setDelimiter(";"); 
    se.drop(true, false); 
    se.create(true, false); 
} 

你還有興趣嗎?如果我找到如何解決問題,我會告訴你。也許其他人對我們有任何建議?

+0

查看我對這個問題的回答。我找到了完成任務最方便的方法。 – 2014-02-16 05:40:20

2

以下爲我工作:

public static void main(String[] args) { 
    Ejb3Configuration jpaConfiguration = new Ejb3Configuration().configure("persistenceUnit", null); 
    jpaConfiguration.buildMappings(); 
    Configuration hibernateConfiguration = jpaConfiguration.getHibernateConfiguration(); 
    AuditConfiguration.getFor(hibernateConfiguration); 
    EnversSchemaGenerator esg = new EnversSchemaGenerator(hibernateConfiguration); 
    org.hibernate.tool.hbm2ddl.SchemaExport se = esg.export(); 
    se.setOutputFile("sql/schema.sql"); 
    se.setFormat(true); 
    se.setDelimiter(";"); 
    se.drop(true, false); 
    se.create(true, false); 
} 
4

你不需要Ant或休眠工具。這是很容易只是直接使用EnversSchemaGenerator,就像這樣:

Configuration config = new Configuration(); 

//make sure you set the dialect correctly for your database (oracle for example below) 
config.setProperty("hibernate.dialect","org.hibernate.dialect.Oracle10gDialect"); 

//add all of your entities 
config.addAnnotatedClass(MyAnnotatedEntity.class); 

SchemaExport export = new EnversSchemaGeneHator(config).export(); 
export.execute(true, false, false, false); 

你也可以給它一個文件名來寫,但上面的代碼將打印到系統日誌反正。