Loading

wait a moment

Criteria JPA 2 join many to many unidirectional realtion

I have two entities, Movie and Genre, with a many to many relationship from genre to movie. Genre being the “parent” of the relationship.
This generates three tables: genremovie and genre_movie

@Entity
public class Genre {
  @Id
  @GeneratedValue(strategy = AUTO)
  private Long id;

  @ManyToMany(fetch = LAZY)
  @JoinTable( name = "genre_movie",
    joinColumns = {@JoinColumn(name = "genre_id")},
    inverseJoinColumns = {@JoinColumn(name = "movie_id")}
  )
  private Set<Movie> movies = new HashSet<>();

  ...
}


@Entity
public class Movie {
  @Id
  @GeneratedValue(strategy = AUTO)
  private Long id;
  ...
}

Entity Diagram

I it possible perform this query using the criteria query api? Filter movies based on their genre id.

select *
from movie
  join genre_movie on movie.id = genre_movie.movie_id
where genre_id = 19;