• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Entity - Field-Based Access Vs Property Based Access

 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

P-230 of EJB3 In Action Book states "If you want to use field-based access, you can declare all your POJO persisted
data fields public or protected"
P-231
"Even if you used field-based access, we recommend that you make the fields private and expose the fields to be modified
by getter/setter method."

The above 2 statements are contradictory... Can we mark our fields private and use them for field-based access ?
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandeep,

yes, you can make fields private and still use them as entity properties. Persistence providers like Hibernate are capable of accessing even private fields (i guess with reflection, bytecode manipulation or something like this).

The two statements are not really contradictory because of course you're free to make access modifiers less restrictive if it even works with private members. Though this is usually nothing you want to do.

Perhaps I should have noted that "field based access" in this context most probably does NOT mean regular field access in your code but instead it means access from the point of view of a persistence provider!

Marco
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Macro,
Thanks for a quick reply...

Marco Ehrentreich wrote:
The two statements are not really contradictory because of course you're free to make access modifiers less restrictive if it even works with private members. Though this is usually nothing you want to do.


[Sandeep:] Yes i agree that 2 statements are not contradictory but the first statement doesn;t mention about private fields which gives a false impression that fields-based access can;t be private.

Marco Ehrentreich wrote:
Perhaps I should have noted that "field based access" in this context most probably does NOT mean regular field access in your code but instead it means access from the point of view of a persistence provider!


[Sandeep:] I think field-based access means that i need not provide it;s getter and setters. Means the following code is still valid and work :


 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You got it exactly Your assumptions and the example are all correct!

Both ways have advantages and disadvantages. The getter/setter approach for example allows you to define bean properties for which there doens't even have to be a real class member, i.e. getters which return "calculated" values. On the other hand the field access approach doesn't force you to publich all class members just for the sake of a persistence provider.

I don't know if this will change with Java EE 6 / JPA 2 but it sometimes was annoying for me that you can't mix both ways to specify persistence properties on a per-property basis. It's either field access or getter access.


Marco
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marco Ehrentreich wrote:
I don't know if this will change with Java EE 6 / JPA 2 but it sometimes was annoying for me that you can't mix both ways to specify persistence properties on a per-property basis. It's either field access or getter access.



I *think* that has changed in JPA2. Although not exactly on the lines of allowing both field and getter access on the same entity. From what i remember, if you have multiple entities extending each other, then with JPA2, the base entity is allowed to have field access and the child entity getter access and vice versa.
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for this information, Jaikiran

I guess there will be good reasons that you can't mix field and getter access within a class. It could be very confusing and "convention over configuration" surely wouldn't work as nicely as it does now.

Marco
reply
    Bookmark Topic Watch Topic
  • New Topic