类似于@ManyToOne Referencing a composite key in JPA. 这是使用Hibernate 5.6的旧版本Spring Boot.
我有三个实体类:
@Entity class A { Long getId(); ... }
@Entity class B { Long getId(); ... }
@Entity
@IdClass(ABPK.class)
@Data
class AB {
@Id
@Column(name="a_id", insertable=false, updatable=false)
private Long aId;
@Id
@Column(name="b_id", insertable=false, updatable=false)
private Long bId;
@ManyToOne
@JoinColumn(name="a_id")
private A a;
@ManyToOne
@JoinColumn(name="b_id")
private B b;
public AB() { }
public AB(A a, B b) {
this.a = a;
this.aId = a.getId();
this.b = b;
this.bId = b.getId();
}
}
@Data
class ABPK implements Serializable {
Long aId;
Long bId;
}
当我try 创建和保存它时
var a = new A("a");
var b = new B("b");
var ab = new AB(a,b);
abDao.saveAndFlush(ab);
我得到了最奇怪的错误
原因:java.xsl.SQLExcycle:参数索引超出范围
它生成的插入声明在日志(log)中是正确的
Hibernate: insert into ab (a_id, b_id) values (?, ?)
我运行了调试器,当我只期待两个时,它似乎添加了第三个"字段"来映射,第一个似乎是AB
类型,然后是A
,然后是B
.