有没有办法让Hibernate的hbm2ddl Ant任务排除特定的表?

时间:2010-01-26 16:17:33

标签: java hibernate ant annotations hbm2ddl

我使用Hibernate自动生成数据库进行测试,我的架构中有一些包含静态数据的表需要很长时间才能导入。过去,我在构建文件中使用了以下代码来生成数据库(来自映射文件):

<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 Annotations进行映射,所以我转向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一样。任何人都知道有没有办法这样做?

4 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

我最后解决的问题是创建一个单独的Hibernate配置,其中包含我想要映射的类,并将其用于导出任务,而不是使用所有映射类的其他Hibernate配置。

答案 2 :(得分:0)

您是否尝试过 hbmddl 标记上的更新属性?

<hbm2ddl update="true" ...

请参阅here了解详情

应该有效

答案 3 :(得分:0)

如果您遇到这种情况并且您不希望Hibernate更新表中的数据,那么您可以替换:

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

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

然后架构导出工具将忽略它,但任何写入也是如此。至少,这是文档建议的内容。

  

subselect(可选):将不可变和只读实体映射到   数据库子选择。

我通过查看Table.isPhysicalTable函数发现了这一点。您还可以考虑使用AbstractUnionTables,这是另一个例外。

我碰巧想要不可变对象。

我的用例是我想加载一些hibernate托管对象的略有不同形状的不可变版本,而不会有意外更改架构导出的风险。所以subselect非常适合。

不幸的是,它将使用该子选择丢弃您的所有查询,数据库应该能够优化它,但人们对数据库优化有不同程度的信任并且有充分的理由。

相关问题