views:

79

answers:

1

Imagine an Employee Entity that references a Department using a Compound Key:

@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:

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.

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.

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...

A: 

Shoot me!

As I mentioned, I made up the Employee/Department code snippet above to clarify my case. I shouldn't have done this! I ommitted a key element: the updatable flag

Actually, my case looks as follows:

   @ManyToOne
   @JoinColumns({
      @JoinColumn(name="dept_country", referencedColumnName="country", insertable = false, updatable = false),
      @JoinColumn(name="dept_id", referencedColumnName="id", insertable = false, updatable = false)
   })

And the answer to the question is fairly obvious: Switch to updatable = true

I'm sorry!

P.S: Still, I'm puzzled why it worked in my Unit Tests and not in the App Server

Jan