3

我使用 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

4 回答 4

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>
于 2010-01-27T14:53:52.510 回答
1

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

于 2010-01-28T17:25:12.160 回答
0

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

<hbm2ddl update="true" ...

详情请看这里

那应该工作

于 2010-01-26T20:25:18.533 回答
0

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

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

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

那么模式导出工具将忽略它,但任何写入都会忽略它。至少,这是文档所建议的。

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

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

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

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

不幸的是,它会用那个子选择乱扔你的所有查询,数据库应该能够优化它,但是人们对数据库优化有不同程度的信任,这是有充分理由的。

于 2015-07-22T01:24:54.190 回答