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
HiAll,

I am stuck with the persistence of an entities.

I have an entities like below

Customer
----------------------
Pk customer_id
customer_name

CreditCard
-----------------
PK customer_id
number


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


Code goes as follows

@entity
public class Customer
{
@Id
@generatedValue
int customer_id

@column(name="customer_name")
private string customerName;

@OneToOne(mapped_by="customer",cascade=CascadeType.PERSIST)
private CreditCard creditCard
}


@entity
public class CreditCard
{
@Id
@generatedValue
int customer_id

@column(name="number")
private int number;

@OneToOne
@joinColumn(name="customer_id")
private Customer customer
}


Test Class
----------------------
Customer cust = new Customer();
cust.setName("XYZ");

CreditCard credit = new CreditCard();
credit.setNumber(123);
credit.setCustomer(cust);

cust.setCreditCard(credit);

entitymanager.persist(cust);


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: 1389
Registered: July 2009
Senior Member
Hello,

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,
Chris
Previous Topic:HSQLDB Update schema
Next Topic:Validate against database schema
Goto Forum:
  


Current Time: Fri Apr 19 21:46:50 GMT 2024

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

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

Back to the top