I am new to JPA and have some problems in understanding bidirectional relations.
I have the entities Job and Execution. 1 Job belongs to exactly 1 Execution and vice versa. So, I wanted to use the onetoone relation.
My code looks like following:
@Entity
public class Execution {
....
@OneToOne(cascade=CascadeType.ALL,optional=false)
private Job job;
....
}
@Entity
public class Job{
......
@OneToOne(mappedBy="job",optional=false)
private Execution execution;
.....
}
I set optional=false for both fields. As expected, hibernate throws an exception if I try to persist an Execution object which has a Null Job object.
But persisting a Job object with a Null Execution object is possible, although I set optional=false.
What am I doing wrong?
Try adding the following annotation.