2012-03-10 51 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); 

你也可以给它一个文件名来写,但上面的代码将打印到系统日志反正。