-->
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.  [ 5 posts ] 
Author Message
 Post subject: Composite Foreign Key lost in merge() operation
PostPosted: Fri Sep 10, 2010 9:32 am 
Newbie

Joined: Mon Jun 21, 2010 4:23 am
Posts: 4
Imagine an Employee Entity that references a Department using a Compound Key:

Code:
@Entity
public class Employee {
   ...
   @ManyToOne
   @JoinColumns({
      @JoinColumn(name="dept_country", referencedColumnName="country"),
      @JoinColumn(name="dept_id", referencedColumnName="id")
   })
   private Department dept;
   ...


In a Stateless Session Bean, I associate an Employee with a Department, by setting the appropriate attribute:
Code:
employee.setAbc(abc);
System.out.println(entityManager.contains(aDepartment)));  //true
employee.setDepartment(aDepartment);
employee.setXyz(xyz);
entityManager.merge(employee);


=> All attributes are correctly persisted (updated) into the database, except the Department is not associated with the Employee.

I wonder if this is related to the compound key, because when I look at the Hibernate SQL in the background, exactly those foreign key columns are missing.

Code:
14:46:18 INFO  [STDOUT#write] Hibernate:
14:46:18 INFO  [STDOUT#write]     update
14:46:18 INFO  [STDOUT#write]         employees
14:46:18 INFO  [STDOUT#write]     set
14:46:18 INFO  [STDOUT#write]         abc=?,
14:46:18 INFO  [STDOUT#write]         xyz=?,
14:46:18 INFO  [STDOUT#write]     where
14:46:18 INFO  [STDOUT#write]         id=?


I hope I missed something trivial...

PS: I asked the same question on StackOverflow.


Top
 Profile  
 
 Post subject: Re: Composite Foreign Key lost in merge() operation
PostPosted: Sat Sep 11, 2010 4:31 am 
Newbie

Joined: Tue Aug 24, 2010 7:04 am
Posts: 14
You must enable cascading for the merge operation, otherwise your department will be ignored? See the JavaDoc of the ManyToOne Annotation.

Kind regards
Michael


Top
 Profile  
 
 Post subject: Re: Composite Foreign Key lost in merge() operation
PostPosted: Sun Sep 12, 2010 7:11 pm 
Newbie

Joined: Mon Jun 21, 2010 4:23 am
Posts: 4
Hi Michael,

Thank you for the suggestion. I tried a couple of cascade options, but that didn't help:

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumns({
...
})

=> Is the above what you had in mind?

Is this really the purpose of "cascade"? From what I understand, the above would persist the Dept atributes whenever I save the Employee.
Notice that I my case, I'm simply setting the FK column(s) in Employee. (No Dept changes)

Still I'm curious to see what Javadoc you refer to... Can you pass a URL?

Thank yuo,
J.


Top
 Profile  
 
 Post subject: Re: Composite Foreign Key lost in merge() operation
PostPosted: Mon Sep 13, 2010 2:34 am 
Newbie

Joined: Tue Aug 24, 2010 7:04 am
Posts: 14
When it is a bidirectional dependency you must also give department a reference to your employee.
Documentation: http://jcp.org/en/jsr/detail?id=317

Kind regards
Michael


Top
 Profile  
 
 Post subject: Re: Composite Foreign Key lost in merge() operation
PostPosted: Mon Sep 13, 2010 4:38 am 
Newbie

Joined: Mon Jun 21, 2010 4:23 am
Posts: 4
True. Unfortunately, it is not a bidirectional association.

Thank you for all those suggestions though,

J.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.