-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Null Foriegn Key On Merge
PostPosted: Fri Jan 05, 2007 9:08 pm 
Newbie

Joined: Thu Nov 09, 2006 9:42 pm
Posts: 13
Hello,

We are noticing some strange behavior with Hibernate's JPA. There is an object called Customer composing a List of PaymentMethod instances with a OneToMany annotation specifying a join column. PaymentMethod is abstract, extended by a CreditCard object using a discriminator.

Now we can add CreditCard instances to a Customer, persist it, remove items from the list, add new CreditCard instances, merge, read the Customer back out and everything ends up like you'd expect.

However, if we read a persisted Customer with a CreditCard out and then completely replace the PaymentMethod list with a new list instance, containing a new CreditCard instance, then call merge we end up with something very odd. The CreditCard instance that was originally persisted with the Customer is still in the database. The new CreditCard instance is also in the database, but does not have a foreign key pointing to Customer. When we read the Customer back out we get the CreditCard instance originally associated with the Customer when it was persisted, which was not associated with the Customer object when it was merged. Since the FK on the new CreditCard instance is null, we never see that one associated with the Customer again after the merge.

Unfortunately, this is done across a web service. After the marshalling/unmarshalling at each side, the list of PaymentMethods is always new.

We have been able to duplicate this behavior without ser/deser across the wire, using only Hibernate and Spring DAOs.

Please advise, and thank you.

--
Tim


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 06, 2007 9:49 am 
Beginner
Beginner

Joined: Sat Dec 16, 2006 9:53 pm
Posts: 31
Location: Brisbane, Australia
Is the customer / payment-method relationship mapped both ways? Apparently, it has to be.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 08, 2007 3:07 pm 
Newbie

Joined: Thu Nov 09, 2006 9:42 pm
Posts: 13
Adding in the bi-directional navigation seems to have fixed the null foreign keys on the new CreditCard instances. Now when we read the Customer out after merging with a new PaymentMechanism list containing new CreditCard instances we are getting the correct PaymentMechanism list.

Unfortunately the old CreditCard instances are not deleted from the database. Their foreign keys are now null, which is why they are not associated with the Customer instance on read. Hibernate seems to offer a 'delete-orphans' configuration to deal with this, but this option isn't available through JPA annotations.

Anyway, thanks for the info. We'll followup with how things are going over here.

--
Tim


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.