You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by vld <kn...@yahoo.com> on 2010/05/23 17:36:23 UTC

Bidirectional OneToMany with Enchancer

Hi!

I'm using OpenJPA 1.2.2 with Enhancer default for eclipse environment. When
I try to persist objects with bidirectional OneToMany relationship, an error
occurs which shows that OneToMany List contains all the objects added to
collection, but none of the objects in collection has it's ManyToOne
relation set. My setter methods just add objects:
Parent:
  public void setChildren(List<Child> children) {
    this.children = children;
  }
Child:
  public void setParent(Parent parent) {
    this.parent = parent;
  }

Should this be enough, or do I have to do something more? Some of the
examples I found do only this kind of setting values.
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Bidirectional-OneToMany-with-Enchancer-tp5091015p5091015.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Bidirectional OneToMany with Enchancer

Posted by Pinaki Poddar <pp...@apache.org>.
Originally, you wrote 
> When I try to persist objects with bidirectional OneToMany relationship, 

I interpreted 'persist' as creating new Parent, new Children ... and in such
case, I do not know of any technique  that will automatically set the
inverse side of a bi-directional link. But I may not have understood your
question properly. 

Update on a bi-directional relation will depend on which is the 'owning'
side and from which side of the relation is updating ... it might help to
analyze further if you bring what you expect to happen and what appears to
you as doubtful behavior in terms of a small test case...

-----
Pinaki 
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Bidirectional-OneToMany-with-Enchancer-tp5091015p5105429.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Bidirectional OneToMany with Enchancer

Posted by vld <kn...@yahoo.com>.
Thank you for your reply.

Some of the examples I found never mentioned the need for manually setting
the relationship. Also, I have some ManyToMany relationships which are
automatically treated during persist and merge operations (well, they are
deleted and inserted instead of updated), and it lead me to conclusion it
might be handled without intervention.

There is one more thing that confuses me. If setting relationship is done in
domain classes, as in example:


> Parent.java
> public void setChildren(Set<Children> children) {
>    for (Child c : children) c.setParent(this);
>    this.children = children;
> }
> 
fetching Parent from DB with it's Children collection makes UPDATE of
Children executed. Is there anything I can do to avoid it?
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Bidirectional-OneToMany-with-Enchancer-tp5091015p5100592.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Bidirectional OneToMany with Enchancer

Posted by Pinaki Poddar <pp...@apache.org>.
The application will need to set the bi-directional relation. One can do that
consistently within the domain class itself or outside

Inside the domain

Parent.java
public void setChildren(Set<Children> children) {
   for (Child c : children) c.setParent(this);
   this.children = children;
}


Or outside:

Hospital.java // a non-persistent class

void haveBabies(Parent p, Set<Children> children) {
   p.setChildren(children);
   for (Child c : children) c.setparent(p);
}

-----
Pinaki 
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Bidirectional-OneToMany-with-Enchancer-tp5091015p5098927.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.