我正在尝试学习Spring并遇到JPA存储库查询问题 我有两个相互之间具有双向关系的类:
public class MovieGenre {
// other fields
@ManyToMany(mappedBy = "genres")
@JsonBackReference
private Set<Movie> movies = new HashSet<>();
// ...
}
和
public class Movie {
// id and other fields
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "movie_movie_genre",
joinColumns = @JoinColumn(name = "movie_id"),
inverseJoinColumns = @JoinColumn(name = "movie_genre_id"))
@JsonManagedReference
private Set<MovieGenre> genres = new HashSet<>();
我想创建一个控制器,以便能够获取其中一种类型的所有电影的json。 我试图让jpa查询为我做这件事。
public interface MovieRepository extends CrudRepository<Movie, Long> {
Stream<Movie> getMoviesByGenresIsLike(String genreName);
}
这行不通
为了给我一个想法,我试图达到的目的是正常的sql查询(并且在h2控制台中有效)
SELECT * FROM MOVIE m
INNER JOIN movie_movie_genre mmg ON m.movie_id = mmg.movie_id
INNER JOIN movie_genre mg ON mmg.movie_genre_id = mg.genre_id
WHERE genre_name = 'action';
我正试图编写这样的自定义查询
@Query(value = "SELECT * FROM MOVIE m \n" +
"INNER JOIN movie_movie_genre mmg ON m.movie_id = mmg.movie_id\n" +
"INNER JOIN movie_genre mg ON mmg.movie_genre_id = mg.genre_id\n" +
"WHERE genre_name = ?1;", nativeQuery = true)
Optional<Movie> getMoviesByGenres(@Param("name") String name);
这一切都会导致
2019-10-25 17:07:42.405 ERROR 831 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]
: Servlet.service() for servlet [dispatcherServlet] in context with path []
threw exception [Request processing failed;
nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.QueryException:
JPA-style positional param was not an integral ordinal;
nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryException:
JPA-style positional param was not an integral ordinal] with root cause
如果无法执行jpa查询,如何编写正确的自定义查询?
编辑: -我将查询更改为“从电影中选择m从内部联接中选择m.genres g WHERE g.genreName =?1”(如以下答案中所建议) -另一个问题是控制器中缺少@Transactional批注。
答案 0 :(得分:2)
尝试从使用本机查询切换到jpql概念:
"SELECT m FROM Movie m INNER JOIN m.genres g WHERE g.genreName = ?1"
或者您可以创建一个MovieGenreRepository
并使用类似以下方法的方法:
Stream<MovieGenre> findByGenreNameLike(String genreName);
答案 1 :(得分:0)
首先,您必须很好地映射您的实体,如果字段名称与您的SQL查询完全相同,则将是这样的:
@Repository
public interface MovieRepository extends CrudRepository<Movie, Long> {
@Query(value = "SELECT m FROM Movie m INNER JOIN m.genres g ON m.movie_id = g.movie_id WHERE g.genreName = :name")
Movie getMoviesByGenres(@Param("name") String name);
}
别忘了放置@Repository批注,因为我们必须始终遵循休眠标准。