JPA: Why the annotations are applied on getter or field

放肆的年华 提交于 2019-11-30 23:52:10
Yogendra Singh

This is is how it's specified. Per JPA Specification:

  • When field-based access is used, the object/relational mapping annotations for the entity class annotate the instance variables, and the persistence provider runtime accesses instance variables directly. All non-transient instance variables that are not annotated with the Transient annotation are persistent.
  • When property-based access is used, the object/relational mapping annotations for the entity class annotate the getter property accessors[7], and the persistence provider runtime accesses persistent state via the property accessor methods. All properties not annotated with the Transient annotation are persistent.
  • Mapping annotations must not be applied to fields or properties that are transient or Transient.

You have two options. Either use field level annotation or property (getter method) annotation. There is no third option.

Because for an Object, there r only two ways to access the properties, fields directly or getter indirectly.

for entity bean, annotation specifys how to map properties to the columns, and JPA needs to access these status of entity, so I guess this is the most intuitional way to put annotations on fields directly or getter.

When we put annotations on getters, JPA access properties via getters.There is no need to put annotations on setters.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!