This question is somewhat related to Hibernate Annotation Placement Question.
But I want to know which is better? Access via properties or access vi
I prefer using field access for the following reasons:
The property access can lead to very nasty bugs when implementing equals/hashCode and referencing fields directly (as opposed through their getters). This is because the proxy is only initialized when the getters are accessed, and a direct-field access would simply return null.
The property access requires you to annotate all utility methods (e.g. addChild/removeChild) as @Transient
.
With field access we can hide the @Version field by not exposing a getter at all. A getter can also lead to adding a setter as well, and the version
field should never be set manually (which can lead to very nasty issues). All version incrementation should be triggered through OPTIMISTIC_FORCE_INCREMENT or PESSIMISTIC_FORCE_INCREMENT explicit locking.