Why you should never use getSingleResult() in JPA | Sysout.be - 0 views
sysout.be/...ver-use-getsingleresult-in-jpa
database JPA EntityManager.getSingleResult() EntityManager.getResultList()

-
The only use for getSingleResult() is when we are
- ...8 more annotations...
-
We never know for sure what we can expect from our database, so throwing an unchecked exception seems the wrong choice for this use-case
-
thinking defensively, in a pathological scenario, the data and/or schema of the database could be corrupt eg. using a named query that should return a single entity based on a unique constraint of a table: - what if the unique constraint was implemented incorrectly in the ddl? (too relaxed or too strict) - what if the data became inconsistent as a result of some external process? => should always code defensively, especially at integration points to anything external to the jvm
-