-->
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.  [ 1 post ] 
Author Message
 Post subject: Question about @OnetoOne
PostPosted: Mon Jan 31, 2011 11:31 pm 
Newbie

Joined: Mon Jan 31, 2011 11:22 pm
Posts: 6
In the book it says when you declare a @OnetoOne mapping Hibernate will enforce the multiplicity with the unique constraint.

I wrote a sample program
Code:
@Entity
@Table(name = "STUDENT")
public class Student {

   @Id
   @GeneratedValue
   @Column(name = "STUDENT_ID")
   private long studentId;
   
   @OneToOne(cascade = CascadeType.ALL)
   @JoinColumn(name="Student_Address_FK")
   private Address studentAddress;
...
}

Code:
@Entity
@Table(name = "ADDRESS")
public class Address {
   @Id
   @GeneratedValue
   @Column(name = "ADDRESS_ID")
   private long addressId;
   @OneToOne(mappedBy="studentAddress")
   private Student student;


Code:
   public static void main(String args[]) {
      
      EntityTransaction tx = em.getTransaction();
      tx.begin();

      Address address1 = new Address();
      Student student1 = new Student("Eswar", address1);
      Student student2 = new Student("Joe", address1);

      em.persist(student2);
      em.persist(student1);

      
      tx.commit();
      em.close();

      
   }




Then I check the database and in the student table both of the rows have the same Address_FK. Correct me if I am wrong, but in this case hibernate does not enforce uniqueness (1-to-1).


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

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.