我正在使用 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,然后编译其余部分。
这对我来说似乎很奇怪,但这是最好的解决方案吗?还是有其他我没有想到的解决方案?例如,有没有办法让它查看我的源代码?