4 Replies Latest reply on Aug 26, 2004 9:54 AM by Eli

    finder exception is not thrown

    Eli Newbie

      a finder exception is not thrown when we use a finder that returns a collection (FindByName), we can see in the test that the collection is empty. also the findByPrimaryKey works fine (exception is thrown)

      this is the test code:

      package messaging_as.domain;
      import java.util.Date;
      import javax.ejb.CreateException;
      import javax.ejb.EJBException;
      import javax.ejb.FinderException;
      import javax.ejb.RemoveException;
      import junit.framework.Test;
      import junit.framework.TestSuite;
      import messaging_as.service.util.EJBHomeFactory;
      import net.sourceforge.junitejb.EJBTestCase;
      import org.apache.log4j.Category;
       * <p>Title: </p>
       * <p>
       * </p>
       * <p>Copyright: Copyright (c) 2004</p>
       * <p>Company: </p>
       * @author elih
       * @version 1.0
      public class FinderExceptionTest
       extends EJBTestCase {
       private Category _log = Category.getInstance(getClass());
       private TestCmpHome _testCmpHome = null;
       private TestCmp _testCmp = null;
       private boolean finderExceptinFlag = false;
       public static Test suite() {
       TestSuite testSuite = new TestSuite("FinderExceptionTest");
       return testSuite;
       public void setUp() throws Exception {
       _testCmpHome = (TestCmpHome) EJBHomeFactory.getFactory().
       public void tearDown() throws Exception {
       public FinderExceptionTest(String name) {
       public void testFinderException() {
       try {
       _testCmpHome.create(new Integer(1), "testCmp");
       _testCmpHome.create(new Integer(2), "testCmp");
       java.util.Collection cmps = _testCmpHome.findByName("testCmp");
       System.out.println("after creation cmps.isEmpty(): " + cmps.isEmpty());
       catch (CreateException ex) {
       catch (Exception e) {
       try {
       _testCmp = _testCmpHome.findByPrimaryKey(new Integer(1));
       _testCmp = _testCmpHome.findByPrimaryKey(new Integer(2));
       catch (RemoveException ex1) {
       catch (EJBException ex1) {
       catch (Exception e) {
       try {
       java.util.Collection cmps = _testCmpHome.findByName("testCmp");
       //_testCmpHome.findByPrimaryKey(new Integer(1));
       System.out.println("after delete cmps.isEmpty(): " + cmps.isEmpty());
       catch (FinderException ex2) {
       finderExceptinFlag = true;

      snippet from ejb-jar.xml:
       <ejb-ql>SELECT OBJECT(u) FROM TestCmp as u WHERE u.name = ?1</ejb-ql>

        • 1. Re: finder exception is not thrown
          Tim Shaw Newbie

          I thought that this was correct. The Collection return will return the empty Collection if no matches are found. Just check for empty instead of a try/catch.

          • 2. Re: finder exception is not thrown
            Eli Newbie

            I know that, the question is should jboss throw a finder exception in this situation (hint: the HomeInterface says it should)

            public Collection findByName(String name) throws FinderException;

            • 3. Re: finder exception is not thrown
              Darran Lofthouse Master

              What makes you think that the home interface says that the exception should be thrown if no results are found.

              Having no data returned from a query is a valid response, it says that there was nothing that matched the search criteria. It is only in the context of your application that you can decide if this is an error situation.

              From the EJB specification: -

              The Container throws the FinderException (or subclass of FinderException) from the implementation of a finder or select method to indicate an application-level error in the finder or select method.

              If the finder method was only supposed to return a single value and multiple were returned this would be a situation that an application error needs reporting.

              • 4. Re: finder exception is not thrown
                Eli Newbie

                When will the cmp engine will throw a finder exception (in case of finder that returns a collection) ?