2014-02-17 4 views
1

Long을 리턴하는 것으로 정의 된 기준 빌더가 있습니다. 결과 집합이 비어 있으면 전체 응용 프로그램이 실패합니다. 설정된 번호를 반환하려면 어떻게해야합니까? 1000? 빈 결과 집합이있는 경우빈 결과 집합을 처리하는 Hibernate/JPA

Long yesterday = new Long(0); 

CriteriaBuilder cb = em.getCriteriaBuilder(); 
CriteriaQuery<Long> q = cb.createQuery(Long.class); 
Root<CustomerHistory> hist = q.from(CustomerHistory.class); 

q.multiselect(hist.get("total")); 
Date yesterDate = new LocalDate().minusDays(1).toDateTimeAtStartOfDay().toDate(); 

Predicate w1 = cb.equal(hist.<Date>get("date"), yesterDate); 
Predicate w2 = cb.equal(hist.get("customer"), customer); 
q.where(w1,w2); 

yesterday = em.createQuery(q).getSingleResult(); 


return yesterday < tot; 
+1

이 예외를 캐치 논리를 계속 고려합니다. oracle.com/javaee/5/api/javax/persistence/NoResultException.html – Durandal

+0

아, 말이 될 것입니다! 나는 언젠가 배울 것이다! – ESP

답변

2

Query.getSingleResult()RuntimeExceptionjavax.persistence.NoResultException가 발생합니다. 당신은 try-catch 블록에서 그것을 잡아서 거기에서 처리 할 수 ​​있습니다.

결과 집합의 최대 값을 설정하려면 Query.setMaximumResults(int maxResult)으로 전화하고 getResultList() (원하는 엔터티의 List을 반환)을 호출하십시오. HTTP : // 문서 JPA API를

java.lang.Object getSingleResult() 
Execute a SELECT query that returns a single untyped result. 
Returns: 
    the result 
Throws: 
    NoResultException - if there is no result 
    NonUniqueResultException - if more than one result 

에서

1

이 경우 당신이`NoResultException`를 잡을 필요가 NoResultException의 예외를 잡아

관련 문제