Mapping of Composite Keys
I have two tables with the below definition
CREATE TABLE VYA6CPP (
A6VN9K VARCHAR(9),
A6VN8K VARCHAR(8),
A6J2CD VARCHAR(20),
PRIMARY KEY(A6VN9K, A6VN8K)
)
CREATE TABLE MAMAM1 (
M1VIN1 VARCHAR(9),
M1VIN2 VARCHAR(8),
M1SHPR VARCHAR(4),
PRIMARY KEY(M1VIN1, M1VIN2),
FOREIGN KEY(M1VIN1, M1VIN2) REFERENCES VYA6CPP(A6VN9K, A6VN8K)
)
Below are the entity definition.
Code:
@Entity
@Table(name="MAMAM1")
public class Mamam1 implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private Mamam1PK id;
@Column(name="M1SHPR")
private String m1shpr;
//bi-directional one-to-one association to Vya6cpp
@OneToOne
@JoinColumns({
@JoinColumn(name="M1VIN1", referencedColumnName="A6VN9K"),
@JoinColumn(name="M1VIN2", referencedColumnName="A6VN8K"),
})
private Vya6cpp vya6cpp;
}
Code:
@Embeddable
public class Mamam1PK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
@Column(name="M1VIN1")
private String m1vin1;
@Column(name="M1VIN2")
private String m1vin2;
}
Code:
@Entity
@Table(name="VYA6CPP")
public class Vya6cpp implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private Vya6cppPK id;
@Column(name="A6J2CD")
private String a6j2cd;
//bi-directional one-to-one association to Mamam1
@OneToOne(mappedBy="vya6cpp")
private Mamam1 mamam1;
}
Code:
@Embeddable
public class Vya6cppPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
@Column(name="A6VN9K")
private String a6vn9k;
@Column(name="A6VN8K")
private String a6vn8k;
}
If the query <Select c from Mamam1 c> is executed, i get the below exception.
Hibernate: select mamam1x0_.M1VIN1 as M1_0_, mamam1x0_.M1VIN2 as M2_0_, mamam1x0_.M1SHPR as M3_0_ from MAMAM1 mamam1x0_ fetch first 10 rows only
java.lang.IllegalArgumentException: org.hibernate.TypeMismatchException: Provided id of the wrong type for class Vya6cpp. Expected: class Vya6cppPK, got class Mamam1PK
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:247)
at Main.main(Main.java:26)
Caused by: org.hibernate.TypeMismatchException: Provided id of the wrong type for class Vya6cpp. Expected: class Vya6cppPK, got class Mamam1PK
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:135)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1080)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1028)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:623)
at org.hibernate.type.EntityType.resolve(EntityType.java:431)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:140)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:898)
at org.hibernate.loader.Loader.doQuery(Loader.java:773)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.doList(Loader.java:2449)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2192)
at org.hibernate.loader.Loader.list(Loader.java:2187)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:452)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:241)
... 1 more