2 Replies Latest reply on May 18, 2006 3:13 PM by tsar_bomba

    need help w/ a simple @OneToOne :(

      I'm having a very hard time making a simple one-to-one relationship between two objects. I'm using JBoss AS 4.0.4.CR2

      I've been comibing the forums since last night but haven't found a solution to this. I figured it would be similar to Hibernate and never had a problem w/ one-to-one relationships w/ it.

      I have a shopping cart application w/ two entities that are related one-to-one - User and Customer. When the customer buys an item he also creates a User account. The relationship should be inverse.

      In the first round attempt, the (shortened) relationships looked like this:

      @Entity
      @Table(name="tbl_user")
      public class User implements Serializable
      {
       @OneToOne(mappedBy="user")
       public Customer getCustomer()
       {
       return this.customer;
       }
      }
      
      @Entity
      @Table(name="tbl_customer")
      public class Customer implements Serializable
      {
       @OneToOne(mappedBy="customer")
       public User getUser()
       {
       return this.user;
       }
      }
      


      If I deploy it that way and let hibernate create my tables...there isn't a foreign key in either of the tables, they don't have a relationship at all. I was *really* hoping this was the way since this works for @OneToMany and is quite simple and nice to read. I tried cascading ALL as well but it appears that this just makes them join on primary key, even though I'm not mapping it that way, am I?

      Seeing that, I checked the forums and some people suggested using @GenericGenerator but that didn't seem to work entirely, the foreign keys are generated in each table but the Customer *always* populated the user_id field w/ a NULL in the table. The relationship was not inverse...but it was a closer step.

      I did this:

      @Entity
      @Table(name="tbl_user")
      public class User implements Serializable
      {
       @OneToOne
       @GenericGenerator(name="fk_user", strategy="foreign")
       public Customer getCustomer()
       {
       return this.customer;
       }
      }
      
      @Entity
      @Table(name="tbl_customer")
      public class Customer implements Serializable
      {
       @OneToOne
       @GenericGenerator(name="fk_customer", strategy="foreign")
       public User getUser()
       {
       return this.user;
       }
      }
      


      So I noticed the documentation used @JoinColumn on one side of the relationship. I had tried to avoid this annotation altogether as I was able to use mappedBy on all of my @OneToMany and it was more elegant, in my opinion.

      So I tried @JoinColumn on the User side and mappedBy on the Customer side but I noticed that the "fk_" field I had created was null when the inserts were performed.

      I'm absolutely sure that I'm assiging a fully-populated User entity to the setUser() setter on the Customer entity - and likewise...a fully populated Customer entity on the setCustomer() setter on the User side...before persisting either of them.

      Should I have manually been assigning this key? I thought Hibernate would take care of it since it generated the column for me - should my entity have that field and manually populate it?

      The question simply is; how do I make these two entities create & use an inverse, one-to-one relationship? I have the feeling I'm just missing a simple combination of what I've tried...I've *got* to be close?

      I'm confused and at wits end at this point, can someone please advise?

      Thanks, very much.