• 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

Mapping annotations for variables.

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


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

 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would think this example is correct. There is chosen for field-based persistence.
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Deepika Joshi
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot for your detailed answer.
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic