Java Spring JPA查询findByNameAndType返回null
我有一个接口公共接口IALACCountsEPO扩展CRUDREposition 下面是一个类似的方法Java Spring JPA查询findByNameAndType返回null,java,spring-boot,spring-data-jpa,Java,Spring Boot,Spring Data Jpa,我有一个接口公共接口IALACCountsEPO扩展CRUDREposition 下面是一个类似的方法 @Query(value = "SELECT * FROM account a where a.id in :id AND a.accountType = :accountType", nativeQuery=true) public AccountEntity findByIdAndAccountType(@Param("id") String id, @Param("account
@Query(value = "SELECT * FROM account a where a.id in :id AND a.accountType = :accountType", nativeQuery=true)
public AccountEntity findByIdAndAccountType(@Param("id") String id, @Param("accountType") String accountType);
当我在助手类中调用此方法时,我将获得一个ID列表,并循环遍历它们,并将筛选值传递给上述方法,但当数据库中没有与查询匹配的记录时,我会遇到问题
它只是通过说null(当数据库中没有记录时)停止。我不想通过异常,需要忽略并继续,如果我可以做到这一点。我是否可以将其编码为忽略空值并继续?或者我需要条件查询或任何其他
for(字符串id:accountIdList){
accountEntityTemp=iAllAccountsRepo.FindByiandAccountType(id,accounttype);
System.out.println(“bs”+accountEntityTemp.getId());
如果(accountEntityTemp!=null){
AccountEntityList.add(accountEntityTemp);
}
}
accountEntityTemp=null表示一个id和accounttype,我得到以下错误
2015-12-10 14:20:03.784警告10524---[nio-8080-exec-1].m.a.ExceptionHandlerExceptionResolver:处理程序执行导致异常:null
谢谢。我甚至不知道在传递id的单个参数时,为什么要在函数中使用
。
然而,您想要的方法应该如下所示:
AccountEntity findByIdAndAccountType(Integer id, String accountType);
嗯。。。首先请
@Query(value = "SELECT * FROM account a where a.id =:id AND a.accountType = :accountType", nativeQuery=true)
public AccountEntity findByIdAndAccountType(@Param("id") String id, @Param("accountType") String accountType);
话虽如此。。。
只是为什么?你在扩展crudepository,所以你可以写
AccountEntity findByIdAndAccountType(String id, String accountType)
如果AccountEntity bean上的属性已命名
身份证件
帐户类型
或者将id更改为整数,因为我不知道它的类型
再加上男人,对潜在NPE的关心。。
您告诉我们可能找不到accountentity。。。但你知道
accountEntityTemp = iAllAccountsRepo.findByIdAndAccounttype(id, accounttype);
System.out.println("bs" + accountEntityTemp.getId()); /// Null pointer here potentially...
if (accountEntityTemp != null) {
accountsEntityList.add(accountEntityTemp);
}
最多把它改成
accountEntityTemp = iAllAccountsRepo.findByIdAndAccounttype(id, accounttype);
if (accountEntityTemp != null) {
System.out.println("bs" + accountEntityTemp.getId());
accountsEntityList.add(accountEntityTemp);
}
在查看了spring文档之后,我改变了一切,但问题是打印语句,这很愚蠢。多谢。