Hi,
i create a bo1 object and a buch of bo2 children from a transport object in one step (code below).
If i don't call Session.flush() before inserting the children, the insert of the children will fail due to a foreign key constraint in the database.
The code is simular to the example in the reference manual, the main difference is that parent and children are created in the same session.
The reason for the failure is in the nullifyTransientReferences() call in net.sf.hibernate.impl.SessionImpl, because the isUnsaved() method checks for e.existsInDatabase, which returns false, allthough the SQL insert call was made.
Code:
// create the parent first
Integer key= new Integer(pTO.getA());
BO1 bo1= new BO1();
bo1.setB(pTO.getB());
pSession.save(bo1, key);
// we must flush the session, otherwise the following inserts will fail
pSession.flush();
// create the children
List ds= pTO.getD();
for (int i = 0, n = ds.size(); i < n; i++)
{
String d = (String)ds.get(i);
BO2 bo2= new BO2();
bo2.setC(i);
bo2.setD(d);
bo1.addBO2(bo2);
}
The addBO2 method in the BO1 class is
Code:
public void addBO2(BO2 pBO2)
{
pBO2.setParent(this);
mBO2s.add(pBO2);
}
Bye,
J