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 Certification (SCBCD/OCPJBCD) and the fly likes Mapping annotations for variables. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Mapping annotations for variables." Watch "Mapping annotations for variables." New topic
Author

Mapping annotations for variables.

Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757


Is this code correct? Here @Id annotation is used for a variable, even though getter/setter methods were used.

Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
I would think this example is correct. There is chosen for field-based persistence.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Deepika Joshi
Ranch Hand

Joined: Feb 24, 2009
Posts: 268
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
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
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
thanks a lot for your detailed answer.
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mapping annotations for variables.
 
Similar Threads
Property versus Field based Persistence
how to use Xdoclet in standalone application
dynamic drop down list
what is super?
Problems Inserting Info. into Access DB