I have a peice of code such as below that runs in a SLSB. I have setup the hibernate properties
to use the session/transaction of the container (weblogic)
//SLSB EJB method
Test1 persistTest1 (Test1 t1) {
if (t1.getId() == null) { //if its a "new" Test1 ----------------------------------(4)
so reset all attribute ids to null...
for (Attrib fa : t1.getAttributes()) {
fa.setId(null);
}
HibernateUtil.getCurrentSession().save(t1);
return t1;
}
}
where "id" is the object-id of Test1
This tries to save a 1:n relationship between Test1 and its Attribs that are declared
as cascade=all,delete-orphan in Test1
Now in the client, I am doing the following
.... create Test1 t1 and add some attributes.. Then,
Test1 t1 = persistTest1(t1);--------------(1)
t1.setId(null); --------------(2)
t1 = persistTest1(t1);-------------(3)
The reason for (2) is so that I can treat t1 as a brand new Test1 (like a "Save As" functionality)
However I get a d/b rollback and an exception in the backend when (3) gets executed.
In the backend when I "replace" the collection as generated by Hibernate in (1)
with a java collection as shown below, everything runs fine.
Test1 persistTest1 (Test1 t1) {
if (t1.getId() == null) { ----------------------------------(4)
//treat it as a new t1, so reset all attribute ids as well...
for (Attrib fa : t1.getAttributes()) {
fa.setId(null);
}
------------>
List<Attrib> l = new ArrayList<Attrib>();
l.addAll(t1.getAttributes());
t1.setAttributes(l);
------------>
HibernateUtil.getCurrentSession().save(t1);
return t1;
}
}
Any clues ?
Thanks in advance
|