Hibernate - 将注释从属性(方法)级别移动到字段级别 [英] Hibernate - moving annotations from property (method) level to field level

查看:70
本文介绍了Hibernate - 将注释从属性(方法)级别移动到字段级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从带有字段级别注释的表生成休眠域类?我使用了 Hibernate Tools 项目并从数据库中的表生成了域类.生成的类在 getter 方法上而不是在字段级别上有注释.请建议一种生成带有注释字段的域类的方法.eclipse/IDEA 等中是否有任何可用的重构工具来将注释从方法级别移动到字段级别?

How do I generate hibernate domain classes from tables with annotations at field level? I used Hibernate Tools project and generated domain classes from the tables in the database. The generated classes have annotations on the getter methods rather than at the field level. Kindly advice a way to generate domain classes that have the fields annotated. Is there any refactoring facility available in eclipse/IDEA etc.. to move the annotations from method level to field level?

感谢您的帮助和时间.

推荐答案

步骤如下:

  1. 通过在 eclipse 文件夹中执行搜索来分配hibernate-tools.jar"例如,您会在C:\eclipse\plugins\org.hibernate.eclipse_3.4.1.xxx\lib\tools

解压到临时文件夹(WinRar 可以做到)例如,解压到[Your Project]\template

Extract to a temp folder (WinRar can do this) For example, extract to [Your Project]\template

在 [Your Project]\template\pojo 文件夹下,创建一个名为Ejb3FieldGetAnnotation.ftl"的文件

Under [Your Project]\template\pojo folder, create a file named "Ejb3FieldGetAnnotation.ftl"

该文件实际上是Ejb3PropertyGetAnnotation.ftl"的副本,但所有单词property"都替换为field",因为该文件将被放置在遍历所有字段(而不是属性)的循环中.我在这篇文章中包含了文件的内容

This file is actually a copy of "Ejb3PropertyGetAnnotation.ftl" but all of words "property" are replaced by "field" because this file will be placed in the a loop that iterates through all fields (instead of properties). I include the content of the file in this post

删除属性级注释:在文件PojoPropertyAccessors.ftl"中,删除或注释掉

Remove property-level annotations: In file "PojoPropertyAccessors.ftl", remove or comment out

<#include "GetPropertyAnnotation.ftl"/>

  • 添加字段级注释:在文件PojoFields.ftl"中,添加

  • Add field-level annotations: In file "PojoFields.ftl", add

    <#include "Ejb3FieldGetAnnotation.ftl"/>
    ${pojo.getFieldModifiers(field)} ... 
    

  • 生成Java实体时,选择使用自定义模板"并指定模板文件夹.在这种情况下,它将是 [Your Project]\template

  • When generate Java entities, select "Use Custom Templates" and specify the template folder. In this case, it will be [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>
    

  • 希望它对你有用.

    这篇关于Hibernate - 将注释从属性(方法)级别移动到字段级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆