how to find the total no of records in a table using a CMP bean

Discussions

EJB design: how to find the total no of records in a table using a CMP bean

  1. Hi,

    I'm working with the databases in Container-Managed Bean. I want to get the total no of records from a table. can that be done using the finder methods.
    If it is possible how can i get from the client.

    Is there anyother way for finding this?

    Please do help me.

    Thank you
    Suresh
  2. Interesting question, I GUESS it is not feasable by CMP finder query language (I am talking about what do I know in WebLogic query language), You can use JDBC and Session been.

    I hope I am wrong, and I would like to see an answer for this question
  3. IMHO this is a task for a SessionBean and not an entity bean. So assuming we have a session bean Teller and an Entity Account I beleive the number of accounts is a call on the Teller session bean, not the Account itself which is a single entity or a Collection of single entities.

    Dave Wolf
    Internet Applications Division
  4. Hi,
       I think the following will help. May be a crude method.
    In the entity bean's home interface u've to define a finder method like this:
        public Collection findAllItems() throws
               FinderException, RemoteException;

    In the xml file, u need to describe about this finder method. In weblogic we've to use WLQL and it'll appear like this.

    <finder>
          <method-name>findAllItems</method-name>
          <finder-query><![CDATA[(= 1 1)]]></finder-query>
    </finder>

    Note the query is equivalent to,
       "select * from <table> where 1 = 1"

    In the session bean where u are making use of ur entity bean, the code will look like this:

         Collection col = home.findAllItems();
         int size = col.size();
         
         now, "size" will give u the total no. of records in the underlying table.

    Please try this out and get back to me. My id is ahamed at aztec dot soft dot net

    --hasan
  5. very cool way Ahmad :)
  6. Hi,

    I didn't understand the xml part and how the query will be processed inside. Please brief me about this phenomenen.

    Thanks in advance.
    Ramagopal
  7. Hi Ramagopal,
       In CMP bean, we need not have to write the code for finder methods. We've to just do 2 things:
       1. define the finder methods in the Bean's Home Interface
       2. write some description for that finder method in the XML Deployment Descriptor using the Query Language of that Appln. server

    The container will generate "EJBObject_Impl.class" during the deployment of our cmp entity bean. At that time it will also generate the data access code for the finder method we've defined based on the description we've made in the DD.

    Currently these query languages are proprietory for each application server. But in EJB 2.0 specifiacation they've come up with a unified query language for querying entity beans call EJB-QL. Now onwards the app.servers that claim to be compliant with EJB 2.0 must support this EJB-QL.

    keep querying...hasan (ahamed at aztec dot soft dot net)
  8. r u seriously telling of making fun of my solution??

    thanks..hasan
  9. But that is NOT portable! It uses WebLogic proprietary syntax.

    Dave Wolf
    Internet Applications Division
    Sybase
  10. Hi,
        Yes in EJB 1.1 there is no other go. Even if u migrate ur bean to other app.server, the only thing u've to change is the <finder-query> part of the DD. You need to write the criteria based on the QL of that paricular app. server.

    thanks...hasan (ahamed at aztec dot soft dot net)