Why JPA uses javax.persistence.NoResultException

ps-aux

Is not usage of javax.persistence.NoResultException violation of the basic principles of exception usage in Java?

Exceptions should not be used to control normal flow of program. Returning zero result from a database query seems to me pretty standard (not exceptional) situation. One which will be handled by calling code anyway.

mabi

Note that the javadoc for NoResultException states:

Thrown by the persistence provider when Query.getSingleResult() or TypedQuery.getSingleResult()is executed on a query and there is no result to return.

By using getSingleResult() you indicate you know there's going to be exactly one result. Not more (see NonUniqueResultException), not less. So I'd argue that the absence of a result is indeed an exceptional circumstance.

If you're unsure about the amount of data that will be returned, use getResultList().

But you're not alone with your distate for this part of the JPA API. This post indicates that the only really valid use for getSingleResult() is returning a scalar value, pointing out that NoResultException is a unchecked exception and quoting Bloch's Effective Java, Item 40:

Use checked exceptions for recoverable conditions and runtime exceptions for programming errors.

Unfortunately the JPA 2.0 spec (or any other for that matter) doesn't explain the reasoning behind this choice.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

javax.persistence.NoResultException: No entity found for query JPQL Query

From Dev

<f:viewParam> javax.persistence.NoResultException: No entity found for query

From Dev

Why has javax.persistence-api been replaced by jakarta.persistence-api in spring data jpa starter?

From Dev

What is the significance of @javax.persistence.Lob annotation in JPA?

From Dev

Spring JPA (Hibernate) No qualifying bean of type: javax.persistence.EntityManagerFactory

From Dev

javax.persistence.TransactionRequiredException Error: jpa 2 + hibernate - Native Query

From Dev

javax.persistence.TransactionRequiredException:

From Dev

Getting NoSuchMethodError: javax.persistence.Table.indexes() while performing JPA query

From Dev

Spring JPA : Insert succesfully but Update Failed (javax.persistence.PersistenceException: error during managed flush)

From Dev

javax.persistence.RollbackException: Error while committing the transaction (JPA without Spring)

From Java

Why does Maven want to use javax.persistence instead of the alternative hibernate version

From Dev

No Database Selected JPA Persistence

From Dev

JPA: configure persistence provider

From Dev

JPA persistence updates

From Java

NoSuchMethodError in javax.persistence.Table.indexes()[Ljavax/persistence/Index

From Dev

ejb3 persistence and javax persistence jar files in classpath?

From Dev

javax.persistence.PersistenceException: Invalid persistence.xml

From Dev

javax.persistence.spi.PersistenceUnitInfo.getSharedCacheMode() Ljavax/persistence/SharedCacheMode;

From Dev

javax.persistence.PersistenceException: Unable to locate persistence units

From Dev

javax.persistence.PersistenceException: No Persistence provider for EntityManager named

From Dev

javax.persistence.PersistenceException: No Persistence provider for EntityManager named XX

From Dev

JPA + Hibernate = No Persistence provider for EntityManager

From Dev

No persistence provider for EntityManager, JPA configuration

From Dev

Transactions and JPA / Persistence.xml

From Dev

JPA persistence and Hibernate javassist objects

From Dev

JPA persistence for external libraries datamodels

From Dev

JPA persistence for external libraries datamodels

From Dev

MongoDB with JAVA Persistence API ( JPA)

From Dev

Maven dependencies regarding javax.persistence JAR?

Related Related

  1. 1

    javax.persistence.NoResultException: No entity found for query JPQL Query

  2. 2

    <f:viewParam> javax.persistence.NoResultException: No entity found for query

  3. 3

    Why has javax.persistence-api been replaced by jakarta.persistence-api in spring data jpa starter?

  4. 4

    What is the significance of @javax.persistence.Lob annotation in JPA?

  5. 5

    Spring JPA (Hibernate) No qualifying bean of type: javax.persistence.EntityManagerFactory

  6. 6

    javax.persistence.TransactionRequiredException Error: jpa 2 + hibernate - Native Query

  7. 7

    javax.persistence.TransactionRequiredException:

  8. 8

    Getting NoSuchMethodError: javax.persistence.Table.indexes() while performing JPA query

  9. 9

    Spring JPA : Insert succesfully but Update Failed (javax.persistence.PersistenceException: error during managed flush)

  10. 10

    javax.persistence.RollbackException: Error while committing the transaction (JPA without Spring)

  11. 11

    Why does Maven want to use javax.persistence instead of the alternative hibernate version

  12. 12

    No Database Selected JPA Persistence

  13. 13

    JPA: configure persistence provider

  14. 14

    JPA persistence updates

  15. 15

    NoSuchMethodError in javax.persistence.Table.indexes()[Ljavax/persistence/Index

  16. 16

    ejb3 persistence and javax persistence jar files in classpath?

  17. 17

    javax.persistence.PersistenceException: Invalid persistence.xml

  18. 18

    javax.persistence.spi.PersistenceUnitInfo.getSharedCacheMode() Ljavax/persistence/SharedCacheMode;

  19. 19

    javax.persistence.PersistenceException: Unable to locate persistence units

  20. 20

    javax.persistence.PersistenceException: No Persistence provider for EntityManager named

  21. 21

    javax.persistence.PersistenceException: No Persistence provider for EntityManager named XX

  22. 22

    JPA + Hibernate = No Persistence provider for EntityManager

  23. 23

    No persistence provider for EntityManager, JPA configuration

  24. 24

    Transactions and JPA / Persistence.xml

  25. 25

    JPA persistence and Hibernate javassist objects

  26. 26

    JPA persistence for external libraries datamodels

  27. 27

    JPA persistence for external libraries datamodels

  28. 28

    MongoDB with JAVA Persistence API ( JPA)

  29. 29

    Maven dependencies regarding javax.persistence JAR?

HotTag

Archive