此代碼在啟動時導致以下例外
Caused by: org.hibernate.DuplicateMappingException: Table [student] contains physical column name [passport_id] referred to by multiple logical column names: [passport_id], [passportId]
我正在使用 H2 記憶體資料庫。
學生單位:
@Entity
public class Student {
@Id
@GeneratedValue
private Integer id;
@Column(nullable = false, length = 250)
private String name;
private Integer passportId; // adding this will cause DuplicateMappingException
@OneToOne
private Passport passport;
// getters and setters omitted for brevity
}
護照物體:
@Entity
public class Passport {
@Id
@GeneratedValue
private Integer id;
@Column(nullable = false, length = 250)
private String number;
// getters and setters omitted for brevity
}
問題一: org.hibernate.DuplicateMappingException 的原因是什么?
問題2:為什么在Student物體中給passportId添加如下注解可以解決問題?
@Column(name = "passport_id", insertable = false, updatable = false)
private Integer passportId; // this resolves DuplicateMappingException
PS:我知道之前有人問過類似的問題,但我無法從其他執行緒中理解這兩個問題的答案。
uj5u.com熱心網友回復:
答案 1
原因是同一個資料庫列有兩個可寫映射,這是不允許的
答案 2
將其中一個映射設為只讀可以解決問題,因為這樣只有一個映射是可寫的
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/498175.html
