我使用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注释进行映射,因此我要转到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一样。有人知道是否有办法这样做?

有帮助吗?

解决方案 2

我处理的解决方案是使用我想映射的类创建单独的冬眠配置,并将其用于导出任务,而不是所有映射的类别的其他Hibernate配置。

其他提示

这应该有效:

<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>

你尝试过吗 更新 属性 HBMDDL 标签?

<hbm2ddl update="true" ...

这里 有关细节

那应该起作用

如果您有这种情况,并且您碰巧不希望Hibernate更新 数据 在表中,您也可以替换:

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

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

然后,模式导出工具将忽略它,但是任何写作都会忽略它。至少,这就是文档所暗示的。

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

我通过查看 Table.isPhysicalTable 功能。您还可以考虑使用AmpactunionTables,这是另一个例外。

我碰巧想要不变的对象。

我的用例是,我想在某些冬眠管理的对象的形状不变的版本中加载略有不同的版本,而不会意外更改模式导出的风险。因此,SubSelect非常适合这一点。

不幸的是,它将用该select(数据库)乱扔所有查询 应该 能够将其优化,但是人们对数据库优化有不同的信任,这有充分的理由。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top