6 Replies Latest reply on Nov 14, 2001 8:37 PM by haytona

    finding only a limited number of beans

    christoph

      I am using EJB1.1 CMP Entity Beans. I want to create a finder method, which returns only a limited number of beans.

      I created a custom finder method returning a collection of primary keys. The finder method takes two arguments, start and count, called ejbFindRangeSortedByName(int start, int count) in the EJB (with corresponding findRangeSortedByName(int start, int count) in the Home Interface.

      When I call findRangeSortedByName(int start, int count) the corresponding ejbfindRange...() never gets called. I just get an empty Collection, no warning or message of any kind in the logfile.

      Now I got two questions:

      1. Is it possible to write custom finders for CMP EJBs (not defined in jaws.xml, but coded in the EJB) for queries, that I can not define in a jaws.xml file (or do I have to use BMP EJBs for this purpose)?

      2. What is the best (database independent) way to implement limited queries, returning a given maximum number of results (like for displaying result 10-20 for a search on a web page)? PostgreSQL defines a "LIMIT - OFFSET" clause for a select, but I want to stay database independent. Is it possible to define such finders with the EJB2.0 Query Language?

      Btw., I am using the JBoss2.4.1 - Tomcat3.2.3 bundle and a PostgreSQL database.

      Christoph

        • 1. Re: finding only a limited number of beans
          foglesa

          Finders should be called findBy(method_of_finder) hence you should have :

          findByRangeSortedByName(params...)
          and
          ejbfindByRangeSortedByName(params...)

          if you want to set the call in the ejb. if you want to use Jboss.xml file then you need to describe the finder there. it still should be findByXxxxxx.

          Al

          • 2. Re: finding only a limited number of beans
            foglesa

            p.s. note that using cmp jboss wont automatically create a finder for you since this is not a findByEntityField type. so you either have to create the ejbfindBy yourself or follow the docks on creating your own.

            Should have mentioned that in the other post.

            Al

            • 3. Re: finding only a limited number of beans
              christoph

              Thanks, I implemented your suggested changes (and removed some bugs in my implementation :) and it works now as expected.

              Christoph

              • 4. Re: finding only a limited number of beans
                haytona

                > Thanks, I implemented your suggested changes (and
                > removed some bugs in my implementation :) and it
                > works now as expected.

                Would you mind sharing how you ended up implementing this functionality.
                i.e. ejbFindByXXX method or custom JAWS finders?

                thanks

                • 5. Re: finding only a limited number of beans
                  christoph

                  > > Thanks, I implemented your suggested changes (and
                  > > removed some bugs in my implementation :) and it
                  > > works now as expected.
                  >
                  > Would you mind sharing how you ended up implementing
                  > this functionality.
                  > i.e. ejbFindByXXX method or custom JAWS finders?
                  >
                  > thanks

                  In the EJB I implemented a method
                  [pre]
                  public Collection ejbFindByRangeSortedByName(int start, int count) throws FinderException
                  [/pre]

                  which returns a collection of primary keys (Integer
                  in my case). The SQL query I use in this method looks somewhat like

                  [pre]
                  "SELECT id FROM userbean ORDER BY name LIMIT "+count+" OFFSET "+start
                  [/pre]

                  which works for PostgreSQL. Don't know for other databases (although I would be interested in a database independent way to specify a query like this), but I think there is no standard way to do this.

                  In the home interface I implemented a method
                  [pre]
                  public Collection findByRangeSortedByName(int start, int count) throws FinderException, RemoteException;
                  [/pre]

                  No entry in jaws.xml was necessary.

                  Hope that helps,

                  Christoph

                  • 6. Re: finding only a limited number of beans
                    haytona

                    thanks.