-->
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.  [ 2 posts ] 
Author Message
 Post subject: @OneToOne bidirectional reference problem
PostPosted: Tue Oct 28, 2008 9:23 pm 
Newbie

Joined: Tue Oct 28, 2008 8:51 pm
Posts: 2
Hi,

I am trying to create a OneToOne as below:

Code:
public Student {

  @OneToOne(cascade={CascadeType.ALL},fetch=FetchType.LAZY,optional=true)
  private Account account;
  :
}


Code:
public Account {

  @OneToOne(mappedBy="account",fetch=FetchType.EAGER)
  private Student belongsTo;
  :
}


When I retrieve a Student it has a reference to Account but the Account does not have a reference to Student. Any ideas?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 02, 2008 7:22 am 
Newbie

Joined: Sat Oct 18, 2008 11:34 am
Posts: 10
When creating an Account, you have to manually set the back-reference to the Student, cause it is not being set automatically. When retrieving an Account from the database, the Hibernate will load the corresponding parent, i.e. Student.

However, you won't see the belongsTo field in the Account table, because it is regulated on the Student side by a foreign key definition.

Here's how your example should look like (I am using field reference for the sake of the simplicity):

Code:
@Entity
class Student {
   @Id
   @GeneratedValue
   public Long id;
   
   @OneToOne(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, optional = true)
   public Account account;
}

@Entity
class Account {
   @Id
   @GeneratedValue
   public Long id;

   @OneToOne(mappedBy = "account", fetch = FetchType.EAGER)
   public Student belongsTo;
   
   public Account() {
   }
   public Account(Student belongsTo) {
      this.belongsTo = belongsTo;
   }
}

public class StudentTest {
   public static void main(String[] args) {
      EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");

      EntityManager em = emf.createEntityManager();
      EntityTransaction tx = em.getTransaction();
      
      tx.begin();
      Student s = new Student();
      s.account = new Account(s);
      em.persist(s);
      tx.commit();

      em.clear();
      
      Long id = s.id;
      Account ac = em.find(Account.class, id);
      System.out.println(ac + " belongs to "+ ac.belongsTo);

      em.close();
   }
}


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