spring jpa create double parenthesis query

拈花ヽ惹草 提交于 2019-12-08 09:28:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!