简体   繁体   中英

Spring Data JPA @Query annotation, nativeQuery = true,

I have problem with my @Repository :

@Repository
public interface RekvZmRepository extends CrudRepository<RekvalZamestn, RekvalZamestnPk> {
@Query(value = "SELECT z.* FROM rek_zm d INNER JOIN proj_a a ON d.id = a.prj_idcislo"
  + "                                   INNER JOIN proj_e e ON a.id = e.id"
  + "                                   INNER JOIN rekv_z z ON d.id = z.id"
  + "WHERE  d.id = ?1 AND a.id = ?2 AND e.id = ?3", nativeQuery = true)
public List<RekvalZamestn> getRekvOsOnDoh(Long dhzmrk, Long prj, Long prje);
}

When I run it, result is:

org.springframework.dao.InvalidDataAccessResourceUsageException: could not
extract ResultSet; SQL [n/a]; nested exception is
org.hibernate.exception.SQLGrammarException: could not extract ResultSet

When I run SQL it works, so for me it looks like problem is not with SQL but with my @Query .

Your query, in one-line format (condensing multiple spaces to one space for reasons of, erm, space), equates to

SELECT z.* FROM rek_zm d INNER JOIN proj_a a ON d.id = a.prj_idcislo INNER JOIN proj_e e ON a.id = e.id INNER JOIN rekv_z z ON d.id = z.idWHERE  d.id = ?1 AND a.id = ?2 AND e.id = ?3

If you concatenate all the strings in one line like this, it becomes obvious that you are missing a space before the WHERE clause.

SELECT z.* FROM更改为SELECT z FROM假设RekvalZamestn类映射到表rekv_z

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM