0

我正在使用 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 出现“找不到枚举类”。我意识到我需要将我的类添加到我的 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}. 如果我从“干净”状态开始,我会再次得到“未找到枚举类”,因为它具有循环依赖关系:为了编译代码,我需要 POJO。但为了获得 POJO,我需要编译后的代码。

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

这对我来说似乎很奇怪,但这是最好的解决方案吗?还是有其他我没有想到的解决方案?例如,有没有办法让它查看我的源代码?

4

1 回答 1

2

我通过添加一个仅编译运行 hbm2java 所需的类的 ant 任务来结束使用我提出的解决方案。该任务被命名为“build-hibernate-dependencies”,所以我只是为它添加了一个依赖属性到我的 hbm2java 目标中:

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

    ...

    </hibernatetool>
</target>

目标“build-hibernate-dependencies”将枚举编译到构建目录:

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

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

于 2013-07-23T22:18:59.633 回答