java - 可以为减速度添加注释吗

标签 java hibernate hibernate-annotations

我们可以向减速度添加注释,而不是向 getter 方法添加注释吗?

我已经习惯了

 @Column(name="Part_ID")
   public Long getPartId() {
   return partId;
 }

我们可以这样使用它

@Column(name="Part_ID")
private Long partId;

谢谢大家。

拉维

最佳答案

是的,可以。参见2.2.2.2 of the Hibernate annotations documentation部分:

By default the access type of a class hierarchy is defined by the position of the @Id or @EmbeddedId annotations. If these annotations are on a field, then only fields are considered for persistence and the state is accessed via the field. If there annotations are on a getter, then only the getters are considered for persistence and the state is accessed via the getter/setter. That works well in practice and is the recommended approach.

因此,如果您将 @Id 放在某个字段上,那么 Hibernate 将查看该字段上的注释以获取其他属性。如果您将 @Id 放在 getter 上,那么 Hibernate 将查看其他 getter 上的注释。

关于java - 可以为减速度添加注释吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3232676/

相关文章:

java - 无效数据访问ApiUsageException : detached entity passed to persist

java - Hibernate OneToMany 映射 - 实体集保存在哪里?

java - Hibernate 注释错误(org.hibernate.exception.SQLGrammarException : could not insert: [com. hib.ex.entity.Node])

javac 提示 : cannot find symbol on enum implementing interface

java - 使所有按钮大小相同

java - 多个按钮,每个按钮选择不同的 Activity

java - 如何使用 Hibernate 控制 INSERT 语句中的并发性

java - 在 CrudRepository 中创建 JPQL 查询

java - 如何在hibernate注解中实现组件映射

java - 在Java中如何初始化相互依赖的类?