Geeks, please accept the hero post of 2021 Microsoft x Intel hacking contest>>>
When using the getsingleresult() method, if there is no result (that is, null is returned), the habitual thinking is that null should be returned. However, there is such a statement in the getsingleresult() method: @ throws entitynotfoundexception if there is no result
that is to say, when he can’t find the result, he will throw entitynotfoundexception instead of null
this is very annoying. If you can’t find the result, you don’t want to throw an exception out of the business logic at any time. It’s troublesome to do exception handling because of this exception
return getSession()
.createQuery(hql)
.uniqueResult();
2. The insurance method is: still use the getresultlist () method, no matter you are getting one or more results, do the following processing when getting one result:
List list = getSession().createQuery(hql).getResultList();
if(list.size() > 0)
return list.get(0);
else
return null;
Similar Posts:
- [Solved] Redis has java.net.socketexception: permission denied: connect problem during stress test
- The execution order of return statement in try except else finally in Python
- [Solved] Using JDK dynamic agent to customize SPI Error: UndeclaredThrowableException
- How to Solve stale element reference: element is not attached to the page document
- [eslint] Unexpected block statement surrounding arrow body; move the returned value immediately a…
- [Solved] Easyui Error: Cannot read property ‘length’ of null
- When spring boot udpate delete data, the transaction management reports an error
- MySQL reports an error lock wait timeout exceeded; Try restarting transaction problem solving method
- Spring: How to Solve Global exception @restcontrolleradvice
- [Solved] DOM parsing XML Error: Content is not allowed in prolog