2

i want to Map a map in JPA, but I get a Exception: My java-code looks like that:

Issue.java:

@ElementCollection
@CollectionTable(
    name="ISSUE_EMPLOYEE",
    joinColumns=@JoinColumn(name="ISSUE_ID", referencedColumnName="ID")
)
@MapKeyColumn(name="EMPLOYEEPOSITION_ID")
@MapKeyConvert("myEnumConverter")
@JoinColumn(name="EMPLOYEE_ID")
private Map<EmployeePosition, Employee> namedEmployees = new Hashtable<EmployeePosition, Employee>();

EmployeePosition is a Enum andEmployee is a Entity.

I get this Exception :

Internal Exception: java.sql.SQLException: ORA-00904: "EMPLOYEES": invalid identifier

Error Code: 904 Call: INSERT INTO ISSUE_EMPLOYEE (ISSUE_ID, EMPLOYEES, EMPLOYEEPOSITION_ID) VALUES (?, ?, ?) bind => [27, [B@18b85d, SERVICE]

It seems to ignore the @JoinColumn annotation and tries to insert the object in the DB. What´s wrong with my mapping/Is it possible to match Entities like this?

Aleš
  • 8,896
  • 8
  • 62
  • 107
CSan
  • 954
  • 1
  • 10
  • 25

1 Answers1

4

As far as I understand, you need @OneToMany instead of @ElementCollection when value of a Map is an entity. Something like this:

@OneToMany
@JoinTable(name = "ISSUE_EMPLOYEE",
    joinColumn = @JoinColumn(name = "ISSUE_ID"),
    inverseJoinColumn = @JoinColumn("EMPLOYEE_ID"))
@MapKeyColumn(name="EMPLOYEEPOSITION_ID") 
private Map<EmployeePosition, Employee> namedEmployees = new Hashtable<EmployeePosition, Employee>(); 

EDIT: The mapping above works fine in Hibernate, but doesn't work in EclipseLink. EMPLOYEEPOSITION_ID column in ISSUE_EMPLOYEE is created, but not used in queries. It happens not only with enum keys, but also with other primitive types.

It looks like a bug in EclipseLink. I can't find in in their Bugzilla, so perhaps it would be better to report it.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • I tried your annotations. I did the joinTable-Annotation like this '@JoinTable(name="ISSUE_EMPLOYEE", joinColumns=@JoinColumn(name="ISSUE_ID", referencedColumnName="ID"), inverseJoinColumns=@JoinColumn(name="EMPLOYEE_ID") )' It now does this insert-statement: "Call: INSERT INTO ISSUE_EMPLOYEE (EMPLOYEE_ID, ISSUE_ID) VALUES (?, ?)" So it doesn´t insert the EmployeePosition...Any ideas? – CSan Feb 10 '11 at 11:43
  • @Lodger: Im'm not sure about your `@MapKeyConvert` annotation. Have you tried to replace it with `@MapKeyEnumerated`? – axtavt Feb 10 '11 at 12:08
  • Yes, i tried the @MapKeyEnumerated instead of @MapKeyConvert, but it throws the same Exception. – CSan Feb 10 '11 at 13:09
  • finally reported it in their [Bugzilla](https://bugs.eclipse.org/bugs/show_bug.cgi?id=355697) – CSan Aug 24 '11 at 12:51
  • thanks this helped me a lot. For those using an Enum as key, they can even use: @MapKeyEnumerated(EnumType.STRING) – Sebastien Lorber Mar 06 '12 at 17:45