Hibernate/JPA - annotating bean methods vs fields [duplicate]
I have a simple question about usage of Hibernate. I keep seeing people using JPA annotations in one of two ways by annotating the fields of a class and also by annotating the get method on the corresponding beans.
My question is as follows: Is there a difference between annotating fields and bean methods with JPA annoations such as @Id.
example:
@Entity
public class User
{
**@ID**
private int id;
public int getId(){
return this.id;
}
public void setId(int id){
this.id=id;
}
}
-----------OR-----------
@Entity
public class User
{
private int id;
**@ID**
public int getId(){
return this.id;
}
public void setId(int id){
this.id=id;
}
}
Yes, if you annotate the fields, Hibernate will use field access to set and get those fields. If you annotate the methods, hibernate will use getters and setters. Hibernate will pick the access method based on the location of the @Id
annotation and to my knowledge you cannot mix and match. If you annotate a field with @Id
, annotations on methods will be ignored and visa versa. You can also manually set the method with the class level annotation @AccessType
The Hibernate Annotations reference guide has proven to be an extremely useful resource for questions like this and details how access types cascade down hierarchies.
Yes, I believe you want to search on field versus property access:
Hibernate Annotations - Which is better, field or property access?
The Spring preference is field access. That's what I follow.
My recommendation is to annotate the methods. By doing so, you gain a little flexibility. For example, let's say you have a few classes:
AbstractEntity
StringIdEntity
-
AutoIdEntity
AbstractEntity
defines the id field/getter/setter. The classes StringIdEntity
and AutoIdEntity
inherit from AbstractEntity
, but use different @Id
strategies. If you annotate the field, you cannot change it from class to another.
If you annotate the methods, you mark getId()
as @Transient/abstract
in AbstractEntity
and then in the subclasses, you simply override the method and apply the strategy you wish to use. I used to annotate fields myself and ran into this and decided that I will always annotate methods moving forward.
So, even if you don't see the benefits of annotating methods immediately, it may become obvious down the line when you have so many classes that switching is going to become an absolute headache.
Why would you annotate the accessors? It just looks plain messy and is a pain to maintain. Now I have to hunt all over the class to find out how JPA or Hibernate is applied. When working with some Hibernate code generation plugins for Eclipse this is the default and it drivers me bonkers.
Not to mention that another reason we use accessors is to add logic to the property access or reference parent or other object which does not meld well with said annotations.
There are several discussion available that gives edge to use FIELD over property (see - http://java.dzone.com/tips/12-feb-jpa-20-why-accesstype). Even spring framework also recommends using FIELD over property (see - http://static.springsource.org/spring/docs/2.5.x/reference/orm.html). I find FIELD more cleaner approach not only because you are not forced to implement setters/getters but you can have your custom type setters/getters in your class. So a better encapsulation and more clean classes. Performance wise FIELD has a bit edge over PROPERTY but which is negligible.
IN one line use FIELD over PROPERTY. Use PROPERTY if u really need it.