0 Replies Latest reply on Jan 16, 2011 7:59 AM by Hubert Lyczek

    @PrimaryKeyJoinColumn - exception

    Hubert Lyczek Newbie



      I've dicided to try one-to-one relation using @PrimaryKeyJoinColumn. But i cannot force it to work.



      - AS: jboss-6.0.0.Final

      - java version "1.6.0_23"


      Problem description:

      I have two entities: Customer, Address. Relation between Customer and address is bidirectional one-to-one. The relation  uses primary keys.

      You can see created entity beans below:



      public class Customer implements Serializable{
           @Column(name="CUSTOMER_ID", nullable=false)
           private Integer customerId;
           @OneToOne(fetch=FetchType.EAGER, cascade=CascadeType.ALL, optional=true)
            private Address address;  




      public class 
      Address implements Serializable{      
           @Column(name="ADDRESS_ID", nullable=false)
                name="foreignGenerator", strategy="foreign", 
                parameters={@Parameter(name="property", value="customer"
           )})     private Integer addressId;
           @OneToOne(mappedBy="address", fetch=FetchType.LAZY, optional=false)     
            private Customer customer;

      As you see i'm using @GeneratedValue and @GenericGenerator to be sure that address_id will be the same as customer_id.

      Unfortunately that doesn't work.


      I tried to run code (from remote client):



      Customer customer = new Customer();
      ...          //set customer fields
       Address address = new Address();
       ...          //set addres fields
      databaseManager.createCustomer(customer);     //persist

      This is implementation of method createCustomer.



      public void createCustomerCore(Customer customer){


      Unfortunately when i try to persist customer with address i get this exception:



      Exception in thread "main" javax.ejb.EJBTransactionRolledbackException: 
      org.hibernate.id.IdentifierGenerationException: null id generated for:class hl.test.relations.entity.Address
           at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.handleInCallerTx(


      I thought that i would get the same id for Address as for Customer. But i got exeption and i don't know why.

      Could you guys help me find out where is the reason of the exception and how to fix my code?


      Thank you in advance