-->
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: very simple one to one question
PostPosted: Wed Aug 13, 2008 11:32 am 
Newbie

Joined: Wed Aug 13, 2008 11:22 am
Posts: 4
Hey people

this is a very simple question... and should be fairly easy answered... but unfortunately after 3 days of googling I give up...

so here the scenario:

Table1: Person (id, name) pk is id and is attached to a sequence
Table2: Person_address (id, street) pk is id and a foreign key to person(id)

so here my two classes:

PersonAddress

Code:
@Entity
@Table(name = "person_address")
public class PersonAddress implements Serializable {

      private static final long serialVersionUID = 1L;

      @Id
      @Column(name="id")
      private int id;

      @Column(name = "street")
      private String street;

    // ... getters and setters

}



Person:

Code:
@Entity
@Table(name="person")
public class Person implements Serializable{

      private static final long serialVersionUID = 1L;

      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "person_seq")
      @SequenceGenerator(name = "person_seq", sequenceName = "person_seq", allocationSize = 1)
      @Column(name="person_id")
      private int id;

      @Column(name="name", length=40)
      private String name;

      @OneToOne(cascade=CascadeType.ALL)
      @PrimaryKeyJoinColumn
      private PersonAddress address;

      public Person(){
           address = new PersonAddress();
     }
      // ... getters and setters


so here is my very simple test scenario that just creates a new person and sets a new address and then saves it

Person p = new Person();
p.setName("mike");

PersonAddress ad = new PersonAddress();
ad.setStreet("street name 1");
p.setAddress(ad);

session.persist(p);

As result i am expecting the following:

person table:
id | name |
1 | mike |

person_address
Id | street |
1 | street name 1|

but the result for the person_address table is:

Id | street |
0 | street name 1|

and i dont understand why .... its aweful... its shouldnt be that hard !! :)

thanks for any help


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 9:32 am 
Newbie

Joined: Wed Aug 13, 2008 11:22 am
Posts: 4
wow... so nobody ever does a simple one -to- one relationship with shared primary keys ?

wow thats odd


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 1:42 pm 
Newbie

Joined: Wed Aug 13, 2008 11:22 am
Posts: 4
after looong looong and endless testing i finally figured it out :)

so i will just my solution for future reference for anyone who wants to use a shared primary key in a one to one relation ship:

the person class
Code:

@Entity
@Table(name="person")
public class Person implements Serializable{

   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "person_seq")
   @SequenceGenerator(name = "person_seq", sequenceName = "person_seq", allocationSize = 1)
   @Column(name="id")
   private int id;
   
   @Column(name="name", length=40)
   private String name;

   @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY, optional=false)
   @PrimaryKeyJoinColumn(name="id", referencedColumnName="id")
   private PersonAddress address;


   [b] // MOST IMPORTANT

public void setAddress(PersonAddress address) {
      this.address = address;
      this.address.setPerson(this);
   }[/b]

      // other getters and setter are omitted
}


and the person_address class
Code:
public class PersonAddress implements Serializable {

   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue(generator = "foreign")
   @GenericGenerator(name = "foreign", strategy = "foreign", parameters = { @Parameter(name = "property", value = "person") })
   @Column(name = "id")
   private int id;

   @OneToOne(mappedBy = "address", fetch=FetchType.LAZY)
   private Person person;

   @Column(name = "street")
   private String street;
   
       // getters and setter are omitted

}



there are 3 important things:

1. the person address class needs the foreign generator
2. in person when you set address the address has to know about the person... so set it right there
3. if you want a oneToOne relationship to be initiliazed lazy you HAVE to say optional=false

well good luck to all of you :)[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 2:45 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You need to use a "foreign" generator. Look of it in the Hibernate Core reference doc.

_________________
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.