What type of bean to use to query the database to get the data.


EJB design: What type of bean to use to query the database to get the data.

  1. I have a query which gets the data from database. I would lik to know, What type of bean I should use to query.

    select sum(noOfPersons) from tableA where starttimestamp >= temptime and endtimestamp <= temptime;

    Any suggestion is appreciable :

    From my side,
          I would like to use the stateless bean and query the database to get the sum of the persons from starttime to endtime from all records in tableA.

          Is it probable to assume that if any query is there, which only gets the data from db. Better to go for stateless bean and query to db ?

    Thanks in advance,
  2. Hi.

    If you're using EJB1.X, I would go for a stateless bean. An entity bean doesn't seem to work here, because in 1.X the only methods that aren't related to a specific entity are finders.
    EJB2.0 added home methods, which are somewhat like normal static methods - related to the home instead of a specific instance. I think a home method in the "Person" entity (assuming you have one) will be more elegant in that case. Performance of home methods and statless bean methods are (atleast theoreticly) the same.

  3. Hi ,
          I agree with you. But thru BMP entity bean , we can achieve the result.
           Through BMP , Write our own business method and query the database. But in this case, resource utilization is more.
           On what basis, I should compare and select the type of bean. Can U suggest the pros and cons of selecting the BMP and stateless bean to implement the query.

  4. First of all, as I said, in EJB2.0 I would definately use home methods because they provide the most elegant solution and good performance&scalability.
    Using BMP entities with EJB1.X is, atleast for me, not an option. Putting this kind of method in a Person entity bean is very bad design (IMHO). This sum is not a property specific to a person, and so it shouldn't be made to look like one. How would you get the sum? Just find some random bean (i.e, home.findByPrimaryKey(whatever)) and then call the method on it? The only option I can even consider in this case is to make a PersonList entity bean and put the method there. But this is bad use of entity beans and will probably result in non-portable code unless you know the EJB spec well, and in less efficient code if you do.
    So, the only option I can see is put the method in a stateless bean (or possibly in a stateful one if you already have a stateful beans that suplies similar services).

  5. Thank you for your suggestions