Hello,
What annotations should I use for parentKey field in Child object so when I add Child object to Parent it will be saved with the given parent id. Like this:
Code:
Parent p = session.load(...);
Child c = new Child();
c.name = "blah blah blah";
p.children.add(c);
session.save(p);
...
When this code is executed all objects are inserted into DB but child has null parentId. :( Can anyone help me? I am confused because of composite keys. Please!
All classes and tables were simplified to improve readability! :)
I have parent and child tables:
Code:
PARENT (
id,
name,
...
)
CHILD (
id,
parentId,
name,
...
)
Parent class looks like this (simplified version):
Code:
class Parent {
@Id
@GeneratedValue
@AttributeOverrides({
@AttributeOverride(name = "id", column = @Column(name = "id"))
})
PrimaryKey key;
...
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(cascade=javax.persistence.CascadeType.ALL, mappedBy = "parentId", targetEntity = Child.class)
List<Child> children;
}
Child object looks like this (simplified version):
Code:
class Child {
@Id
@GeneratedValue
@AttributeOverrides({
@AttributeOverride(name = "id", column = @Column(name = "id"))
})
PrimaryKey key;
@Column(name = "name)
String name;
...
@AttributeOverrides({
@AttributeOverride(name = "id", column = @Column(name = "parentId"))
})
PrimaryKey parentKey;
}