Hibernate: multiple @ManyToMany relationships in one table
I have a project that uses Hibernate to manage objects. There are multiple @ManyToMany relationships between the two tables. So basically I have code like this:
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "movies_screenplay_authors",
joinColumns = @JoinColumn(name = "movie_id", nullable = false, updatable = false),
inverseJoinColumns = @JoinColumn(name = "staff_id", nullable = false, updatable = false))
private Set<Staff> screenplayAuthors = Sets.newHashSet();
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "movies_story_authors",
joinColumns = @JoinColumn(name = "movie_id", nullable = false, updatable = false),
inverseJoinColumns = @JoinColumn(name = "staff_id", nullable = false, updatable = false))
private Set<Staff> storyAuthors = Sets.newHashSet();
Now I would like to keep this relationship inside a single table with an additional column describing the type of the relationship. So, basically, I would like to have something like this (pseudocode is used):
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "movies_staff",
joinColumns = @JoinColumn(name = "movie_id", nullable = false, updatable = false),
inverseJoinColumns = @JoinColumn(name = "staff_id", nullable = false, updatable = false),
joinCriteria = @JoinCriteria(columnName = "staff_type", value = StaffType.SCREENPLAY_AUTHOR, enumType = EnumType.STRING))
private Set<Staff> screenplayAuthors = Sets.newHashSet();
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "movies_staff",
joinColumns = @JoinColumn(name = "movie_id", nullable = false, updatable = false),
inverseJoinColumns = @JoinColumn(name = "staff_id", nullable = false, updatable = false),
joinCriteria = @JoinCriteria(columnName = "staff_type", value = StaffType.STORY_AUTHOR, enumType = EnumType.STRING))
private Set<Staff> storyAuthors = Sets.newHashSet();
Is this possible in Hibernate or anywhere in the Java world?
+3
source to share