3 Replies Latest reply on Dec 10, 2002 4:24 PM by Dain Sundstrom

    single-entity vs. collection find method

    Josh Lisle Newbie


      Hello All.

      I have a find method that returns a single entity bean reference.

      If no bean matches the input parameters, an exception is generated. I catch a FinderException. Should I be catching an ObjectNotFoundException instead? What is the distinction?

      Also, when more than one bean matches the input params, I also get an exception. Do I have to use a collection as the return type rather than a single bean reference? I only want one reference and would rather not use a collection.

      Thanks.
      Josh

        • 1. Re: single-entity vs. collection find method
          Dain Sundstrom Master

          FinderException is a very generic exception and can mean anything from the database crashed to the object was not found. If you are only interested in ObjectNotFoundException, only catch that one. BTW, I think in the proposed EJB 2.1 spec the changed this to not throw an exception and instead return null.

          • 2. Re: single-entity vs. collection find method
            Jeremy Boynes Newbie

            EJB2.1 still throws ObjectNotFoundException is nothing was found. The scenarios are:

            * Zero objects matched: throw ObjectNotFoundException
            * Exactly one object matched and the value was not null: return the remote/local reference
            * Exactly one object matched and the value was null: return null
            * More than one object matched: throw FinderException
            * Something else went horribly wrong: throw FinderException (or possibly RemoteException)

            The null case is allowing the finder to differentiate between finding no data and finding data but having an unknown value. This could happen in an optional realtionship e.g. a Node bean with a finder

            Node findParent(Node child)

            where the child exists but does not have a parent.

            This could happen with EJB2.0 (or 1.1) but the 2.1 spec is now specifically describing it.

            • 3. Re: single-entity vs. collection find method
              Dain Sundstrom Master

              Thanks for the summary. I haven't read the spec to closely myself.