gpt4 book ai didi

java - JPA:为什么注释应用于 getter 或字段

转载 作者:搜寻专家 更新时间:2023-11-01 01:06:32 25 4
gpt4 key购买 nike

为什么将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com