I have a projection using Interface, and i need the genre.Id and the movie.Id, but in table it have the same name:
public interface MovieProjection {
Long getId(); //Genre Id
Long getId(); //Movie Id
String getName();
String getTitle();
String getSynopsis();
String getImg_url();
Integer getMovie_Year();
String getSub_Title();
}
SQL:
public interface MovieRepository extends JpaRepository<Movie, Long>{
@Query(nativeQuery = true, value = """
SELECT *
FROM tb_movie
INNER JOIN tb_genre ON tb_genre.id = tb_movie.genre_id
WHERE (:genreId IS NULL OR tb_genre.id IN :genreId)
""", countQuery = """
SELECT COUNT(*) FROM (
SELECT DISTINCT tb_movie.title, tb_movie.Synopsis, tb_genre.name
FROM tb_movie
INNER JOIN tb_genre ON tb_genre.id = tb_movie.genre_id
WHERE (:genreId IS NULL OR tb_genre.id IN :genreId)
) AS tb_result
""")
Page<MovieProjection> searchAllPaged(List<Long> genreId, Pageable pageable);
}
I tried using @Value but it doesn't seem to work
When you create the query you have to rename the fields manually.
For example
public interface MovieProjection {
Long getGenreId(); //Genre Id
Long getMovieId(); //Movie Id
String getMovieName();
String getMovieTitle();
....
}
and
SELECT *
FROM tb_movie
INNER JOIN tb_genre ON tb_genre.id = tb_movie.genre_id
WHERE (:genreId IS NULL OR tb_genre.id IN :genreId)
to
SELECT tbm.id as movieId,
tmb.name as movieName,
tmb.title as movieTitle,
tbg.id as genreId
FROM tb_movie tbm
INNER JOIN tb_genre tbg ON tbg.id = tbm.genre_id
WHERE (:genreId IS NULL OR tb_genre.id IN :genreId)