Search code examples
jpaeclipselink

JPA: Map a Map<Enum, Entity>


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?


Solution

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