Hibernate Annotation Placement Question
From a performance and design perspective, using annotations on getters is a better idea than member variables, because the getter setters are called using reflection if placed on the field, than a method. Also if you plan to use validation and other features of hibernate, you'll have all the annotations at one place, rather than scattering them all over the place.
My recommendation go with methods not member variables.
From the documentation
Depending on whether you annotate fields or methods, the access type used by Hibernate will be field or property. The EJB3 spec requires that you declare annotations on the element type that will be accessed, i.e. the getter method if you use property access, the field if you use field access. Mixing EJB3 annotations in both fields and methods should be avoided. Hibernate will guess the access type from the position of @Id or @EmbeddedId.
You got me on the right track toolkit. Thanks. Here's the deal... Of course, my contrived example didn't include the whole story. My Widget class was actually much larger than the example I gave. I have several extra fields/getters and I was MIXING my annotations. So I was annotating the @Id on the field but others were being annotated on the getter.
So the moral of the story is that you cannot mix annotation locations. Either all annotations are on the fields or they are on the getter methods. Long time Java and Hibernate, new to Annotations. Learn something every day.
Once I knew what to Google for I came across this which was helpful - http://chstath.blogspot.com/2007/05/field-access-vs-property-access-in-jpa.html
Of course, this now brings up the question as to which is better from design and performance perspectives.
A long reach, but do you have an old *.hbm.xml
file floating around?
Perhaps it might be picking up the wrong setting for default-access
and using property
instead of field
?
This is a very nice link that can help you with understanding the accesstypes and the relted best practices!
Understading @AccessType in Hibernate