• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

annotation: field Vs.method

 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • 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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
 
Yuval Goldstein
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic