Hibernate/JPA - 注释 bean 方法与字段

Hibernate/JPA - 注释 bean 方法与字段

问题描述:

我有一个关于 Hibernate 使用的简单问题.我一直看到人们以两种方式之一使用 JPA 注释,方法是对类的字段进行注释,另一种是在相应的 bean 上注释 get 方法.

I have a simple question about usage of Hibernate. I keep seeing people using JPA annotations in one of two ways by annotating the fields of a class and also by annotating the get method on the corresponding beans.

我的问题如下:使用@Id等JPA注解注解字段和bean方法是否有区别.

My question is as follows: Is there a difference between annotating fields and bean methods with JPA annoations such as @Id.

示例:

@Entity
public class User
{

**@ID**
private int id;

public int getId(){
return this.id;
}

public void setId(int id){
this.id=id;
}

}

-----------或-----------

-----------OR-----------

@Entity
public class User
{


private int id;

**@ID**
public int getId(){
return this.id;
}

public void setId(int id){
this.id=id;
}

}

是的,我相信您想搜索字段与属性访问:

Yes, I believe you want to search on field versus property access:

Hibernate Annotations - 哪个更好,字段或属性访问?

Spring 偏好是字段访问.这就是我所遵循的.

The Spring preference is field access. That's what I follow.