javax.persistence.NonUniqueResultException: result returns more than one elements
at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:287)
at com.sencloud.dao.impl.SpecificationValueDaoImpl.findByCode(SpecificationValueDaoImpl.java:79)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.sencloud.dao.impl.SpecificationValueDaoImpl.findByCode(SpecificationValueDaoImpl.java:79)
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)
getSingleResult方法获取的值不唯一。不过这个犯法不是应该获取第一个么?后面更新
getSingleResult方法一定要加try catch(
NoResultException e),这个方法假如查不到值并不会返回null而是抛出NoResultException异常。
补充:
这个方法经过查阅发现很局限,他要求必须只能返回一个值:多了抛出NonUniqueResultException异常,查询不到结果抛出NoResultException异常。所以方法慎用!
本文探讨了Hibernate中getSingleResult方法使用不当导致的NonUniqueResultException异常,解释了该方法的局限性和正确用法。
4401

被折叠的 条评论
为什么被折叠?



