DuplicateMappingException包含在向Student实体添加passportId时由多个逻辑列名引用



这段代码在启动时导致以下异常

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
}

问题1:org.hibernate.DuplicateMappingException的原因是什么?

问题2:为什么向passportId添加以下注释在学生实体中解决问题?

@Column(name = "passport_id", insertable = false, updatable = false)
private Integer passportId; // this resolves DuplicateMappingException

PS:我知道类似的问题已经问过了,但我不能理解这两个问题的答案从其他线程。

答案1

原因是同一个数据库列有两个可写映射,这是不允许的

回答2

将其中一个映射设置为只读可以解决这个问题,因为只有一个映射是可写的

最新更新