如何从带有字段级别注释的表生成休眠域类?我使用了 Hibernate Tools 项目并从数据库中的表生成了域类。生成的类在 getter 方法上而不是在字段级别上有注释。请建议一种生成带有注释字段的域类的方法。 eclipse/IDEA 等中是否有任何可用的重构工具来将注释从方法级别移动到字段级别?
感谢您的帮助和时间。
最佳答案
以下是步骤:
例如,您会在
C:\eclipse\plugins\org.hibernate.eclipse_3.4.1.xxx\lib\tools
例如解压到
[Your Project]\template
该文件实际上是“Ejb3PropertyGetAnnotation.ftl”的副本,但所有单词“property”都被替换为“field”,因为该文件将被放置在遍历所有字段(而不是属性)的循环中。
我在这篇文章中包含了文件的内容
在文件“PojoPropertyAccessors.ftl”中,删除或注释掉
<#include "GetPropertyAnnotation.ftl"/>
在文件“PojoFields.ftl”中,添加
<#include "Ejb3FieldGetAnnotation.ftl"/>
${pojo.getFieldModifiers(field)} ...
==================================================================================
Ejb3FieldGetAnnotation.ftl
==================================================================================
<#if ejb3>
<#if pojo.hasIdentifierProperty()>
<#if field.equals(clazz.identifierProperty)>
${pojo.generateAnnIdGenerator()}
<#-- if this is the id property (getter)-->
<#-- explicitly set the column name for this property-->
</#if>
</#if>
<#if c2h.isOneToOne(field)>
${pojo.generateOneToOneAnnotation(field, cfg)}
<#elseif c2h.isManyToOne(field)>
${pojo.generateManyToOneAnnotation(field)}
<#--TODO support optional and targetEntity-->
${pojo.generateJoinColumnsAnnotation(field, cfg)}
<#elseif c2h.isCollection(field)>
${pojo.generateCollectionAnnotation(field, cfg)}
<#else>
${pojo.generateBasicAnnotation(field)}
${pojo.generateAnnColumnAnnotation(field)}
</#if>
</#if>
希望它对你有用。
关于reverse-engineering - Hibernate - 将注释从属性(方法)级别移动到字段级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1861817/