2015-12-10 65 views
1

我有一个接口公共接口IAllAccountsRepo扩展CrudRepository春天JPA查询findByNameAndType返回NULL

,并像下面

@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和I列表循环遍历它们并将过滤器值传递给上述方法,但当没有与数据库中的查询匹配的记录时,我遇到了问题。

它只是停止说空(当在db中没有记录)。我不想通过例外,如果我能做到这一点,需要忽略并继续。无论如何,我可以编码它忽略null并继续?或者我需要标准查询还是其他?

for (String id : accountIdList) { accountEntityTemp = iAllAccountsRepo.findByIdAndAccounttype(id, accounttype); System.out.println("bs" + accountEntityTemp.getId()); if (accountEntityTemp != null) { accountsEntityList.add(accountEntityTemp); } } accountEntityTemp = null为一个id和accounttype,我得到下面的错误。

2015年12月10日14:20:03.784 WARN 10524 --- [NIO-8080-EXEC-1] .m.m.a.ExceptionHandlerExceptionResolver:处理程序执行导致异常:空

感谢。

回答

3

呃... ...都请首先..

@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); 

话虽这么说... 只是..为什么?你延长crudrepository,所以你可以和只写

AccountEntity findByIdAndAccountType(String id, String accountType) 

如果AccountEntity豆你的属性被命名为 标识 ACCOUNTTYPE 或更改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); 
     } 
+0

经过春季文档后,我改变了一切,但问题是印刷声明是愚蠢的。非常感谢。 – Arun

0

我甚至不知道为什么在传递一个id的单个参数时使用IN函数。 不过,你想应该是这样的方法:

AccountEntity findByIdAndAccountType(Integer id, String accountType);