user6192706
user6192706

Reputation:

MySQLSyntaxErrorException in Repository interface

I'm using code below:

@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); 

Error is:

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 '.test.technical.dto.TopMemberDTO(m.id, m.name, m.email, COUNT(t.book_id)) F' at line 1

Upvotes: 0

Views: 171

Answers (1)

Jose
Jose

Reputation: 124

You are using jpql and you say it is nativeQuery = true, you need to change it. This kind of queries i use them of this way.

@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")
})

And in another class I return a list of DTO

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

Upvotes: 1

Related Questions