我正在使用以下代码:

@RestResource(exported = false)
public interface TransactionRepository extends CrudRepository<Transaction, Long> {

@Query(value = "SELECT " +
        "    new com.test.technical.dto.TopMemberDTO(m.id, m.name, m.email, COUNT(t.book_id)) " +
        "FROM " +
        "    member m JOIN transaction t where m.id = t.member_id and t.date_of_issue >= :dateOfIssue and t.date_of_return <= :dateOfReturn " +
        "GROUP BY t.member_id ORDER BY COUNT(t.book_id) DESC limit 5", nativeQuery = true)


List<TopMemberDTO> getTopMembers(@Param("dateOfIssue") LocalDateTime dateOfIssue, @Param("dateOfReturn") LocalDateTime dateOfReturn);


错误是:

您的SQL语法有误。检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在行'.test.technical.dto.TopMemberDTO(m.id,m.name,m.email,COUNT(t.book_id))F'附近使用1个

最佳答案

您正在使用jpql,并且说它是nativeQuery = true,需要对其进行更改。
我以这种方式使用这种查询。

@Entity
@Table(name = "TABLE")
@NamedQueries({
    @NamedQuery(name = "ANYIDENTIFIER", query = "select new example.DTO(rp.example) from
RemesaProceso rp where rp.vigente = true and rp.proceso.idProceso = :idProceso order by rp.remesaId asc")
})


在另一个类中,我返回一个DTO列表

return entityManager.createNamedQuery("ANYIDENTIFIER").setParameter("idProceso ", 2).getSingleResult();

关于java - Repository接口(interface)中的MySQLSyntaxErrorException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53914433/

10-11 03:23