I'm obviously not understanding something about how this works. Hopefully I can demonstrate my confusion consicely so that someone can elucidate:
I have two classes as follows:
Code:
@Entity
public class GenOne {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
long id;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "genone")
Set<GenTwo> gentwos = new HashSet<GenTwo>();
}
@Entity
public class GenTwo {
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
private long id;
private long genone;
}
I try to create a GenOne and put some GenTwos into it, thus:
Code:
GenOne g1 = new GenOne();
for (int i = 0; i < 3; i++) g1.gentwos.add(new GenTwo());
session.save(g1);
On the save I get an exception, because when trying to insert a record for the first
GenTwo, Hibernate sets its foreign key (
genone) to zero, which fails the foreign-key constraint. I would have expected Hibernate to first insert the
GenOne, get its
id and then use that to fill the
genone values in the
GenTwo records. What am I doing wrong?
TIA
Michael