Cast Exception from EJB returning Collection

Still can't understand why I'm getting a cast exception when I call an object method when the object has been returned in a Collection from an EJB.

The object CountryModel is a serialised object, returned in a Collection from home.findAllCountries.....

I understood RMI returns the object in serialised form, why does called the CountryModel object method getCountryName() cause a ClassCast method? This object casting works ok for a non-rmi application.

Collection boris = home.findAllCountries();

Iterator i = boris.iterator();

System.out.println( boris.size());//gets right results

while( i.hasNext()){

Object obj = i.next();

CountryModel leModel = (CountryModel)obj;

System.out.println(leModel.getCountryName());//causes ClassCastException

}

Any help appreciated. Thanks, lebo

[1037 byte] By [bocockli] at [2007-9-26 1:56:50]
# 1

> Still can't understand why I'm getting a cast

> exception when I call an object method when the object

> has been returned in a Collection from an EJB.

>

> The object CountryModel is a serialised object,

> returned in a Collection from

> home.findAllCountries.....

>

> I understood RMI returns the object in serialised

> form, why does called the CountryModel object method

> getCountryName() cause a ClassCast method? This

> object casting works ok for a non-rmi application.

>

> Collection boris = home.findAllCountries();

> Iterator i = boris.iterator();

> System.out.println( boris.size());

> e()); //gets right results

>

> while( i.hasNext()) {

> Object obj = i.next();

> CountryModel leModel = (CountryModel)obj;

> System.out.println(leModel.getCountryName());

> ame()); //causes ClassCastException

> }

>

>

> Any help appreciated. Thanks, lebo

>

Cast exceptions mostly occur when the same class is being loaded by two classloaders, and thus a conflict. I believe that the statement

CountryModel leModel = (CountryModel)obj;

is throwing the exception and not the method call.

Kindly try to check that out.

Moreover instead of using the iterator use the toArray method and then try to access the elements. This would give an indication as what is going wrong.

Kindly do the above and update the topic.

Regards

Rohit Parik

rohitparik@inidiatimes.com

RohitParik at 2007-6-29 3:12:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Hi Leonard,

I think your problem is related to casting only. Actually if you are using EJB 1.1 , my illustration would hold right. EJB1.1 uses RMI-IIOP protocol instead of RMI as in EJB1.0. Now how does this effect ? Since RMI is protocol for passing java objects , simply serialising the objects will do. But for RMI-IIOP the protocol is that of CORBA. so the type casting has to be done.

It is recommended that you use javax.rmi.PortableRemoteObject.narrow() method. I think it will help you to solve the problem

javahunt at 2007-6-29 3:12:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
In the method findAllCountries(), if you are using an ArrayList or a Vector then you need to cast the result like: Vector boris = (Vector)home.findAllCountries();hope this helps.
skuruganti at 2007-6-29 3:12:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

Guys, thanks for your feedback. Rohit, you're absolutely right, it is the cast

CountryModel leModel = (CountryModel)obj;

that's causing the ClassCast exception.

I've tried a number of options, .toArray(), Vectors and so on, and in every instance the CastException occurs when I try and cast to the CountryModel object. I've tried returning the results in an ArrayList rather than Collection, but still can't crack it. I know this Casting approach works in EJB1.0, but it seems not to in EJB1.1.

Using javax.rmi.PortableRemoteObject.narrow() and calling the remote object defeats the purpose. I want to be able to pass a controlled result set to the client through RMI as a collection of objects.

Ideas? Thanks, lebo

bocockli at 2007-6-29 3:12:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5
Hi lebo, Kindly explain as to why you wanna do that.Regards
RohitParik at 2007-6-29 3:12:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6

What javahunt says here is absolutely right, except it's not just recommended you use the narrow() method, you have to use this method.

EJB1.0 used Java RMI, thus applying a Java cast would be legitimate under EJB1.0. EJB1.1 uses Java RMI-IIOP, and has to conform to IIOP standards.

As IIOP accomodates many languages, some of which do not support casting, we cannot assume the return type can be cast to a Java object. Thus it is necessary to use javax.rmi.PortableRemoteObject.narrow().

In the O'Reilly EJB book, which covers this subject well, there are 6 instances identified where it is necessary to use this narrow() method. One is 'When an EJB object reference is obtained from a collection returned by a Home interface method', i.e. exactly what you are doing here.

So, as much as you might not like it, I'm afraid you have no choice!

HTH,

Neil.

neilr3 at 2007-6-29 3:12:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7

hi,

you are calling the finder method. the finder method always returns a collection of the remoteinterface stub objects which you will type cast to remote interface scope.

is CountryModel your remote object. i will tell a easy solution.

simply add a line,

System.out.println(i.next().getClass());

and identify the class name. this is the name you need to give for casting and left hand side.

if the class is a stub, give the parent interface name for casting and left hand side. this will definitely solve the problem.

regards

srini

vams00 at 2007-6-29 3:12:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 8
Hi, Pls update if you have found the solution of this problem as I'm facing the same problem in my app.Thanks in advancePradeep
psundriyal at 2007-6-29 3:12:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 9

Hi lebo,

try this :

while( i.hasNext()) {

CountryModel leModel = (CountryModel)i.next();

System.out.println(leModel.getCountryName());

}

I'm just not sure CountryModel is your EJB. As you probably know, findAllCountries is returning a collection of ejbs.

hope this helps,

Dimitar

dstavrakov at 2007-6-29 3:12:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...