Returning Collection from finder method (EJB-QL).


EJB programming & troubleshooting: Returning Collection from finder method (EJB-QL).

  1. Hi. I'm working with CMP Entities Beans and StateLess Session beans.
    The session beans invoke a finder method that returns more than one Entity bean object (like findAll() method) as a Collection.
    But when I try to use it from the client side it throws an NotSerializable Exception.
    Does anybody knows why ? An example please!
    Best regards,
  2. make sure that once you have your collection that you are making base classes for the client application


    Collection entities = entityhome.findAll();

    Iterator it = entities.iterator();

    <ClientDTO>[] dtos = new <ClientDTO>[ entities.size() ];
    int count = 0;
    while( it.hasNext() ){

    dtos[ count++ ] = ClientDTOFactory.getData( );

    return dtos;

    you should never return the actual entity beans but make a client side dto for them
  3. Another solution could be:

     Vector result = new Vector();
     Collection users = userLocalHome.findAll();

     for(Iterator i = users.iterator(); i.hasNext(); ) {
       userLocal item = (userLocal);
       UserDTO elem = new UserDTO();
       elem.loadFromEjb(item); <---this loadFromEJB could be a method which reads data from the EJB to the DTO
     return result;

     Of course, the DTO must implement the Serializable interface.

     Hope this helps.
  4. Hi Sergui.

    Thanks for your answers.
    I've resolved it, after two or three days of studdying and studdying, with a flat Java class (only with atributes and getters/setters methods) that implement Serializable.
    I'm returning an array casting it to this class.