views:

497

answers:

3

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?

A: 

have you tried the update attribute on the hbmddl tag?

<hbm2ddl update="true" ...

see here for the details

that should work

Stefan De Boey
That's not what I'm looking for. I want to recreate most of the tables (to reset things for my tests), but I want to skip some tables. update will leave everything that exists intact, which isn't quite the behavior I want.
Rafe
+1  A: 

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>
mtpettyp
This was a good idea, but it didn't work because I could never get the fileset to work quite right. Getting it to include exactly the files I wanted and not include those I didn't want was just too much work.
Rafe
+1  A: 

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.

Rafe