Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
实体类中的Hibernate注释放置_Hibernate_Annotations - Fatal编程技术网

实体类中的Hibernate注释放置

实体类中的Hibernate注释放置,hibernate,annotations,Hibernate,Annotations,我有以下实体类。我可以将注释放在成员变量声明的顶部或getter和setter的顶部 当我将注释放在成员变量声明附近时,它将保存该变量的值(不是getter返回的值),如果我将注释放在getter附近,它将保存getter返回的值(不是变量的值)。这一切都很好 我的问题是,在持久化过程中,当我们在成员变量附近放置注释时,hibernate如何能够访问成员变量值(尽管它被声明为私有) @Entity @Table(name="USERS") public class Users { @

我有以下实体类。我可以将注释放在成员变量声明的顶部或getter和setter的顶部

当我将注释放在成员变量声明附近时,它将保存该变量的值(不是getter返回的值),如果我将注释放在getter附近,它将保存getter返回的值(不是变量的值)。这一切都很好

我的问题是,在持久化过程中,当我们在成员变量附近放置注释时,hibernate如何能够访问成员变量值(尽管它被声明为私有)

 @Entity
 @Table(name="USERS")
 public class Users {
   @Id @GeneratedValue(strategy=GenerationType.AUTO) 
   private int userId;

   //This will save only value of userName no mater what getter returns
   @Column(name="user_name")
   private String userName;

    public int getUserId() {
      return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    //Putting @Column(name="user_name") here will save value  
    // "userName from getter" to the DB

    public String getUserName() {
          return userName + " from getter";
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
 }

Hibernate和其他JPA提供程序用于私有成员访问。在Java中,与许多OO语言一样,可见性声明的执行并不严格,因此不能绕过它们,因为它使用反射。有关更多信息,请参阅本文: