Hibernate - moving annotations from property (method) level to field level

帅比萌擦擦* 提交于 2019-12-17 22:43:55

问题


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?

Appreciate your help and time.


回答1:


Here are the steps:

  1. Allocate "hibernate-tools.jar" by perform a search within your eclipse folder For example, You will find it at C:\eclipse\plugins\org.hibernate.eclipse_3.4.1.xxx\lib\tools

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

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

    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

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

    <#include "GetPropertyAnnotation.ftl"/>
    
  5. Add field-level annotations: In file "PojoFields.ftl", add

    <#include "Ejb3FieldGetAnnotation.ftl"/>
    ${pojo.getFieldModifiers(field)} ... 
    
  6. 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>
    

Hope it work for you.




回答2:


I spent a lot of time reading answers from 5+ years ago without understanding how to do it (especially if you work in Intellij and not Eclipse) and how come this is not already solved. So i found it, here it is, and it is simple:

In Intellij:

  1. Create a file orm.xml in the same folder as your persistence.xml with this content
<?xml version="1.0" encoding="UTF-8"?>
    <entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
                     version="2.0">
        <persistence-unit-metadata>
            <persistence-unit-defaults>
                <access>FIELD</access>
            </persistence-unit-defaults>
        </persistence-unit-metadata>
    </entity-mappings>
  1. Now you can generate your pojos (Generate persistence mapping -> By database schema -> choose your tables etc and don't forget to tick the "Generate JPA Annotations")

Your entities will have field annotations!

@Entity
@Table(name = "user", schema = "public", catalog = "my_db")
public class User {
    @Id
    @Column(name = "id")
    private Integer id;
...
}



回答3:


currently it is necessary to use custom templates. here is more references and examples hot to implement this: https://forum.hibernate.org/viewtopic.php?f=6&t=1003858&p=2429868#p2429868



来源:https://stackoverflow.com/questions/1861817/hibernate-moving-annotations-from-property-method-level-to-field-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!