11 reverse-engineering hibernate-annotations
如何从字段级别带注释的表生成hibernate域类?我使用了Hibernate Tools项目并从数据库中的表中生成了域类.生成的类在getter方法上有注释,而不是在字段级别.请建议一种方法来生成具有注释字段的域类.在eclipse/IDEA等中是否有任何重构工具可以将注释从方法级别移动到字段级别?
感谢您的帮助和时间.
小智 16
以下是步骤:
通过在eclipse文件夹中执行搜索来分配"hibernate-tools.jar"例如,您将在以下位置找到它
C:\eclipse\plugins\org.hibernate.eclipse_3.4.1.xxx\lib\tools
提取到临时文件夹(WinRar可以这样做)例如,提取到 [Your Project]\template
在[Your Project]\template\pojo文件夹下,创建名为"Ejb3FieldGetAnnotation.ftl"的文件
此文件实际上是"Ejb3PropertyGetAnnotation.ftl"的副本,但所有单词"property"都替换为"field",因为此文件将放置在循环遍历所有字段(而不是属性)的循环中.我在这篇文章中包含了文件的内容
删除属性级注释:在文件"PojoPropertyAccessors.ftl"中,删除或注释掉
<#include "GetPropertyAnnotation.ftl"/>
Run Code Online (Sandbox Code Playgroud)添加字段级注释:在文件"PojoFields.ftl"中,添加
<#include "Ejb3FieldGetAnnotation.ftl"/>
${pojo.getFieldModifiers(field)} ...
Run Code Online (Sandbox Code Playgroud)生成Java实体时,选择"使用自定义模板"并指定模板文件夹.在这种情况下,它将是[Your Project]\template
==================================================================================
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>
Run Code Online (Sandbox Code Playgroud)希望它对你有用.
目前需要使用自定义模板。这里有更多的参考和示例来实现这一点: https://forum.hibernate.org/viewtopic.php ?f=6&t=1003858&p=2429868#p2429868