休眠一对一,不存在具有给定标识符的行异常
2022-09-03 04:53:07
我需要两个实体之间的链接,所以我使用一对一
@Entity
@Table(name = "T_USER")
public class User implements Serializable {
@Id
@Column(name = "user_id")
private int userId;
@Column(name = "login")
private String login;
@OneToOne(optional = true)
@JoinColumn(name="login", referencedColumnName="person_id", nullable = true, insertable = false, updatable = false)
private Person person;
}
@Entity
@Table(name = "T_PERSON")
public class Person implements Serializable {
@Id
@Column(name = "person_id")
private String personId;
@Column(name = "pin")
private String pin;
}
如果表 T_USER 中没有特定 PERSON 的项目,则 user.getPerson 会引发异常:
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [packagename.com.entity.Person#scabriou]
但是,如果我在 db 中的 2 个表之间有引用,则 getter 可以工作!