I got a doubt, when user uses field-based, then do we need to declare field with public access?
all though it has public getter/setter but when we assume it's field-based then should not that field be public?
Remko Strating
Ranch Hand
Joined: Dec 28, 2006
Posts: 893
posted
0
I've not tried it. I use property-based-access at the moment
But reading page 231 of EJB in Action they recommend that by using field-based-access that you mark your fields private and expose these fields by a getter/setter method. This means that it's possible to mark your fields private with a field-based-access.
Ralph Jaus
Ranch Hand
Joined: Apr 27, 2008
Posts: 342
posted
0
Deepika Joshi wrote:
I got a doubt, when user uses field-based, then do we need to declare field with public access?
The access type determines how the persistence provider accesses persistent state: See JPA spec 2.1.1:
If the entity has field-based access, the persistence provider runtime accesses instance variables directly.
If the entity has property-based access, the persistence provider runtime accesses persistent state via the property accessor methods.
The entity's variables must not be public and should be accessed by the user via getter/setter methods: See JPA spec 2.1:
An instance variable may be directly accessed only from within the methods of the entity by the entity instance itself. Instance variables must not be accessed by clients of the entity. The state of the entity is available to clients only through the entity's accessor methods (getter/setter methods) or by other business methods. Instance variables must be private, protected, or package visibility.
SCJP 5 (98%) - SCBCD 5 (98%)
Deepika Joshi
Ranch Hand
Joined: Feb 24, 2009
Posts: 268
posted
0
thanks a lot for your detailed answer.
Hong Anderson
Ranch Hand
Joined: Jul 05, 2005
Posts: 1936
posted
0
Treimin Clark wrote:
Is this code correct? Here @Id annotation is used for a variable, even though getter/setter methods were used.
Correct, we can use @Id on field or getter method.
Entity's fields can be private, package, protected but cannot be public.
SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional