|Persistence [message #831633]
||Thu, 29 March 2012 01:56
| Mitesh Pandey
Registered: March 2012
I am stuck with the persistence of an entities.
I have an entities like below
Credit card primary key is the customer id from the customer table.
Code goes as follows
public class Customer
private string customerName;
private CreditCard creditCard
public class CreditCard
private int number;
private Customer customer
Customer cust = new Customer();
CreditCard credit = new CreditCard();
As per my understanding of JPA, while persisting customer entity, it will generate a primary key for the customer table(customer_id) and then it will persist it.
My requirement is that the same customer_id generated for the customer table should be set in the credit card table also when JPA persist creditCard entity as part of CascadeType.PERSIST
How this can be achieved?
Currenty it persist all the creditcard entity attributes except with the default value of 0 as customer id in the credit card table
I can't change the database design or the method signature of persistence. I have to persist customer entity and not credit card
Also, I want to achieve this in one database trip.
|Re: Persistence [message #831957 is a reply to message #831633]
||Thu, 29 March 2012 11:14
| Chris Delahunt
Registered: July 2009
If you wish to have CreditCard's Id set through the mapping, I am not sure why you marked the customer_id attribute with @generatedValue - that should be removed. It also seems odd that EclipseLink would allow this mapping as you have it set up - EclipseLink should throw an exception because you have both the customer_id and customer attributes able to change the "customer_id" field. The only thing I can think of is that EcliseLink is unable to tell they are the same field, since customer_id by default will use a "CUSTOMER_ID" field and EclipseLink is case sensitive by default - so this is not the same "customer_id" field used in the customer relationship. You should make the fields match the case that is used in your database or set the "eclipselink.jpa.uppercase-column-names" which will have EclipseLink behave in a case insensitive manner.
If you are using JPA 2.0 you can use the @MapsId annotation instead of the @JoinColumn on CreditCard's customer relationhips. Or you could do with out the customer_id attribute in CreditCard outright and just mark the customer relationship with the @Id field - EntityManager's find would use Customer's customer_id as the id.
If you are using JPA 1.0, you need to make the customer_id attribute read only by setting the updatable=false, insertable=false tags so that the field is only set through the relationship.
Powered by FUDForum
. Page generated in 0.01675 seconds