我有3张桌子: A (id,type,b_id), B1 (id,message)和 B2 (id,message)。 表记录应与 B1 或 B2 表绑定,这取决于类型字段。如果type为1,则绑定 B1 ,否则 - B2 。我怎么能用hibernate做到这一点?我试图做以下事情:
@Entity
@Table(name = "ps.A")
public class A{
@Id
@Column(name = "ID")
private int index;
@Column(name = "type")
private int type;
@ManyToOne
@JoinColumn(name = "b_id")
private B1 b1;
@ManyToOne
@JoinColumn(name = "b_id")
private B2 b2;
如果类型等于1或2并且返回null或object,我可以检查get方法中的类型。但控制台说:
实体映射中的重复列:列:b_id(应该映射 with insert =" false"更新="假&#34)
答案 0 :(得分:1)
将注释更改为@JoinColumn(name =" b_id",updatable = false,insertable = false)
更新:澄清。我会像这样改变实体
@Entity
@Table(name = "ps.A")
public class A{
@Id
@Column(name = "ID")
private int index;
@Column(name = "type")
private int type;
@ManyToOne
@JoinColumn(name = "b_id", updatable = false, insertable = false)
private B1 b1;
@ManyToOne
@JoinColumn(name = "b_id", updatable = false, insertable = false)
private B2 b2;
@JoinColumn(name = "b_id")
private Integer bId;
因此,您可以使用纯整数bId(加上类型)进行创建。保存后,应填写b1和b2。