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

