I am trying to create a bidirectional one-to-many association between two entities where the many side has a compound key. And one of the keys of the many side is coming from the one side. Also, I need to have the many side the owner of the association. Below is an example code showing what my code looks like.
Child Class which is the many side. Its primary key is "name" and "parent". "parent" is coming from the association.
Code:
public class child{
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name="parent", column=@Column(name="PARENT", nullable=false)),
@AttributeOverride(name="name", column=@Column(name="NAME", nullable=false))})
private ChildId id;
@MapsId("pool")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="PARENT", nullable = false, updatable = false, insertable = false)
private Parent parent;
}
ChildId is the Embedded id.
Code:
@Embeddable
public class childId{
@Column(name = "PARENT")
private String parent;
@Column(name = "NAME")
private String name;
}
Parent Class which is the one side.
I need to have this side owner of the association.
Code:
public class parent{
@Id
@Column(name = "NAME")
private String name;
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name="id", nullable = false)
private Set<Child> childs;
Question1:When I run this code, I got the following exception:
Code:
Hibernate: update CHILD set id=? where NAME=? and PARENT=?
2015-11-10 15:36:16,960 66657 [qtp797818094-25] DEBUG o.s.j.s.SQLErrorCodeSQLExceptionTranslator - Translating SQLException with SQL state '42703', error code '-206', message [DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703, SQLERRMC=ID, DRIVER=3.61.86]; SQL was [] for task [DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703, SQLERRMC=ID, DRIVER=3.61.86]
2015-11-10 15:36:16,972 66669 [qtp797818094-25] DEBUG o.h.e.jdbc.spi.SqlExceptionHelper - could not execute statement [n/a]
msjava.dbpool.DBPoolSQLException: DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703, SQLERRMC=ID, DRIVER=3.61.86 (DataSource: dataSource, Type: DB2)
As it shows, it is trying to update the child and not insert! how can I solve it?