Hi Stefan - I think your A.SetB(B) method must call B.add(A)
From section 2.1.7 in JPA Specification - "Note that it is the
application that bears responsibility for maintaining the consistency of
runtime relationships---for example, for insuring that the "one" and the
"many" sides of a bidirectional relationship are consistent with one
another when the application updates the relationship at runtime."
- Paul
On 4/11/2009 1:56 PM, Stefan Zeller wrote:
Hello all,
I've got a problem with the update of a manyToOne field which is
mapped by another entity. I searched and tried out for the last week
and I didn't had success, so I ask here. BTW, I'm new to JPA so maybe
it's a conceptual problem.
My test case is fairly simple. I've got to entities A and B.
A has got a ManyToOne relationship to B.
B has got a OneToMany rel to A.
--- entity A ---
@Entity
public class A {
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "BID", referencedColumnName = "ID")
private B b;
//...
---
--- entity B ---
@Entity
public class B {
@OneToMany(mappedBy = "b", fetch = FetchType.EAGER, cascade =
CascadeType.ALL)
private Collection<A> as;
//...
---
What I want is the following: When I update the field 'b' of an A
(lets say a6.setB(b5)) and I call the b5.getAs(), I would like to get
a list including the a6. But the Collection 'as' of b5 hasn't been
updated.
I tried the following as assignment:
---
public String actionA6SetB5() {
a6.setB(b5);
dataManager.getEm().persist(a6);
return null;
}
I also tried:
---
public String actionA6SetB5() {
dataManager.getEm().getTransaction().begin();
a6.setB(b5);
dataManager.getEm().persist(a6);
dataManager.getEm().flush();
dataManager.getEm().getTransaction().commit();
return null;
}
How can I achieve that the OneToMany rel would be also updated?
Thanks and greetings,
Stefan
PS: I use openJPA 1.2.0 and new, 1.2.1