I would like to know if it is possible to use a Map in a @ManyToMany relationship.
For instance, consider the entities Movie and Person:
public class Movie implements Serializable {
...
@ManyToMany(mappedBy="actor_roles")
private Set<Person> cast;
...
}
and now Person.java:
public class Person implements Serializable {
...
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "Person_Movie_Actor",
joinColumns = { @JoinColumn(name = "person_id") },
inverseJoinColumns = { @JoinColumn(name = "movie_id") })
private Set<Movie> actor_roles;
...
}
The issue is that I want to preserve this many-to-many relationship, but I also want to hold a mapping [Person ==> Role] per Movie. Something like this I guess:
@ManyToMany(mappedBy="actor_roles")
private Map<Person, String> cast;
where the String would be something like "Neo", "Inspector Gadjet", "Don Corleone",... you get the idea.
What is the best way to get such mapping? Of course, I could create a new attribute in the Person entity
private Map<Movie, String> roles;
but this approach feels like I would be duplicating data as result of bad design. Any ideas?