Hibernate/JPA – annotating bean methods vs fields [duplicate]

Yes, if you annotate the fields, Hibernate will use field access to set and get those fields. If you annotate the methods, hibernate will use getters and setters. Hibernate will pick the access method based on the location of the @Id annotation and to my knowledge you cannot mix and match. If you annotate a field with @Id, annotations on methods will be ignored and visa versa. You can also manually set the method with the class level annotation @AccessType

The Hibernate Annotations reference guide has proven to be an extremely useful resource for questions like this and details how access types cascade down hierarchies.

Leave a Comment