Consider this scenario:
A User may have multiple Projects, and a Project contains multiple Users.
Additionally, each relationship has a participation level (VIEWER, MEMBER or MANAGER) which grants the user different privileges depending on their role. The participation level is an enumeration.
In JPA, I have implemented this relationship on the Project as a map with the User entity as its key, and a Participation enum as its value, as follows:
@Entity
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
// fields...
@ElementCollection
@CollectionTable(name = "user_x_project")
@MapKeyJoinColumn(name = "user_id")
@Column(name = "participation_level_uid")
@Enumerated(EnumType.STRING)
private Map<User, Participation> userAssignments;
// getters and setters...
}
The above code works fine. What I haven't been able to achieve is the inverse relationship.
I have tried this:
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
// fields...
@ManyToMany(mappedBy = "userAssignments")
private Map<Project, Participation> projectAssignments;
// getters and setters...
}
But when it runs, I get this output:
Exception Description: [class User] uses a non-entity [class Participation] as target entity in the relationship attribute [field projectAssignments].
I think I need to indicate somehow that the key of the map is to be used as the target entity, but I'm not sure how. Any ideas?