Similar to @ManyToOne Referencing a composite key in JPA. This is with an older version of Spring Boot using Hibernate 5.6.
I have three entity classes:
@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;
}
When I try to create and save it
var a = new A("a");
var b = new B("b");
var ab = new AB(a,b);
abDao.saveAndFlush(ab);
I get the oddest error
Caused by: java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).
The insert statement it generated is correct in the logs
Hibernate: insert into ab (a_id, b_id) values (?, ?)
I ran through the debugger and it seems to add a third "field" to map when I am only expecting two, the first one appears to be the AB
type, followed by A
and then B
.