I want to add a Collection of persistent objects to a class where the collection returned is based on a column value in the collection table but is NOT based on any foreign key. For example, let's say we have the following persistent classes: 'Movie', 'TvShow', and 'AwardShow'. We want all movies to be fetched with the full set of potential award shows it could appear in, based on the logic that it can only be in movie award show (e.g. Oscars). This is a made up example, so please don't think too hard about this data model :)
Code:
public class Movie {
private Integer movieId;
private String name;
private List<AwardShow> possibleMovieAwardShows;
}
Code:
public class TvShow {
private Integer tvShowId;
private String name;
private List<AwardShow> possibleTvShowAwardShows;
}
Code:
public class AwardShow {
private Integer awardShowId;
private String name;
private String type;
}
Here are the tables:
Code:
movie
--------
movie_id
name
tvshow
---------
tvshow_id
name
award_show
-------------
award_show_id
name
type
When fetching a Movie, I want to left join on the 'award_show' table 'using (award_show.type = 'movie')... i.e. without any join column from the Movie table. How do I achieve this via annotations in the Movie class? I tried a bunch of different mappings, and none worked. Here were two I thought might achieve it:
Code:
@OneToMany(targetEntity = AwardShow.class, cascade = CascadeType.ALL)
@Column(name = "type")
@Formula(value = "'movie'")
Code:
@OneToMany(targetEntity = AwardShow.class, cascade = CascadeType.ALL)
@Where(clause = "type = 'movie'")
Any help would be really appreciated!
-Arthur Loder