在带有 Hibernate 的 Spring Boot 中,我有几个表,其中所有表都具有相同的主键组合,即 2 列、id 和 active_flag 的复合键(每个表都有自己的主键列集)。其中一个表有另一个表的外键,其中外键也是复合键(指引用表的 id 和 active_flag 列)。
在我的实体映射中,我收到错误
Repeated column in mapping for entity: B column: active_flag (should be mapped with insert="false" update="false")
。我相信错误是因为如下所示,active_flag
映射到我的BaseEntity
类中,并且在我的B
类中我也将它与@JoinColumn
注释一起使用。它是否正确?如果是这样,有没有一种方法可以使其工作而不必从 BaseEntity
类中删除列定义,因为有更多的类从 BaseEntity 扩展并使用相同的 @IdClass
注释。
此外,根据错误消息建议,我尝试将
insertable
和 updateable
属性添加到 @JoinColumn
注释中,但随后我收到另一个错误,Mixing updatable and non updatable columns in a property is not allowed: B.aInstance
和 Mixing insertable and non insertable columns in a property is not allowed: B.aInstance
。
以下是我的班级结构:
基础实体:
@MappedSuperclass
public abstract class BaseEntity {
@Id
@Column(name = "active_flag", nullable = false)
private Boolean activeFlag;
...
}
实体ID:
public class EntityId implements Serializable {
private Integer id;
private Boolean activeFlag;
}
答:
@Entity
@Table(name = "t_a")
@IdClass(EntityId.class)
public class A extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Integer id;
@OneToMany(mappedBy = "aInstance")
private List<B> bInstanceList;
...
}
乙:
@Entity
@Table(name = "t_b")
@IdClass(EntityId.class)
public class B extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Integer id;
@ManyToOne
@JoinColumn(name = "a_id", nullable = false)
@JoinColumn(name = "active_flag", nullable = false)
private A aInstance;
...
}
编辑:我尝试从
active_flag
类中删除 BaseEntity
列并将其放置在每个子类中,因此在这种情况下,B 类如下所示:
乙:
@Entity
@Table(name = "t_b")
@IdClass(EntityId.class)
public class B extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Integer id;
@Id
@Column(name = "active_flag", nullable=false)
private Boolean activeFlag;
@ManyToOne
@JoinColumn(name = "a_id", nullable = false)
@JoinColumn(name = "active_flag", nullable = false)
private A aInstance;
...
}
但是仍然报错
Repeated column in mapping for entity: B column: active_flag (should be mapped with insert="false" update="false")
。