Collection/vector in EJB?

Discussions

EJB programming & troubleshooting: Collection/vector in EJB?

  1. Collection/vector in EJB? (5 messages)

    hey all,

      my client calls on a session bean and session bean calls on an entity bean.

    in entity bean, one of the methods is

    [code]
    public Collection ejbFindByxxx ( xx xx) {
         Vector v = new Vector();
         xxx
         xxx
        //
         return v;
    }

    [/code]

    my session bean calls on this method.. and what ever the result, passes on to the client.
    [code]

    public Collection callEntityBean(xx xx) {
         Vector v = new Vector();

         //look up entity bean..

        v = home.findByxxx(xx xx);

       return v;
    }

    [/code]

    first thing, i am not sure i am doing it right.

    second, when i compile my session bean, compiler complains Collection and Vector are incompatible. but this doesn't happen for entity bean. why? and how do i work around this?

    thank you
    lucy..

    Threaded Messages (5)

  2. Collection/vector in EJB?[ Go to top ]

    You are trying to force Collection into Vector.

    Do the following:
    [code]
    public Collection callEntityBean(xx xx) {
        //look up entity bean..
        Collection c = home.findByxxx(xx xx);
        return c;
    }
    [/code]
  3. Collection/vector in EJB?[ Go to top ]

    or cast the collection you receive to a vector

    [code]
    public Vector callEntityBean(xx xx) {
        //look up entity bean..
        Vector v = (Vector) home.findByxxx(xx xx);
        return v;
    }
    [/code]

    Anyway consider repackaging your results into an ObjectValue before returning
    it to an external application.

    Lauro
    http://www.nelcom.ch (J2EE in Ticino, Switzerland)
  4. Collection/vector in EJB?[ Go to top ]

    hey all..
      thanx all. i got the point. Still,
     how come the same thing, converting a vector into collection object compiles in an entity bean but doesn't in session bean?

    lucy..
  5. Collection/vector in EJB?[ Go to top ]

    That's because in the entity you are converting a vector to a collection whereas in your session you are converting a collection to a vector.

    All vectors are collections, but not all collections are vectors. In your case, it happens to be so, but the VM doesn't know that. All it knows is that findByxxx returns a collection.

    The error is in this line:
        v = home.findByxxx(xx xx);

    While you can cast to a vector, it is not good programming as you are relying on implementation details of findByxxx (which could return some other collection later). You should treat the return of findByxxx as a collection only (and not as a vector).
  6. Collection/vector in EJB?[ Go to top ]

    hi,
      that cleared it up.
    thank you
    lucy..