File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies 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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "field or property based access" Watch "field or property based access" New topic
Author

field or property based access

Peter Curila
Greenhorn

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?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30057
    
149

Peter,
Neither is going to always be recommended over the other. A lot of it is preferences. Personally, I prefer property based access because it gives me the OO encapsulation. It's easier to develop that way up front than add it later.

Do note that you must use the same technique throughout your EJB. You can't use some field based and some property based.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Peter Curila
Greenhorn

Joined: Oct 11, 2005
Posts: 20
Thank you Jeanne,
but I thought that with field based access I received better encapsulation. Fields would be private, only entity manager could access them from outside. And I would write only necessary getters and setters for clients.

I am asking because I am about to start new project and there is no easy way to change my mind later.
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

In both the cases the fields would be private. You can put annotations either on field or property. It is personal choice.

One thing using the property based access, you can put extra logic in the method for validation purpose.

Thanks
[ February 02, 2008: Message edited by: Narendra Dhande ]

Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Peter Curila
Greenhorn

Joined: Oct 11, 2005
Posts: 20
Thanks Narendra,

I can also put my validation logic in setters methods. Only entity manager
can by pass this logic. But I suppose that on DB is everything OK. If not there is possibility to put this logic into entity listeners.

Peter
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30057
    
149

Originally posted by Peter Curila:
Only entity manager can by pass this logic.

I like my test code to exercise as much of the code as the production code. Minimizing the difference in paths that the entity manager takes (through my code) helps achieve this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: field or property based access
 
Similar Threads
Mapping annotations for variables.
field or property based access
different betwenn property based and field based persistance
Annotations on instance declaration or get method using JPA in entities?
The difference between field-based access and property-based access