• 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

annotation: field Vs.method

 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
using POJO, EJB3 and persistence, what's the difference in writing the annotation above the property field instead of the get() method?

Thanks in advance
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The difference is if a getter/setter method is called by the ORM implementation or not. If your Annotation is on the getter, then the getter/setters will be called when the ORM sets and gets the data, if the Annotatation is on the field, then the getters and setters are not called.

So the question is do you need to add special code in your getters and setters that you also want the ORM implementation have to call or not. If not stick with field.

Mark
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have another thing to consider.

The getter/setter option seem very tempting because you have the chance to manipulate the values of memebers before you return them.
However, If you have validation codein the setter methods, you may encounter a situation during the development, in which you read several records from the database , one record does not match the setter validation rules, the set method fails (hibernate use it to populate the object with data from a db record) and the whole read query fails.

This may also happen if you have a bug and the data in the database is corrupted (does not match the validation rules), you usually wouldn't want to fail the whole 'get many records' action because of one corrupted record.

Anyways, you candecide otherwise but this is the trade-off.

Cheers.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic