Repository接口中的MySQLSyntaxErrorException

时间:2018-12-24 14:04:48

标签: java mysql spring jpa

我正在使用以下代码:

@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

1 个答案:

答案 0 :(得分: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();