-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: OneToMany and MappedBy problem
PostPosted: Mon Apr 03, 2006 5:40 am 
Beginner
Beginner

Joined: Fri Oct 07, 2005 5:35 am
Posts: 38
Location: France
Hibernate version:3.1.2

I writte 2 classes:
Code:
@Entity
public class A extends IdentifiablePE{
 
  private List<B> bs;

  @OneToMany(cascade={CascadeType.ALL},mappedBy="a")
  public List<B> getBs() {return bs;}
  public void setBs(List<B> bs) {this.bs = bs;}
}

@Entity
public class B extends IdentifiablePE {

  private A a;

  @ManyToOne
  @JoinColumn(name = "a_id")
  public A getA() {return a;}
  public void setA(A a) {this.a = a;}
}


My problem is that when I add a B instance in A, nothing is persisted !!
Code:
a.getBs().add(b);


It works fine only if i writte:
Code:
b.set(a);



That's quite disapointing. Any idea what's going wrong ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 03, 2006 5:58 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
mappedBy means this side is not the owner side of the relationship ie does not trigger an assoication update in DB.
Whe updating the non managed side, you also need to update the owning side.
In hyou case I would recommend you to write a some addB() method that add the element in the collection and call b.setA() at the same time

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 03, 2006 5:59 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
BTW this is the way it works in plain java objects (forget about persistence), you ahev to sync both sides

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 03, 2006 8:12 am 
Beginner
Beginner

Joined: Fri Oct 07, 2005 5:35 am
Posts: 38
Location: France
OK, but if I writte:
Code:
a.getBs().add(new B())

and in another transaction
Code:
a.getBs().size()
=>0


My Collection is empty, it should contain the added element !


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 03, 2006 10:46 am 
Beginner
Beginner

Joined: Fri Oct 07, 2005 5:35 am
Posts: 38
Location: France
I I understant properly your answer in A I should have:
Code:
public List<B> getBs() {...}
public void addB(B b) {
  getBs().add(b);
  b.setA(this);
}


My problem is that I need getBs method in order be able to use size, insert ... and other methods of List.
But I must not use add method of List directly otherwise it is a bug.I must use addB method.

It is very bug prone. IS there another solution than implementing all the sizeB, insertB methods in A ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.