错误生成 Hibernate Jpa 存储库查询

Teo*_*Teo 4 sql spring hibernate jpa repository

我将此查询与 Jpa 存储库一起使用:

@Query("select f.attrezzature from FoglioLavoro f where f.id = :idFoglioLavoro")
Page<AbstractAttrezzatura> findAttrezzaturaFoglioLavoro(@Param("idFoglioLavoro")Long idFoglioLavoro, Pageable pageable);
Run Code Online (Sandbox Code Playgroud)

但是当我使用这个查询时,我得到了这个错误:

ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') as col_0_0_ from foglio_lavoro fogliolavo0_, foglio_lavoro_attrezzature attrez' at line 1
Run Code Online (Sandbox Code Playgroud)

这是生成的 spring-data 查询:

Hibernate: 
select
    count(.) as col_0_0_ 
from
    foglio_lavoro fogliolavo0_,
    foglio_lavoro_attrezzature attrezzatu1_,
    abstract_attrezzatura abstractat2_ 
where
    fogliolavo0_.id=attrezzatu1_.fogli_lavoro 
    and attrezzatu1_.attrezzature=abstractat2_.id 
    and fogliolavo0_.id=?
Run Code Online (Sandbox Code Playgroud)

我该如何解决??非常感谢

axt*_*avt 6

当您的存储库方法返回时Page<T>,Spring Data JPA 会尝试从您指定的查询中推断出元素总数的查询,有时推断出的查询是错误的。

在这种情况下,您需要明确指定计数查询:

@Query(
    value = "select f.attrezzature from FoglioLavoro f where f.id = :idFoglioLavoro",
    countQuery = "select count(elements(f.attrezzature)) from FoglioLavoro f where f.id = :idFoglioLavoro")
Page<AbstractAttrezzatura> findAttrezzaturaFoglioLavoro(@Param("idFoglioLavoro")Long idFoglioLavoro, Pageable pageable);
Run Code Online (Sandbox Code Playgroud)