Reputation: 2342
I have an interface public interface IAllAccountsRepo extends CrudRepository
and a method in that like below
@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);
When i call this method in a helper class, where I will get a list of ids and I loop through them and pass in a filter value to the above method, but i have issues when there is no record matching the query in the database.
It just stops by saying null(when there is no record in db). I do not want to through exception, need to ignore and proceed if i can do that. Is there anyway i can code it to ignore the null and proceed? or do i need criteria query or any other?
for (String id : accountIdList) {
accountEntityTemp = iAllAccountsRepo.findByIdAndAccounttype(id, accounttype);
System.out.println("bs" + accountEntityTemp.getId());
if (accountEntityTemp != null) {
accountsEntityList.add(accountEntityTemp);
}
}
accountEntityTemp = null for one id and accounttype and i'm getting the below error.
2015-12-10 14:20:03.784 WARN 10524 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Handler execution resulted in exception: null
Thanks.
Upvotes: 1
Views: 3422
Reputation: 305
Uhm... First of all please..
@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);
That being said... Just.. why? You're extending crudrepository, so you could as well just write
AccountEntity findByIdAndAccountType(String id, String accountType)
IF your attributes on AccountEntity bean are named Id AccountType or change id to Integer, as I don't know its type..
plus man, some care of potential NPE.. you told us it's possible no accountentity is found... yet you do
accountEntityTemp = iAllAccountsRepo.findByIdAndAccounttype(id, accounttype);
System.out.println("bs" + accountEntityTemp.getId()); /// Null pointer here potentially...
if (accountEntityTemp != null) {
accountsEntityList.add(accountEntityTemp);
}
at most change it to
accountEntityTemp = iAllAccountsRepo.findByIdAndAccounttype(id, accounttype);
if (accountEntityTemp != null) {
System.out.println("bs" + accountEntityTemp.getId());
accountsEntityList.add(accountEntityTemp);
}
Upvotes: 3
Reputation: 14806
I don't even know why you use IN
function when you are passing a single argument for an id.
Nevertheless, the method you want should look like this:
AccountEntity findByIdAndAccountType(Integer id, String accountType);
Upvotes: 0