Framework/hibernate
[Hibernate] org.springframework.dao.EmptyResultDataAccessException: No entity found for query;
코린이예요
2018. 7. 9. 22:54
반응형
Hibernate 관련 에러 발생.
Hibernate framework를 사용하던중 아래와 같은 error가 발생하였다.
org.springframework.dao.EmptyResultDataAccessException: No entity found for query;
nested exception is javax.persistence.NoResultException: No entity found for query
에러 로그
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | An unexpected error has occurred <!-- org.springframework.dao.EmptyResultDataAccessException: No entity found for query; nested exception is javax.persistence.NoResultException: No entity found for query at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:301) at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:106) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:403) at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58) at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) ....... 생략 at java.lang.Thread.run(Thread.java:662) Caused by: javax.persistence.NoResultException: No entity found for query at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:286) at org.hibernate.ejb.criteria.CriteriaQueryCompiler$3.getSingleResult(CriteriaQueryCompiler.java:257) at com.alticast.cas3.administrator.service.impl.StatSubscriberServiceImpl.countGroupByRegioncode(StatSubscriberServiceImpl.java:134) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:64) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155) ... 104 more | cs |
원인 분석
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | @Override public Long countGroupByRegioncode(StatSubscriberCommand command) { CriteriaBuilder criteriaBuilder = super.entityManager.getCriteriaBuilder(); CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class); // criteriaQuery.distinct(true); // TODO: when active, breaks the sort // on x-to-one property! // from Root<StatSubs> root = criteriaQuery.from(StatSubs.class); // select criteriaQuery = criteriaQuery.select(criteriaBuilder.count(root)); // predicate Predicate predicate = getPredicate(root, criteriaBuilder, command); if (predicate != null) { criteriaQuery = criteriaQuery.where(predicate); } // group by criteriaQuery.groupBy(root.get("statSubsPK").<String> get("statDate")); TypedQuery<Long> typedQuery = super.entityManager.createQuery(criteriaQuery); return typedQuery.getSingleResult(); } | cs |
line 20 : statDate entity가 없는데 groupby로 지정하여 query를 수행하였던 것이 원인
수정 전
1 2 3 | TypedQuery<Long> typedQuery = super.entityManager.createQuery(criteriaQuery); return typedQuery.getSingleResult(); | cs |
수정 후
1 2 3 4 5 6 7 | TypedQuery<Long> typedQuery = super.entityManager.createQuery(criteriaQuery); try { return typedQuery.getSingleResult(); } catch (NoResultException nre) { return 0l; } | cs |
NoResultException 처리를 해주어서 해결하였다.
반응형