Hibernate Annotations - Which is better, field or property access?
I prefer accessors, since I can add some business logic to my accessors whenever I need.
Here's an example:
@Entity
public class Person {
@Column("nickName")
public String getNickName(){
if(this.name != null) return generateFunnyNick(this.name);
else return "John Doe";
}
}
Besides, if you throw another libs into the mix (like some JSON-converting lib or BeanMapper or Dozer or other bean mapping/cloning lib based on getter/setter properties) you'll have the guarantee that the lib is in sync with the persistence manager (both use the getter/setter).
What is the purpose of AccessType.FIELD, AccessType.PROPERTY and @Access
By default the access type is defined by the place where you put your identifier annotation (@Id
). If you put it on the field - it will be AccessType.FIELD
, if you put it on the getter - it will be AccessType.PROPERTY
.
Sometimes you might want to annotate not fields but properties (e.g. because you want to have some arbitrary logic in the getter or because you prefer it that way.) In such situation you must define a getter and annotate it as AccessType.PROPERTY
.
As far as I remember, if you specify either AccessType.FIELD
or AccessType.PROPERTY
on any of the entity fields / methods you must specify the default behaviour for the whole class. And that's why you need to have AccessType.FIELD
on the class level (despite that AccessType.FIELD
is the default value.)
Now, if you wouldn't have @Transient
on the phnnumber
field, the JPA would provide you with a 3 columns table:
- id,
- phnnumber,
- getphnnumber.
That's because it would use AccessType.FIELD
for all of the entity fields (id
and phnnumber
) and, at the same time, it'd use AccessType.PROPERTY
for your getter (getPhnnumber()
).
You'll end with phone number mapped twice in the database.
Therefore, the @Transient
annotation is required - it means that the entity won't store the value of the field in the underlying storage but the value returned by your getter.
Performance difference between annotating fields or getter methods in Hibernate / JPA
Loaded 5000 records into a simple 3 column table. Mapped two classes to that table, one using annotated private fields and another using annotated public getters. Ran 30 runs of Spring's HibernateTemplate.loadAll() followed by a HibernateTemplate.clear() to purge the Session cache. Results in ms below...
methods total: 6510, average: 217
fields total: 6586, average: 219
I should probably take another stab at it after adding more properties to each class but right now the difference doesn't appear to be statistically significant.
Hibernate/JPA - annotating bean methods vs fields
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.
Where should I apply JPA annotations , getters or fields?
There is a difference between Field and Property access as others have stated. Field indicates direct access by the persistence provider and Property (method access) indicates the use of the getter/setter of your choosing to access the data.
I would recommend starting with Field access as in my experience this is easier/safer for new users to get started with. Property access can be very useful, and may be generally preferred by some, but it can also lead to problems if you are not careful (due to the additional behavior that getters and setters may have). So in general it is best to use Field access as a beginner unless you have a reason to use Property access.
I also recommend picking up a copy of Pro JPA 2 by Mike Keith and Merrick Schincariol if you are just getting started with JPA. It covers this and many other related topics.
Related Topics
Why Does Runtime.Exec(String) Work for Some But Not All Commands
Combineddomainxyplot Not Rescaling Domain Axis
Jformattedtextfield Is Not Properly Cleared
Java 8 Iterable.Foreach() VS Foreach Loop
What Does the Arrow Operator, '->', Do in Java
Where Are Static Methods and Static Variables Stored in Java
Java.Rmi.Serverexception: Remoteexception Occurred in Server Thread (Classnotfoundexception)
Implementing a Simple File Download Servlet
How to Write a Custom JSON Deserializer for Gson
How to Configure Port for a Spring Boot Application
Java: How to Start a Standalone Application from the Current One When Both Are in the Same Package
What's the Difference Between Concurrenthashmap and Collections.Synchronizedmap(Map)
Using Env Variable in Spring Boot's Application.Properties
Java: Insert Multiple Rows into MySQL with Preparedstatement