IntelliJ Java IDE
The moose likes Object Relational Mapping and the fly likes annotation: field Vs.method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "annotation: field Vs.method" Watch "annotation: field Vs.method" New topic
Author

annotation: field Vs.method

Alessandro Ilardo
Ranch Hand

Joined: Dec 23, 2005
Posts: 218
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


trying to decode a woman mind....
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 16622

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Yuval Goldstein
Greenhorn

Joined: Dec 27, 2006
Posts: 18
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.
 
 
subject: annotation: field Vs.method
 
Threads others viewed
Anotation location
about SCBCD
ElementType.TYPE versus ElementType.FIELD
I am getting null value for particular column, while loading the object.
Generating PK
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com