I am using hibernate core 3.2.6ga with hibernate annotations 3.2.1ga.
I have an object model with ~20 classes using 1:n bidirectional relations.
The model is ERM model is not "tree like", however has one root object.
Unlike tree, parent's grandchildren can have relation with 2 parent's children.
All relations are annotated like that:
Code:
@Entity
@Table (name="stack")
public class Stack
{
private Section section;
private JobColor color;
// some other properties
Stack()
{
}
@ManyToOne(optional=false) @Cascade(CascadeType.SAVE_UPDATE)
@JoinColumn (name="section_id", nullable=false)
public Section getSection()
{
return this.section;
}
void setSection(Section section)
{
this.section = section;
}
@ManyToOne(optional=false) @Cascade(CascadeType.SAVE_UPDATE)
@JoinColumn (name="job_color_id", nullable=false)
public JobColor getColor()
{
return color;
}
void setColor(JobColor color)
{
this.color = color;
}
}
@Entity
@Table (name="section")
public class Section
{
private Marker marker;
private List<Stack> stacksList = new ArrayList<Stack>();
// some other properties
Section()
{
}
@ManyToOne @Cascade(CascadeType.SAVE_UPDATE)
@JoinColumn (name="marker_id")
public Marker getMarker()
{
return this.marker;
}
protected void setMarker(Marker marker)
{
this.marker = marker;
}
@OneToMany(mappedBy="section") @Cascade(CascadeType.ALL)
List<Stack> getStackList()
{
return stacksList;
}
void setStackList(List<Stack> stacksList)
{
this.stacksList = stacksList;
}
}
Problem is: when i create a complex object and call session.saveOrUpdate(parentObject),
i get error "not-null property references a null or transient value: Stack.section".
From cascade log i see SAVE_UPDATE event was properly cascaded to ALL objects.
However, my class Stack is going to be saved before Section.
This is bad, because Stack has
Code:
@JoinColumn (name="section_id", nullable=false)
My question is: can i expect hibernate will find proper order of insertions in a any complex graph "automatically" or i should ensure it "manually" under certain circumstances?