Enum class not found when using EnumType in Hibernate hbm file

1.2k views Asked by At

I'm using hbm files to generate my POJO objects using hbm2java through an Ant task. I'm trying to change some hard coded values to Enum using org.hibernate.type.EnumType in my XML:

<set name="myCollection" table="table_name" lazy="true">
    <key column="ref_id"/>
    <element column="col" not-null="true">
        <type name="org.hibernate.type.EnumType">
            <param name="enumClass">my.path.MyEnum</param>
            <param name="type">12</param>
            <param name="useNamed">true</param>
        </type>
    </element>
</set>

My first attempt at running hbm2java resulted in 'Enum class not found' for MyEnum. I realized that I needed to add my classes to the classpath in my ant file:

<hibernatetool destdir="${src.dir}">
    <classpath>
        <path location="${build.dir}"/>
    </classpath>
    <configuration configurationfile="${basedir}/sql/hibernate.cfg.xml" >
        <fileset dir="${src.dir}" id="id">
            <include name="model/*.hbm.xml" />
        </fileset>
    </configuration>

    <hbm2java ejb3="false" jdk5="true" />
</hibernatetool>

Everything worked this time, but it turns out it is only because I had already compiled everything in ${src.dir} to ${build.dir}. If I start from a "clean" state, I get the 'Enum class not found' again because it has a circular dependency: In order to compile the code, I need the POJO's. But in order to get the POJO's, I need the compiled code.

The only solution I can think of is to Compile everything in the enum package first, then run hbm2java, then compile the rest.

It seems strange to me, but is this the best solution? Or is there some other solution that I haven't thought of? For example, is there any way to have it look at my source code instead?

1

There are 1 answers

0
Henry On BEST ANSWER

I ended using the solution that I proposed by adding an ant task that only compiles the classes needed to run hbm2java. The task is named "build-hibernate-dependencies", so I simply add a depends attribute to my hbm2java target for it:

<target name="hbm2java" depends="build-hibernate-dependencies">
    <hibernatetool destdir="${src.dir}">

    ...

    </hibernatetool>
</target>

The target "build-hibernate-dependencies" compiles the enums to the build directory:

<target name="build-hibernate-dependencies">
    <mkdir dir="${build.dir}" />
    <javac destdir="${build.dir}">
        <src path="${src.dir}/enums" />
    </javac>
</target>

And after that, I can now compile the entire project.