문제

I use Hibernate to generate my database automatically for testing, and I have some tables in my schema that contain static data that takes a very long time to import. In the past, I've used the following code in my build file to generate the database (from mapping files):

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

The .hbm.xml files were generated using XDoclet. I'm migrating to using Hibernate Annotations for mapping, so I'm moving to hibernatetools to generate the schema:

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

I'd like to be able to tell hbm2ddl to leave out the classes in the "inert" package, just like I used to with schemaexport. Anyone know if there's a way to do so?

도움이 되었습니까?

해결책 2

The solution I wound up going with was creating a separate Hibernate configuration with exactly the classes I wanted to map, and using that for the export task instead of the other Hibernate configuration with all of the mapped classes.

다른 팁

This should work:

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

have you tried the update attribute on the hbmddl tag?

<hbm2ddl update="true" ...

see here for the details

that should work

If you have this situation and you happen to not want Hibernate to update the data in the tables either, then you can replace:

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

with

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

then the schema export tool will ignore it, but so will any writes. At least, that's what the documentation suggests.

subselect (optional): maps an immutable and read-only entity to a database subselect.

I discovered this through looking at the Table.isPhysicalTable function. You could also look into using AbstractUnionTables, which are the other exception.

I happen to want immutable objects.

My use case is that I want to load slightly differently shaped immutable editions of some hibernate managed objects without a risk of accidentally changing the schema export. So subselect is pretty well suited to that.

Unfortunately it will litter all your queries with that subselect, which the database should be able to optimize it away, but people have varying amounts of trust in database optimisation with good reason.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top