I'm mapping a legacy read-only database.
I have two tables A and B
- A with a multiple column primary key (ID and ID2)
- B with a multiple column primary key (ID and ID3)
A row in A can reference several rows in B (B.ID = A.ID) and a row in B can be referenced by several rows in A.
It is basically a many-to-many relationship without a join table. Since I just have to read the tables, I tried with two one-to-many relationships in both classes.
The additional problem that I have is that both IDs used for the join are not the primary key.
I have the following classes:
Code:
@Entity
@Table( name = "A" )
@IdClass( PrimaryKeysA.class )
public class A {
@Id
@Column( name = "ID", insertable = false, updatable = false, columnDefinition = "char" )
private String id;
@Id
@Column( name = "ID2", insertable = false, updatable = false )
private int id2;
@OneToMany( cascade = CascadeType.ALL )
@JoinColumn( name = "ID", columnDefinition = "char", referencedColumnName = "ID" )
private Set< B > setOfBs;
}
@Entity
@Table( name = "B" )
@IdClass( PrimaryKeysB.class )
public class B {
@Id
@Column( name = "ID", insertable = false, updatable = false, columnDefinition = "char" )
private String id;
@Id
@Column( name = "ID3", insertable = false, updatable = false )
private int id3;
@OneToMany( cascade = CascadeType.ALL )
@JoinColumn( name = "ID", columnDefinition = "char", referencedColumnName = "ID" )
private Set< A > setOfAs;
}
Hibernate generates the following error:
Code:
Exception while preparing the app : referencedColumnNames(ID) of package.B referencing package.A not mapped to a single property
I agree with the message: B.ID is referencing only a part of the primary key of A. But why is this a problem?