I have the following classes:
@Entity
@Table(name = "PARENT")
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "PARENT_ID", unique = true, nullable = false, insertable = true, updatable = true)
private Long parentId;
@OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER )
@JoinColumn(name="PARENT_ID", nullable = false)
private List<Child> children;
public Parent(List<Child> children) {
this.children = children;
}
..
... Getters\Setters
}
@Entity
@Table(name = "CHILD")
public class Child {
@Id
@Column(name = "TYPE", unique = false, nullable = false)
private String type;
@Column(name = "RANK", unique = false, nullable = false, insertable = true, updatable = false)
private String rank;
}
The 'PARENT_ID' is foreign key in "CHILD" table. so it makes "CHILD" table having 2 columns forming its PRIMARY_KEY.
I perform the following insert:
List<Child> children = new LinkedList<Child>();
children.add(new Child("1,2,3,4"));
children.add(new Child("1,2,3,4"));
Parent parent = new Parent(children);
session.save(parent);
If both tables are empty it creates the 'parent' and 'children' correctly by assigning PARENT_ID!, BUT if 'Child' entries are already exist in table it performs update instead of insert!
PAY ATTENTION that both 'child' have the same "TYPE" ("1,2,3,4"), but they should have different "PARENT_ID" though.
What am I missing here ????
Thank you !