问题
I'm using Spring Boot with JPA (EclipseLink) and Oracle 11.
I have CrudRepository
interface:
public interface TransportDefRepository extends CrudRepository<TransportDef, Long> {
public List<TransportDef> findByNameInOrderByNameAsc(List<String> names);
}
Calling findByNameInOrderByNameAsc
method creates query:
SELECT ID, NAME,
FROM TRANSPORT_DEFS WHERE (NAME IN (('A','B'))) ORDER BY NAME ASC
and Oracle throws exception:
ORA-00907: missing right parenthesis
Am I doing something wrong? Why there are double parenthesis in generated query?
回答1:
My solution:
@Query("select t from TransportDef t where t.name in ?1 order by t.name asc")
public List<TransportDef> findByNameInOrderByNameAsc(List<String> names);
With @Query annotation eclipselink creates query:
SELECT ... FROM TRANSPORT_DEFS WHERE (NAME IN (?,?,?)) ORDER BY NAME ASC
It's only workaround - I have no idea why Spring Data JPA generates invalid query without this annotation...
回答2:
I am not so sure about that specially the need for you to pass a list in to be queried from your table but if I were you I would code it differently.
1.Instead of passing a list as an argument I would pass parameters. and do the List names iteration on the service layer and still return list from your repo. so my code on my repo would be like below
@Query("SELECT t FROM TRANSPORT_DEFS t WHERE t.name = :name)
List<TransportDef> findByNameInOrderByNameAsc(@Param("name") String name);
This would work you just need to tweak your logic on your service.
来源:https://stackoverflow.com/questions/36921151/spring-jpa-create-double-parenthesis-query