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");
       testSuite.addTestSuite(FinderExceptionTest.class);
       return testSuite;
       }
      
       public void setUp() throws Exception {
       _testCmpHome = (TestCmpHome) EJBHomeFactory.getFactory().
       lookUpHome("TestCmpLocalHome");
       }
      
       public void tearDown() throws Exception {
       }
      
       public FinderExceptionTest(String name) {
       super(name);
       }
      
       public void testFinderException() {
       _log.info("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) {
       ex.printStackTrace();
       }
       catch (Exception e) {
       e.printStackTrace();
       }
       try {
       _testCmp = _testCmpHome.findByPrimaryKey(new Integer(1));
       _testCmp.remove();
       _testCmp = _testCmpHome.findByPrimaryKey(new Integer(2));
       _testCmp.remove();
       }
       catch (RemoveException ex1) {
       ex1.printStackTrace();
       }
       catch (EJBException ex1) {
       ex1.printStackTrace();
       }
       catch (Exception e) {
       e.printStackTrace();
       }
      
       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;
       _log.info("FinderException.");
       }
       assertTrue(finderExceptinFlag);
       }
      }
      


      snippet from ejb-jar.xml:
      <entity>
       <display-name>TestCmp</display-name>
       <ejb-name>TestCmp</ejb-name>
       <home>messaging_as.domain.TestCmpRemoteHome</home>
       <remote>messaging_as.domain.TestCmpRemote</remote>
       <local-home>messaging_as.domain.TestCmpHome</local-home>
       <local>messaging_as.domain.TestCmp</local>
       <ejb-class>messaging_as.domain.TestCmpBean</ejb-class>
       <persistence-type>Container</persistence-type>
       <prim-key-class>java.lang.Integer</prim-key-class>
       <reentrant>False</reentrant>
       <cmp-version>2.x</cmp-version>
       <abstract-schema-name>TestCmp</abstract-schema-name>
       <cmp-field>
       <field-name>id</field-name>
       </cmp-field>
       <cmp-field>
       <field-name>name</field-name>
       </cmp-field>
       <primkey-field>id</primkey-field>
       <query>
       <query-method>
       <method-name>findByName</method-name>
       <method-params>
       <method-param>java.lang.String</method-param>
       </method-params>
       </query-method>
       <ejb-ql>SELECT OBJECT(u) FROM TestCmp as u WHERE u.name = ?1</ejb-ql>
       </query>
       </entity>
       <entity>
      


        • 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) ?