Search code examples
mappingpersistenceopenjpa

mapping a one-to-many where the map key is an int


I am trying to understand the annotations MapKey and MapKeyColumn and I have found them confusing. I was reading an article that made me even more confused (The specification section)

I have an entity with an int field and it is not the primary key:

public class Connections{
...
public final int getConnectionId() { 
    return this.connectionId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "FK_StartpointTNA")
public final Endpoint getStartpoint() {
    return this.startpoint;
}
...
}

and in the other side I have

public class Endpoint{
...
 @OneToMany(mappedBy = "startpoint", fetch = FetchType.LAZY, cascade = { javax.persistence.CascadeType.REMOVE })
@MapKeyColumn(name = "connectionId")
public Map<Integer, Connections> getConnections() {
    return this.connections;
}
 ....
 }

I dont know really how to fix this. I keep getting: org.apache.openjpa.persistence.ArgumentException: "connections" declared that it is mapped by "startpoint", but that is a not a field of the related type.

what is the proper way to map this?


Solution

  • As someone posted to the JIRA you opened, get rid of the final on your methods.

    From the JPA 2 spec:

    (Section 2.1 "The Entity Class", page 21) states: "The entity class must not be final. No methods or persistent instance variables of the entity class may be final."