JBoss Community

@PrimaryKeyJoinColumn - exception

created by Hubert Lyczek in EJB3 - View the full discussion

Hello,

 

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

 

Environment:

- 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:

 

 

@Entity
@Table(name="CUSTOMER")
public class Customer implements Serializable{
  
     @Id
     @Column(name="CUSTOMER_ID", nullable=false)
     @GeneratedValue(strategy=GenerationType.AUTO)
     private Integer customerId;
  
     ...
  
     //bidirectional
     @OneToOne(fetch=FetchType.EAGER, cascade=CascadeType.ALL, optional=true)
     @PrimaryKeyJoinColumn
      private Address address;  
 
     ...
}
 

 

 

 

@Entity
@Table(name="ADDRESS")
public class 
Address implements Serializable{      
     @Id
     @Column(name="ADDRESS_ID", nullable=false)
     @GeneratedValue(generator="foreignGenerator")     @GenericGenerator(
          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
customer.setAddress(address);address.setCustomer(customer);

 
 
 
 
databaseManager.createCustomer(customer);     //persist


This is implementation of method createCustomer.

 

 

public void 
         entityManager.persist(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
 CMTTxInterceptor.java:148)
     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

 

Hubert

Reply to this message by going to Community

Start a new discussion in EJB3 at Community