-->
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.  [ 3 posts ] 
Author Message
 Post subject: annotations: bidirectional OneToMany fk
PostPosted: Tue Aug 14, 2007 3:32 pm 
Newbie

Joined: Tue Aug 14, 2007 3:16 pm
Posts: 4
have simple pojos
Code:
public class A {
   protected int aId;
   private List<B> bs = new ArrayList();
   
   @Id
   @GeneratedValue
   @Column(name="a_id")
   public int getAId() {
      return aId;
   }
   public void setAId(int aId) {
      this.aId = aId;
   }
   @OneToMany(mappedBy="a", cascade = CascadeType.ALL)
   public List<B> getBs() {
      return bs;   }
   public void setBs(List<B> bs) {
      this.bs = bs;
   }
}
public class B{
        protected int bId;
   protected A a;
   @Id
   @GeneratedValue
   @Column(name="pk_b_id")
   public int getBId() {
      return bId;
   }
   public void setBId(int bId) {
      this.bId = bId;
   }
   
   @ManyToOne
        @JoinColumn(name="fk_a_id")
   public User getA() {
      return a;
   }
   public void setA(A a) {
      this.a = a;
   }
}


when i do a session.save(); it inserts a and b but does not put the fk_a_id into b's database.
Anyideas on how to get it to persist this value


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 14, 2007 11:09 pm 
Newbie

Joined: Sat Oct 28, 2006 6:16 am
Posts: 17
How are you establishing the association between a and b? I expect the problem is because you are using something like the following code to establish the relationship between a and b.

Code:
A a = new A();
B b = new B();
a.getBs().add(a);
session.save(a);


It is fair enough the you expect this to work but it doesn't. It doesn't work with your setup because when saving or updating hibernate determines whether an assocation exists between a and b by looking at b.getA() not a.getBs(). So in the code above b.getA() will return null, hence hibernate thinks there is no relationship between a and b. (you can read more about why this is the case here: http://www.hibernate.org/hib_docs/v3/reference/en/html/collections.html#collections-bidirectional)

There are two ways to fix this.

1.

Read section "2.2.5.3.2.1. Bidirectional" of http://www.hibernate.org/hib_docs/annotations/reference/en/html/entity.html#entity-mapping-association-collections and figure out how to make hibernate use the one-to-many side to drive the assocation instead of the many-to-one side.

2.

Change your code ...

Code:
@ManyToOne
        @JoinColumn(name="fk_a_id")
   public User getA() {
      return a;
   }


... to:

Code:
@ManyToOne (cascade = CascadeType.ALL)
        @JoinColumn(name="fk_a_id")
   public User getA() {
      return a;
   }

)

Then use something like the following code to establish the relationship between a and b

Code:
A a = new A();
B b = new B();
b.setA(a);
session.save(b);


I hope that helps.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 16, 2007 4:36 pm 
Newbie

Joined: Tue Aug 14, 2007 3:16 pm
Posts: 4
In context
a.getBs().add(b);
makes more sense so I ended up with

Code:
public class A {
   protected int aId;
   private List<B> bs = new ArrayList();
   
   @Id
   @GeneratedValue
   @Column(name="a_id")
   public int getAId() {
      return aId;
   }
   public void setAId(int aId) {
      this.aId = aId;
   }
   @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
   @JoinColumn (name="fk_a_id")
   public List<B> getBs() {
      return bs;   }
   public void setBs(List<B> bs) {
      this.bs = bs;
   }
}
public class B{
        protected int bId;
   protected A a;
   @Id
   @GeneratedValue
   @Column(name="pk_b_id")
   public int getBId() {
      return bId;
   }
   public void setBId(int bId) {
      this.bId = bId;
   }
   
   @ManyToOne
   @JoinColumn(name="fk_user_id", updatable = false, insertable = true)
   public User getA() {
      return a;
   }
   public void setA(A a) {
      this.a = a;
   }
}


I didn't see the post before coming to this and plan on reading those articles but just posting for advice on if I am missing something in my testing that this will fail. I know that inserting b will not update the list in a but am ok with that and plan on taking b's getA and setB away

Thank you for the advise


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.