17
I have list List<Long> list, that contains: [160774, 7212775] and Long id = 7212775. I need to check if the list contains an element with value of id. How to do that? Unfortunately list.contains(id) returns false in my case.
I'm using it that way:
@RequestMapping("/case/{id}")
public String openCase(@PathVariable("id") Long id) {
log.debug(caseDAO.findAllCasesId()); // [160774, 7212775]
log.debug(id); // 7212775
if(caseDAO.findAllCasesId().contains(id)) {
return "case";
} else {
return "404";
}
}
Piece of DAO (Hibernate, but native sql here):
public List<Long> findAllCasesId() {
String sql = "select id from cases";
SQLQuery query = getSession().createSQLQuery(sql);
return query.list();
}
SOLVED
The problem was with caseDAO.findAllCasesId(), that return list of Object, not list of Long. I corrected this by:
SQLQuery query = getSession().createSQLQuery(sql).addScalar("id", Hibernate.LONG);
Big thanks to: Nayuki Minase