【问题标题】:Hibernate - why put a column name before getter instead of variable?Hibernate - 为什么在 getter 而不是变量之前放一个列名?
【发布时间】:2023-04-02 03:57:01
【问题描述】:

如果我们创建一个对应于用户 id 的列 Id,那么为什么不把它放在变量 id 附近而不是 id 的 getter 附近呢?我在这里得到了答案 - Where to put hibernate annotations?

但是,因为我的书把它放在 getter 附近,看起来 某些类 需要通过 getter 访问这个对象以将其序列化/持久化到数据库中。这个类是什么,它是如何执行持久性的?我是否调用它的方法来做持久性?

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class User {
  private Long id;
  private String password;

  @Id
  @GeneratedValue
  public Long getId() {
    return id;
  }

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

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }
}

【问题讨论】:

    标签: java hibernate jpa


    【解决方案1】:

    Hibernate 将使用反射在您的类上找到要读取的适当方法或字段。这就是 Hibernate 能够读取私有字段的原因。

    session.save(Object object) 调用执行此反射的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 2010-09-27
      • 1970-01-01
      • 2011-06-11
      • 1970-01-01
      • 2018-09-29
      • 2020-08-04
      相关资源
      最近更新 更多