In JPA, cascadeType is used to cascade the operations from the current entity
to the relationship entities. In a bi-directional case, A and B refers to
each other, the cascade may be set in both side. However, there is not
specific instruction on whether the cascade configuration needs to be in
both side of the relationship or not. During Kodo/JPA test, it seems that it
is required.

Here is a real case: Account One2Many Application and the relationship is
bi-directional

Application:
@ManyToOne
@JoinColumn(name="ACCOUNT_ID")
private Account account;
Account:
@OneToMany(mappedBy="account", cascade={CascadeType.ALL})
private Set<Application> applicationCollection = new HashSet<Application>();

In most of the case, we only save and update from Account, thus we only
define cascade form Account side. So far the PERSIST and DELETE operations
all worked fine.

Account account = new Account();
Application application = new Application();
Application.setAccount(account);
Account.getApplicationCollection().add(application).
EntityManager.persist(account); // Everything passed. Both Account and
Application are persisted.

BUT, if I have a detached Account (with detached Application inside) and I
want to merge the Account directly, the operation failed with the follow
error. The only way to fix it is to enable the cascade from Application to
Account as @ManyToOne(cascade={CascadeType.ALL})

I am afraid it is caused by Kodo/openJPA's implementation. Based on Kodo41
document, the merge operation creates a new A' object for each detached A
object. It could be the reason of the exception during MERGE. For example,
Entity A has a bi-directional reference to Entity B (A <=> B) and only A
cascade to B. Now during merge, new entities A' and B' are created. I do not
know how Kodo/openJPA handle the relationship, but it is possible that B'
has a reference to the original A. So when B' get flushed, a detached A is
now allowed. We have to enable cascade from B' => A ...

Can anyone confirm my thought? Below are the exception:

org.apache.openjpa.persistence.ArgumentException: Encountered new object
"com.psi.vida.platform.entity.pojo.Account-com.psi.vida.platform.entity.pojo.Account-1010960"
in persistent field "com.psi.vida.platform.entity.pojo.Application.account"
of managed object
"com.psi.vida.platform.entity.pojo.Application-com.psi.vida.platform.entity.pojo.Application-1010440"
during attach. However, this field does not allow cascade attach. You cannot
attach a reference to a new object without cascading.
FailedObject: [EMAIL PROTECTED]
at weblogic.rjvm.ResponseImpl.unmarshalReturn(ResponseImpl.java:195)

......
Caused by: <4|false|0.9.6-incubating>
org.apache.openjpa.persistence.ArgumentException: Encountered new object
"com.psi.vida.platform.entity.pojo.Account-com.psi.vida.platform.entity.pojo.Account-1010960"
in persistent field "com.psi.vida.platform.entity.pojo.Application.account"
of managed object
"com.psi.vida.platform.entity.pojo.Application-com.psi.vida.platform.entity.pojo.Application-1010440"
during attach. However, this field does not allow cascade attach. You cannot
attach a reference to a new object without cascading.
FailedObject: [EMAIL PROTECTED]
-- 
View this message in context: 
http://www.nabble.com/Kodo-JPA-4.1.2-Bi-directional-Cascade-question-tf3495106.html#a9762373
Sent from the open-jpa-dev mailing list archive at Nabble.com.

Reply via email to