Java 有没有办法让Hibernate的hbm2ddl Ant任务排除特定的表?
我使用Hibernate自动生成数据库进行测试,我的模式中有一些表包含静态数据,导入这些数据需要很长时间。过去,我在构建文件中使用以下代码从映射文件生成数据库:Java 有没有办法让Hibernate的hbm2ddl Ant任务排除特定的表?,java,hibernate,ant,annotations,hbm2ddl,Java,Hibernate,Ant,Annotations,Hbm2ddl,我使用Hibernate自动生成数据库进行测试,我的模式中有一些表包含静态数据,导入这些数据需要很长时间。过去,我在构建文件中使用以下代码从映射文件生成数据库: <target name="schema-gen" depends="hibernate-gen"> <taskdef name="schemaexport" classname="org.hibernate.tool.hbm2ddl.SchemaExportTask" classpathref="projec
<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文件是使用XDoclet生成的。我将迁移到使用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时一样。有人知道是否有办法做到这一点吗?您是否尝试过hbmddl标记上的更新属性?
有关详细信息,请参阅
这应该行得通这应该行得通:
<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>
我最终采用的解决方案是创建一个单独的Hibernate配置,其中包含我想要映射的类,并将其用于导出任务,而不是使用另一个Hibernate配置来包含所有映射的类。如果您遇到这种情况,并且碰巧也不希望Hibernate更新表中的数据,然后您可以替换:
<class name="FooClass" table="FOO_TABLE"></class>
与
然后模式导出工具将忽略它,但任何写入操作也将忽略它。至少,这是文件中的建议
子选择可选:将不可变只读实体映射到
数据库子选择
我通过查看Table.isPhysicalTable函数发现了这一点。您还可以研究使用AbstractUnionTables,这是另一个例外
我碰巧想要不可变的对象
我的用例是,我希望加载一些hibernate托管对象的形状稍有不同的不可变版本,而不会有意外更改模式导出的风险。所以subselect非常适合这种情况
不幸的是,它会将您的所有查询与子选择一起丢弃,数据库应该能够对其进行优化,但人们对数据库优化的信任程度各不相同,这是有充分理由的。这不是我想要的。我想重新创建大多数表以重置测试的内容,但我想跳过一些表。更新将保留所有存在的内容,这不是我想要的行为。这是一个好主意,但它不起作用,因为我无法使文件集正常工作。让它完全包含我想要的文件,而不包含我不想要的文件,这实在是太多的工作了。显然,我在第一次测试时欺骗了自己。最初,我使用subselect=select*fromtable_name进行测试,并检查它是否更新了架构,是否有效,然后我必须将其优化为subselect=table_name,并只检查它是否有效。由于再次测试,仅使用subselect=table_name仍然会更新模式。我将更新答案,使此注释使用select from语法,尽管它更难看:我探索了另一个选项,设置abstract=true。这似乎对我的情况很好,我也有mutable=false。在更新这个答案或添加另一个答案之前,我会更仔细地研究这个问题,因为它确实不同。
<class name="FooClass" table="FOO_TABLE"></class>
<class name="Foo" subselect="select * from FOO_TABLE">
<synchronize table="FOO_TABLE">
</class>