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.
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:
- 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>
- 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;
...
}
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