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

How can I generate hibernate domain classes from tables with field level annotations? I used the Hibernate Tools project and generated domain classes from tables in the database. Generated classes have annotations on getter methods, and not at the field level. Please advise how to create domain classes that have annotated fields. Are there any refactoring objects in eclipse / IDEA etc. to move annotations from method level to field level?

Appreciate your help and time.

+11
hibernate-annotations reverse-engineering
Dec 07 '09 at 18:07
source share
3 answers

Here are the steps:

  • Highlight "hibernate-tools.jar" by searching your eclipse folder. For example, you will find it in C:\eclipse\plugins\org.hibernate.eclipse_3.4.1.xxx\lib\tools

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

  • In the [Your project] \ template \ pojo folder, create a file named "Ejb3FieldGetAnnotation.ftl"

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

  • Delete annotations at the property level: In the "PojoPropertyAccessors.ftl" file, delete or comment

     <#include "GetPropertyAnnotation.ftl"/> 
  • Add field level annotations: In the file "PojoFields.ftl" add

     <#include "Ejb3FieldGetAnnotation.ftl"/> ${pojo.getFieldModifiers(field)} ... 
  • When creating Java objects, select Use Custom Templates and specify a 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 works for you.

+14
May 29 '12 at 15:36
source share

I spent a lot of time reading answers from 5 years ago, not understanding how to do this (especially if you work in Intellij and not in Eclipse), and why this has not yet been decided. So, I found it here and it is simple:

In Intellij:

  • Create an orm.xml file 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 (Create a persistence mapping → by database schema → select your tables, etc. and don't forget to check “Generate JPA annotations”)

Your objects will have field annotations!

 @Entity @Table(name = "user", schema = "public", catalog = "my_db") public class User { @Id @Column(name = "id") private Integer id; ... } 
+1
May 03 '16 at 12:53 on
source share

Custom templates are currently required. here are more links and examples hot for implementing this: https://forum.hibernate.org/viewtopic.php?f=6&t=1003858&p=2429868#p2429868

0
May 6 '10 at 19:05
source share



All Articles