• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

field or property based access

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are pros and cons of using field or property based access in JPA entities? Which one is recommended?
 
author & internet detective
Posts: 40552
825
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Peter Curila
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Peter Curila
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
author & internet detective
Posts: 40552
825
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic