Client code cast error:EJB returning data in Collection of Objects

Still trying to understand this EJB stuff.....

My BMP EJB returns data to the client in a collection of objects;

// Home interface (CountryHome) nothing unusual here

public Collection findAllCountries()

//Remote interface (Country) nothing unusual here

public CountryModel getDetails()

The data object is a serialised object;

// data object - nothing strange here

publicclass CountryModelimplements Serializable{

privateint countryId;

private String countryName;

public CountryModel (){......etc etc

public CountryModel getDetails(){.......etc etc

public String toString(){ ...etc

When I try and get at the data from the collection in the client code, calling getDetails() in CountryModel causes a cast exception. How can I get at the data?

Collection a = home.findAllCountries();

Iterator i = a.iterator();

while (i.hasNext()){

Object obj = i.next();

Country country = (Country)

PortableRemoteObject.narrow(obj, Country.class);

// this fails with class cast exception.....

System.out.println(country.getDetails());

And to add to the confusion, why does calling the remote interface's getPrimaryKey() method in the client code invoke the toString() method in the CountryModel class and work?

while (i.hasNext()){

Object obj = i.next();

Country country = (Country)

PortableRemoteObject.narrow(obj, Country.class);

// have no idea why this works.....but it does.....

System.out.println(country.getPrimaryKey());

Thanks, lebo

[2430 byte] By [bocockli] at [2007-9-26 1:35:53]
# 1

hi,

you are getting a collection of serializable objects and not remote objects. the narow method has to be applied only for remote objects (actually it is the stub that will be received on the client side).

so you modify your code as,

Collection a = home.findAllCountries(); Iterator i = a.iterator(); while (i.hasNext()) {Object obj = i.next();Country country = (Country)obj;

this will definitely solve the problem.

regards

srini

vams00 at 2007-6-29 2:20:46 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...