-->
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.  [ 4 posts ] 
Author Message
 Post subject: bidirectional one-to-many
PostPosted: Sat Apr 08, 2006 11:57 am 
Beginner
Beginner

Joined: Tue May 11, 2004 10:40 am
Posts: 37
Location: Belgium
I'm unable to set up a bidirectional one-to-many relationship.

Code:
public class Person() {

...

   @OneToMany(mappedBy="person")
   public Set<Invoice> getInvoices() {
      return invoices;
   }
}


Code:
public class Invoice() {

...

        @ManyToOne
        @JoinColumn(name="fk")
   public Person getPerson() {
      return person;
   }
}


I use this method to save the person object. (eg: saveOrUpdate(new Person(...));
Code:
   protected void saveOrUpdate(Object obj) {
      try {
         startOperation();
         session.saveOrUpdate(obj);
         tx.commit();
      } catch (HibernateException e) {
         HibernateFactory.rollback(tx);   
         handleException(e);
      } finally {
         HibernateFactory.close(session);
      }
   }


I tried many variants on this, but I always get a unidirectional relationship instead of a bidirectional relationship.

Here are some examples of variants I tried, but without result.
Code:
@ManyToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="fk", insertable=false, updatable=false)

...

@OneToMany (targetEntity=Invoice.class, mappedBy="invoice", cascade=CascadeType.ALL)

...


Why can I never get a bidirectional relationship???


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 08, 2006 4:22 pm 
Newbie

Joined: Tue Mar 28, 2006 4:03 pm
Posts: 11
I don't understand your question.
What exactly makes you believe that you
Quote:
get a unidirectional relationship instead of a bidirectional relationship

?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 09, 2006 4:23 am 
Beginner
Beginner

Joined: Tue May 11, 2004 10:40 am
Posts: 37
Location: Belgium
First of all, thanks for your reply...

Simply because one time, I have an entry in the person-invoice table but no entry in the column fk of the invoice table. And the other time I get the opposite.

Also when I retrieve an object back from the database (an invoice or a person object), one time I can do invoice.getPerson() but not person.getInvoices(), and the other time I can do person.getInvoices() but not invoice.getPerson().
(with 'I can do' I mean that I can get the right object back out of the database, the other times I get nothing (null))


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 16, 2006 1:13 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you need to check in the documentation what mappedBy (or inverse) means

_________________
Emmanuel


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