Hibernate Annotation Placement Question

后端 未结 5 1617
离开以前
离开以前 2020-12-01 00:41

I\'ve got what I think is a simple question. I\'ve seen examples both ways. The question is - \"why can\'t I place my annotations on the field?\". Let me give you an exam

5条回答
  •  生来不讨喜
    2020-12-01 00:58

    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.

提交回复
热议问题