Craig L Russell wrote:
>
> Hi,
>
> I can't tell from your description what might be going wrong, but
> there is one clue. You ask if all entities should be persisted in one
> transaction. Not necessarily. But if you have several transactions,
> it's possible that you are using detached instances and the problem
> might be that if you assign an instance to a field of a detached
> instance and then commit the transaction, nothing happens!
>
> You need to make sure you merge all detached instances or nothing is
> supposed to happen.
>
> Can you attach a test case?
>
> Craig
>
>
Thanks for your reply!
here is a simple version of what we are doing. we are creating all new
objects and then persist them in one transaction by a session bean.
occasionally A&C got stored without B. if we re run the process all A, B
and C will be saved. what might be wrong?
@Entity
public class ClassA {
//fields definition
@OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER, mappedBy
= "classA")
private Collection<ClassB> classBs = new HashSet<ClassB>();
@OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER, mappedBy
= "classA)
private Collection<classC> classCs = new HashSet<ClassC>();
}
@Entity
public class ClassB{
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "class_a_id")
private ClassA classA;
//fields defintions
}
@Entity
public class ClassC{
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "class_a_id")
private ClassA classA;
//fields defintions
}
//by default all the methods in this session bean has required tx attribute
@Stateless
public class SessionBean{
@PersistenceContext(unitName = "test")
private EntityManager em;
public buildA{
ClassA a = new ClassA();
ClassB b = new ClassB();
Collection<ClassB> classBs = new ArrayList<ClassB>();
b.setClassA(a);
classBs.add(a);
a.setClassBs(classBs);
ClassC c = new ClassC();
Collection<ClassC> classCs = new ArrayList<ClassC>();
c.setClassA(a);
classCs.add(a);
a.setClassCs(classCs);
em.persist(a);
}
}
--
View this message in context: http://www.nabble.com/Casade-Persist-tf4946103.html#a14161450
Sent from the OpenJPA Users mailing list archive at Nabble.com.
|