Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Persistence(persisting bidirectional entities)
Persistence [message #831633] Thu, 29 March 2012 05:56 Go to next message
Mitesh Pandey is currently offline Mitesh PandeyFriend
Messages: 7
Registered: March 2012
Junior Member

I am stuck with the persistence of an entities.

I have an entities like below

Pk customer_id

PK customer_id

Credit card primary key is the customer id from the customer table.

Code goes as follows

public class Customer
int customer_id

private string customerName;

private CreditCard creditCard

public class CreditCard
int customer_id

private int number;

private Customer customer

Test Class
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 15:14 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1300
Registered: July 2009
Senior Member

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.

Best Regards,
Previous Topic:HSQLDB Update schema
Next Topic:Validate against database schema
Goto Forum:

Current Time: Wed Jun 20 09:53:05 GMT 2018

Powered by FUDForum. Page generated in 0.01600 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top