为什么将 jpa 注释应用于字段或 getter 方法。如果我尝试在 setter 方法上应用注释,则编译器会生成错误。因为编译器忽略了 setter 方法上的注解。他们背后的原因是什么?
最佳答案
这是它的指定方式。每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.
您有两个选择。使用字段级注释或属性(getter 方法)注释。没有第三种选择。
关于java - JPA:为什么注释应用于 getter 或字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14028198/