在Hibernate hbm文件中使用EnumType时找不到枚举类

时间:2013-07-16 20:43:09

标签: hibernate ant hibernate-tools hbm2java

我正在使用hbm文件通过Ant任务使用hbm2java生成我的POJO对象。我正在尝试使用我的XML中的org.hibernate.type.EnumType将一些硬编码值更改为Enum:

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

我第一次尝试运行hbm2java导致MyEnum发现'Enum class not found'。我意识到我需要将我的类添加到我的ant文件中的类路径中:

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

这一切都有效,但事实证明这只是因为我已经编译了${src.dir}${build.dir}的所有内容。如果我从一个“干净”状态开始,我再次得到'Enum class not found',因为它有一个循环依赖:为了编译代码,我需要POJO。但是为了获得POJO,我需要编译代码。

我能想到的唯一解决方案是首先在enum包中编译所有内容,然后运行hbm2java,然后编译其余部分。

我觉得很奇怪,但这是最好的解决方案吗? 或者是否有其他一些我没想过的解决方案?例如,有没有办法让它看一下我的源代码?

1 个答案:

答案 0 :(得分:2)

我最后使用我提出的解决方案,添加了一个只编译运行hbm2java所需类的ant任务。该任务命名为&#34; build-hibernate-dependencies&#34;,所以我只需为我的hbm2java目标添加一个depends属性:

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

    ...

    </hibernatetool>
</target>

目标&#34; build-hibernate-dependencies&#34;将枚举编译到构建目录:

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

之后,我现在可以编译整个项目了。

相关问题