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

标签 java hibernate jpa annotations jpa-2.0

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

相关文章:

java - 如何有效地用常量值初始化Java中的数组

java - 从另一个目录读取 .txt 文件

java - 使用Spring时如何注入(inject)多个JPA EntityManager(持久化单元)

java - 消息传递代理和缓冲区

java - 带有可选嵌入式 Tomcat 容器的 Spring Boot 中的 JNDI

java - 需要重新设置oracle中sequence的值

java - 如何检查 Hibernate 代理的代码长度?

java - 使用 Spring 框架获取 org.hibernate.MappingException : Unknown entity.

java - 如何在 Spring Boot 应用程序中保存 Postgres 中的对象列表

java - Hibernate 可以用作 Google App Engine 中的 JPA 提供者吗