在spring数据jpa本机查询中传递列表
使用SpringDataJPA,我试图使这种查询更加复杂,这是一个简单的例子在spring数据jpa本机查询中传递列表,jpa,jpa-2.0,spring-data-jpa,jpa-2.1,Jpa,Jpa 2.0,Spring Data Jpa,Jpa 2.1,使用SpringDataJPA,我试图使这种查询更加复杂,这是一个简单的例子 @Query(nativeQuery = true, value = "SELECT * FROM events WHERE typeId IN (?1)") List<Event> findEventsByType(List<Integer> types); 我尝试了List、Integer[]、Object[]和String,但不起作用 我不能传递值列表吗 进行此类查询的最
@Query(nativeQuery = true,
value = "SELECT * FROM events WHERE typeId IN (?1)")
List<Event> findEventsByType(List<Integer> types);
我尝试了List谢谢 尝试删除@Query并使方法名为:
public List<Event> findByTypeIn(List<Integer> types);
请参见链接中的表2.2:使用JPQL。本机查询是或应该完全按照创建SQL字符串的方式传递到数据库,除非驱动程序能够接受序列化的集合并理解单个参数需要解释为多个,否则它将无法工作。您传入的集合需要SQL从扩展到?,。。。基于集合中元素的数量,JDBC驱动程序无法做到这一点,JPA提供程序需要按原样执行字符串
JPQL查询允许JPA提供程序根据传入的列表动态创建所需的SQL,以便为您扩展集合 尝试如下查询:
@Query(nativeQuery = true,
value = "SELECT * FROM events WHERE typeId = ?1")
List<Event> findEventsByType(List<Integer> types);
它有用吗?我像下面那样试过,它对我有用
@Query(value = "select * from events where type_id in :types", nativeQuery = true)
List<Event> findEventsByType(@Param("types") List<Integer> types);
试试这个。它适用于SpringBoot JPA中的本机查询:
@Query(value = "SELECT * FROM table WHERE columnName IN (:inputList)" ,
nativeQuery = true)
List<Object> findByObjectList(@Param("inputList") List<Object> inputList);
如果是JPA,请尝试以下方法:
List<Object> findByObjectList(List<Object> inputList)
在IN子句中以这种方式传递数组
@Query(value = "select name from teams where name in :names", nativeQuery = true)
List<String> getNames(@Param("names") String[] names);
这边叫
String[] names = {"testing team","development team"};
List<String> teamtest = teamRepository.getNames(names);
我知道这有点脱离上下文,我们使用更新而不是选择,但这对其他人很有用:
/**
* Update the state of list of entities using their ids
* @param ids request ids
* @param state new state
* @return
*/
@Modifying
@Query(value = "UPDATE AbstractRequest SET state = :state WHERE id IN (:ids)")
int updateStates(@Param("ids") List<Long> ids, @Param("state") InternalRequestStep state);
这就是你问题的解决办法
这里我传递包含公司名称的列表,查询数据库并将结果存储在列表中
希望这次和平 我不能这样做,因为真正的查询更复杂。我在这里展示了基本情况。这是一篇很老的帖子,但是你解决了这个问题吗?
/**
* Update the state of list of entities using their ids
* @param ids request ids
* @param state new state
* @return
*/
@Modifying
@Query(value = "UPDATE AbstractRequest SET state = :state WHERE id IN (:ids)")
int updateStates(@Param("ids") List<Long> ids, @Param("state") InternalRequestStep state);
@Query(value = "SELECT c from Company c where " +
"c.companyName IN (:company_names)")
List<Company> findCompaniesByName(@Param("company_names") List<String> companyNames);