2010-01-26 72 views
3

我使用Hibernate自动生成我的数据库进行测试,并且我的模式中有一些表包含需要很长时间导入的静态数据。在过去,我用下面的代码在我的构建文件生成数据库(从映射文件):使用了XDoclet生成有没有办法让Hibernate的hbm2ddl Ant任务排除特定的表?

<target name="schema-gen" depends="hibernate-gen"> 
    <taskdef name="schemaexport" classname="org.hibernate.tool.hbm2ddl.SchemaExportTask" classpathref="project.classpath" /> 

    <schemaexport properties="resources/hibernate.properties" text="false" quiet="false" delimiter=";" output="schema.sql"> 
     <fileset dir="${build.doclets}"> 
      <include name="**/*.hbm.xml" /> 
      <exclude name="**/inert/*.hbm.xml" /> 
     </fileset> 
    </schemaexport> 
</target> 

的的.hbm.xml文件。我迁移到使用Hibernate注解映射,所以我搬到hibernatetools生成模式:

<target name="annotations-export" depends="hibernate-gen"> 
    <hibernatetool destdir="${basedir}"> 
     <annotationconfiguration configurationfile="${basedir}/resources/hibernate.cfg.xml" propertyfile="${basedir}/resources/hibernate.properties" /> 
     <classpath> 
      <path refid="project.classpath" /> 
     </classpath> 
     <hbm2ddl drop="true" create="true" export="true" outputfilename="schema.sql" delimiter=";" format="true" /> 
    </hibernatetool> 
</target> 

我希望能够告诉先得用hbm2ddl的“惰性”离开了班包,就像我以前用schemaexport一样。任何人都知道是否有办法做到这一点?

回答

1

的解决方案,我结束了同是创造一个单独的Hibernate配置去正是我想要映射的类,并将其用于导出任务而不是其他Hib ernate配置与所有映射类。

0

你试过在hbmddl标签更新属性?

<hbm2ddl update="true" ... 

看到here的细节

应该工作

+0

这不是我要找的。我想重新创建大多数表格(为我的测试重置事物),但我想跳过一些表格。更新会使所有存在的东西都完好无损,这并不是我想要的行为。 – Rafe 2010-01-26 21:54:21

2

这应该工作:

<target name="annotations-export" depends="hibernate-gen"> 
    <hibernatetool destdir="${basedir}"> 
     <annotationconfiguration configurationfile="${basedir}/resources/hibernate.cfg.xml" propertyfile="${basedir}/resources/hibernate.properties"> 
      <fileset dir="${build.doclets}"> 
       <include name="**/*.class" /> 
       <exclude name="**/inert/*.class" /> 
      </fileset> 
     </annotationconfiguration> 
     <classpath> 
      <path refid="project.classpath" /> 
     </classpath> 
     <hbm2ddl drop="true" create="true" export="true" outputfilename="schema.sql" delimiter=";" format="true" /> 
    </hibernatetool> 
</target> 
+0

这是一个好主意,但它不起作用,因为我永远无法让文件集正常工作。把它包括到我想要的文件中,而不包括那些我不想要的文件太多了。 – Rafe 2010-01-28 17:24:05

0

如果你有这样的情况,你碰巧不想休眠更新该数据表中,那么你就可以更换:

<class name="FooClass" table="FOO_TABLE"></class> 

<class name="Foo" subselect="select * from FOO_TABLE"> 
    <synchronize table="FOO_TABLE"> 
</class> 

然后将模式导出工具将忽略它,但写入也是如此。至少,这就是文档所暗示的。

子选择(可选):映射不可变和只读实体映射到一个 数据库的子查询。

我通过查看Table.isPhysicalTable函数发现了这个。你也可以看看使用AbstractUnionTables,这是另一个例外。

我碰巧想要不可变的对象。

我的用例是,我想加载一些hibernate管理对象的稍微不同形状的不可变版本,而没有意外更改模式导出的风险。所以子选择非常适合这一点。

不幸的是,它会抛弃您所有的查询,数据库应该能够优化它,但人们对数据库优化有不同程度的信任有很好的理由。

+0

显然我第一次测试这个时候欺骗了自己。起初我 子查询测试=“SELECT * FROM表格名” 并检查它没有更新的架构,工作,然后我必须有它细化到 子查询=“表格名” ,只检查它的工作。因为再次测试,只使用subselect =“table_name”仍会更新模式。 我会更新答案,使这个评论使用select语法,即使它更丑陋:( – 2015-08-03 02:58:38

+0

我探索了另一个选项,设置abstract =“true”。这似乎工作正常我的情况,我也是有mutable =“false”,在更新这个答案或添加另一个答案之前,我会更仔细地探讨这个问题,因为它真的不同。 – 2015-08-03 04:50:51

相关问题