4 Replies Latest reply on Feb 15, 2006 2:58 PM by dsouza

    PrimaryKeyJoinColumn in OneToOne relationship

    dsouza

      Hi,

      I want to create a one-to-one bi-directional relationship in which both entities share the same primary key. So, following the example in the docs, I wrote this:

      @Entity
      public abstract class Customer {
      
       @Id
       @GeneratedValue(strategy = GenerationType.AUTO, generator = "C_SEQ")
       @SequenceGenerator(name = "C_SEQ", sequenceName = "CUSTOMER_SEQ")
       public Integer getId() {
       return id;
       }
      
       ...
      
       @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, optional = true)
       @PrimaryKeyJoinColumn
       public CustomerLogin getCustomerLogin() {
       return customerLogin;
       }
      }
      

      My other entity looks like this:
      @Entity
      public class CustomerLogin {
      
       @Id
       public Integer getId() {
       return id;
       }
      
       ...
      
       @OneToOne(optional = false, mappedBy = "customerLogin")
       public Customer getCustomer() {
       return customer;
       }
      


      Whenever I insert a customer/customerLogin pair I get the following exception:

      org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): CustomerLogin
      


      I was hoping that by using automatic ID generation in the customer entity and annotating the relationship with @PrimaryKeyJoinColumn it would automatically set the id in CustomerLogin. Note that the id is generated from a sequence in the database so if I want it to work like this I'd have to save the Customer entity, so that it would fetch the next id from the sequence, set the ID for CustomerLogin and then save CustomerLogin. Is there any way to make this automatic?


        • 1. Re: PrimaryKeyJoinColumn in OneToOne relationship
          epbernard

          you can use the hibernate specific @GenericGenerator(strategy="foreign")
          Check Hibernate reference documentation for more information

          • 2. Re: PrimaryKeyJoinColumn in OneToOne relationship
            dsouza

            Great! Exactly what I'm looking for. I'm really getting fond of hibernate :-)

            • 3. Re: PrimaryKeyJoinColumn in OneToOne relationship
              emdlc

              I have the same problem. I tried using the example, but it wasn't finding the @GeneratedValue annotation used:


              Child

               @Id @GeneratedValue(generator="fk1")
               @GenericGenerator(name="fk1", strategy="foreign")
               public Integer getId() {
               return this.id;
               }
              


              Parent
               @Id(generate = GeneratorType.AUTO)
               @Column(name = "testAssignmentId", unique = true, nullable = false, insertable = true, updatable = true)
               public Integer getId() {
               return this.Id;
               }
              


              What am I doing wrong?

              • 4. Re: PrimaryKeyJoinColumn in OneToOne relationship
                dsouza

                If it won't find the @GeneratedValue annotation maybe you're using an older version of EJB3. I'm using the latest EJB3 RC5 with the latest hibernate and it works.

                You also have to tell hibernate which property maps the relationship so I guess in your case it would be something like this (in the child class):

                @Id
                @GeneratedValue(strategy = GenerationType.AUTO, generator="fk1")
                @GenericGenerator(name="fk1", strategy="foreign",
                 parameters = {@Parameter(name = "property", value = "parent")})
                public Integer getId() {
                
                ...
                
                @OneToOne(mappedBy = "child")
                public Parent getParent() {
                
                ...
                
                


                ...assuming you have a "child" relationship in the parent class.