我们可以向减速度添加注释,而不是向 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/