File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes field or property based access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "field or property based access" Watch "field or property based access" New topic

field or property based access

Peter Curila

Joined: Oct 11, 2005
Posts: 20
What are pros and cons of using field or property based access in JPA entities? Which one is recommended?
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

You basically mean the difference between mapping to the class attribute or a getter/setter

The main difference is if you need some extra code to run before you set or get the value, some extra validation if you need it to go above and beyond Hibernate's built in validation on fields.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Peter Curila

Joined: Oct 11, 2005
Posts: 20
Thank you Mark,

I am about to start new project with EJB3 and I am new to that.
I am asking because many people choose property based access because of better encapsulation.

But I have an idea to make my entities with private fields so only entity manager can access them. Then write only necessary getters and setters for clients and business logic like validation store in entity listeners.

I agree. Here's the link:
subject: field or property based access
It's not a secret anymore!