sql Archives - KateBeckin Sale Gallery https://www.katebeckinsalegallery.com/tag/sql/ Do The Home Improvement Mon, 27 Dec 2021 10:33:02 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 https://www.katebeckinsalegallery.com/wp-content/uploads/2019/04/cropped-10-1-32x32.png sql Archives - KateBeckin Sale Gallery https://www.katebeckinsalegallery.com/tag/sql/ 32 32 Criteria JPA 2 join many to many unidirectional realtion https://www.katebeckinsalegallery.com/criteria-jpa-2-join-many-to-many-unidirectional-realtion/ Fri, 17 Dec 2021 10:25:19 +0000 https://www.katebeckinsalegallery.com/?p=1333 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: genre, movie and genre_movie @Entity public class Genre { @Id @GeneratedValue(strategy = AUTO) private Long id; Read More

The post Criteria JPA 2 join many to many unidirectional realtion appeared first on KateBeckin Sale Gallery.

]]>
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;

The post Criteria JPA 2 join many to many unidirectional realtion appeared first on KateBeckin Sale Gallery.

]]>