java—如何在SpringBoot中使用查询和分页

vlju58qv  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(276)

我在处理spring电影项目,在处理select查询和可分页代码时遇到了问题
存储库

public interface MovieRepository extends JpaRepository<Movie,Integer> {

    @Query("select p from Pelicula p join fetch p.director d where p.titulo like %?1% or d.nombre like %?1% or d.apellido like %?1%")
    public Page<Pelicula> findByTituloOrDirector(String term,Pageable pageable);
}

控制器

@GetMapping("/search/{term}")
public ResponseEntity<?> search(@PathVariable String term,Pageable pageable){
        try {
            return ResponseEntity.status(HttpStatus.FOUND).body(service.findByTitleOrDirector(term,pageable));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("{\"message\": \"Error. Please try again later.\"}"+e);
        }
    }

服务

@Transactional(readOnly = true)
public Page<Pelicula> findByTitleOrDirector(String term,Pageable pageable){
    return repository.findByTitleOrDirector(term, pageable);
}

例外情况呢

Caused by: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=d,role=com.movies.app.models.Entity.Pelicula.director,tableName=director,tableAlias=director1_,origin=pelicula pelicula0_,columns={pelicula0_.director,className=com.movies.app.models.Entity.Director}}]
    at org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:215) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.hql.internal.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:1026) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.hql.internal.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:794) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:689) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:325) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:273) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:276) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:192) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    ... 89 common frames omitted

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题