Wednesday, April 22, 2009

Using field level access for hibernate entities

Hibernate allows you to configure the access mechanism for attributes in the entity object. It can access the attributes directly or through the accessors. The field or property access type is decided by hibernate based on the location of @Id or @EmbeddedId.

@Entity
public class Item {
@Id
public String id;
public long itemNumber;
}

@Entity
public class FoodItem {
private String id;
private long foodId;

@Id
public String getId(){
return id;
}
public long getFoodId(){
return foodId;
}
}

In the above code, Item class uses field access and FoodItem class uses property access.

Using property access mechanism allows classes to hide the internal data structure from the external world and it enables class to perform validations while setting the value or while accessing it. It is useful when we want to perform some calculation before returning the result or setting the value.

However, mostly we end up directly accessing the variable from the getter and setter methods. In that case, property access is overkill. It makes us write lot of getter - setters which are not used anywhere else in the code. It also exposes setters if when we don't want anyone to set the value of fields like 'id'. We can mark the accessors private or protected and avoid this problem, but we will still have the code.

IMHO, while creating the entity, we should start with field level access. Doing so will reduce the unnecessary code which is used only by hibernate. We should keep the fields private and provide accessor methods only when required. So our entity object will be modeled by the needs of the application and not by the hibernate. In future, if we need to validate or manipulate the values set on the object (which mostly won't happen), we can then switch to property access, but we shouldn't start with it.

36 comments:

sud said...

Wouldn't you want to always stick with Field access and use the entity lifecycle events for any persistence logic.

The getters and setters can still be used if necessary for business logic.

Anna said...
This comment has been removed by the author.
vigneswaran said...


If you want to throw JSON lib mix like to converting lib to setter properties for mapping to guarantee sync with persistence to JSON lib bean mapper manager


Selenium Training in Chennai
Best Selenium Training

pooja said...

I am a regular reader of your blog and being students it is great to read that your responsibilities have not prevented you from continuing your study and other activities. Love
Big data training in Velachery
Big data training in Marathahalli
Big data training in btm
Big data training in Rajajinagar
Big data training in bangalore

Unknown said...

Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting.So know it's helpful.

Blueprism training in Chennai

Blueprism training in Bangalore

Blueprism training in Pune

Blueprism training in tambaram

Blueprism training in annanagar

Blueprism training in velachery

Blueprism training in marathahalli

gowsalya said...

Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
Devops Training in Chennai

Devops Training in Bangalore

Devops training in sholinganallur

Unknown said...

Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
Python training in pune
AWS Training in chennai

Unknown said...

Really nice experience you have. Thank you for sharing. It will surely be an experience to someone.

java training in chennai | java training in bangalore

java online training | java training in pune

Unknown said...

Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
Data science training in tambaram | Data Science training in anna nagar
Data Science training in chennai | Data science training in Bangalore
Data Science training in marathahalli | Data Science training in btm

gowthunan said...

I wish to show thanks to you just for bailing me out of this particular trouble. As a result of checking through the net and meeting techniques that were not productive, I thought my life was done.
industrial course in chennai

Anonymous said...

Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us

angularjs Training in btm

angularjs Training in electronic-city

angularjs online Training

angularjs Training in marathahalli

angularjs interview questions and answers

sunshineprofe said...

Existing without the answers to the difficulties you’ve sorted out through this guide is a critical case, as well as the kind which could have badly affected my entire career if I had not discovered your website.
iosh course in chennai

Anbarasan14 said...

This is an excellant blog. Thanks for taking time to share this information. Waiting for more updates.

IELTS Training Institute in Guduvanchery
IELTS Coaching in Tambaram
IELTS Coaching Centre in Tambaram
IELTS Training in Tambaram
IELTS Coaching Class in Velachery
IELTS Coaching near me
IELTS Coaching in Chennai Tharamani

Priyanka said...

Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
python training in bangalore

Anonymous said...

xilften apk

whatsapp plus themes said...

business whatsapp groups

whatsapp plus themes said...

business whatsapp groups

kevin said...

This is most user friendly and informative.Keep posting more blog like this,Thank you...
Hadoop training in Bangalore|
Big Data Analytics Training in Bangalore|
Hadoop Training in Bellandur|
Hadoop Training in Bangalore
Hadoop Training in Marathahalli

jude said...

Amazing blog with the recent news. Thank you very much for sharing such helpful data...
Big Data Analytics Training in Bangalore|
Hadoop Training in Bellandur|
Hadoop Training in Bangalore|
Hadoop Training in Marathahalli|
Hadoop training in Bangalore

gopi said...

Good information.Thank you for sharing this wonderful article.It was so good to read and upgrade my understanding...
Big Data Analytics Training in Bangalore|
Hadoop Training in Bellandur|
Hadoop Training in Marathahalli|
Hadoop training in Bangalore|
Angular JS Training in Bangalore|
Big Data Training in Bangalore

dhanush kumar said...


Actually I read it yesterday but I had some thoughts about it and today I wanted to read it again because it is very well written.

salesforce Training in Bangalore
uipath Training in Bangalore
blueprism Training in Bangalore

dhanush kumar said...

Thank you for sharing wonderful information with us to get some idea about that content.

salesforce Training in Bangalore
uipath Training in Bangalore
blueprism Training in Bangalore

Archana said...

I went through your blog, it was very helpful to me and I got some fresh data as well...
aws Training in Bangalore
python Training in Bangalore
hadoop Training in Bangalore
angular js Training in Bangalore
bigdata analytics Training in Bangalore

nash b said...

Great Post...
final year project proposal for information technology

free internship for bca

web designing training in chennai

internship in coimbatore for ece

machine learning internship in chennai

6 months training with stipend in chennai

final year project for it

inplant training in chennai for ece students

industrial training report for electronics and communication

inplant training certificate

parvina said...

Your article is just amazing.You might be interested in:yesmovies.to apk

Lily said...

I Love your article. You can visit my website : the sims free play mod

ethiraj raj said...

Very interesting, good job and thanks for sharing such a good blog. your article is so convincing that I never stop myself to say something about it. You’re doing a great job. Keep it up

aws Training in Bangalore
python Training in Bangalore
hadoop Training in Bangalore
angular js Training in Bangalore
bigdata analytics Training in Bangalore

vijay said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

aws Training in Bangalore
python Training in Bangalore
hadoop Training in Bangalore
angular js Training in Bangalore
bigdata analytics Training in Bangalore
python Training in Bangalore
aws Training in Bangalore

Maneesha said...

Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
data analytics courses

Durai Moorthy said...

I am really happy with your blog because your article is very unique and powerful for new reader.
aws Training in Bangalore
python Training in Bangalore
hadoop Training in Bangalore
angular js Training in Bangalore
bigdata analytics Training in Bangalore
python Training in Bangalore
aws Training in Bangalore

Data Science Course said...

Impressive! I finally found great post here. Nice article on data science . It's really a nice experience to read your post. Thanks for sharing your innovative ideas to our vision.
Data Science Course

divya said...

I’m planning to start my blog soon, but I’m a little lost on everything. Would you suggest starting with a free platform like Word Press or go for a paid option?thanks a lot.
Ai & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai

Lopa said...

Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.

Digital Marketing Online Course
AWS Online Course
Python Online Course
Selenium Online Course
Data Science Online Course
DevOps Online Course

aarthi said...

Simple and excellent article.It contains lot of resources.Thank you. Java training in Chennai | Certification | Online Course Training | Java training in Bangalore | Certification | Online Course Training | Java training in Hyderabad | Certification | Online Course Training | Java training in Coimbatore | Certification | Online Course Training | Java training in Online | Certification | Online Course Training

Data Analytics Courses in Bangalore said...

I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors.
data analytics courses in bangalore

Devi said...

Get Big Data Certification in Chennai for making your career as a shining sun with Infycle Technologies. Infycle Technologies is the best Big Data training institute in Chennai, providing complete hands-on practical training of professional specialists in the field. In addition to that, it also offers numerous programming language tutors in the software industry such as Oracle, Java, Python, AWS, Hadoop, etc. Once after the training, interviews will be arranged for the candidates, so that, they can set their career without any struggle. Of all that, 200% placement assurance will be given here. To have the best career, call 7502633633 to Infycle Technologies and grab a free demo to know more.
Big Data Training Institute in Chennai | Infycle Technologies