why the jpa annotations are applied on field or on getter methods. if i try to apply the annotations on setter method then compiler generate the error. because compiler ignore the annotation on setter method. what is the reason behind them?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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.
回答2:
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.
回答3:
When we put annotations on getters, JPA access properties via getters.There is no need to put annotations on setters.