-
1. Re: ClassCastException for remote acccess of entity bean
jboss2005_01 Apr 7, 2006 10:05 AM (in response to pieterjan)Hi PieterJan,
Be carefull by putting application code in het lib folder. This is not the place to be for this kind of code. It should be under the deploy folder and only there unless you are using libraries used by multiple applications to share the same code. In this case you should put it into your configuration's lib folder instead of the server's lib folder.
You could add one.ear to two.ear to have all code available, but there are much better ways to do this.
By adding only the EJB class files needed by one.ear from two.ear into one.ear. There is, as far as I remember, a way to provide them in a bootstrap mechanism, called RMI classloading but I have to admit that I am not familiar with this way of working.
Anyone who can explain this approach? Feel free to do so, I'm also very interested in this solutions too :-)
The ClassCastException might get caused by two reasons:
1. First you retrieve a reference to a local interface while a remote interface is needed in your application or vice versa. Since you run on the same server, use local interface to retrieve EJB references instead of remote interfaces. Therefor check if you are retrieving a remote interace from your InitialContext and using a local interface in your code. Remember that if you use remote interface, a standard cast will not work, you have to use the narrowing mechanism on the PortableRemoteObject to cast successfully.
2. The type of class in the RMI registry and the one you want to use in your code do not match. Probably there is something wrong in your deployment descriptor where you specify the JNDI name together with the appropriate class. Check the objects types in the JNDI registry. (tip : JNDIView in the jmx-console)
I hope this might solve your problem and you get things up and running... :-)
Regards,
Kurt -
2. Re: ClassCastException for remote acccess of entity bean
pieterjan Apr 10, 2006 9:36 AM (in response to pieterjan)Can an entity bean have a local and remote interface or doest that only apply to session beans?
Anyway, I'm accessing the entity bean through an SLSB (using the dao-pattern found in Hibernate in action). A call to findAll().size() will return the correct amount of records in my table. But the call findById() throws the ClassCastException when I try to assign the returnvalue to an object of the entity bean's class.
Here is how my packages roughly look like:
One.ear:
- entity bean (to oracle, no interface)
- stateless session bean (dao implementation and interface)
Two.ear:
- stateless session bean (a facade that calls the dao from one.ear causing the exception at the cast)
So far no luck using PortableRemoteObject.
Thanks for the help Kurt ... but keep it coming ;-)
Or any idea where I can find an example where multiple ear files are deployed where this kind of remoting is used? -
3. Re: ClassCastException for remote acccess of entity bean
jboss2005_01 Apr 11, 2006 3:51 AM (in response to pieterjan)Local and remote interface apply to all kinds of EJBs, entity and session. Are you using BMP or CMP for your entity beans? Under normal circumstances there can be up to 5 classes defining your EJB : 2 home interfaces (defining create, finders,...), 2 remote interfaces (containing your business methods) and the bean implementation class. Both remote and home interfaces have a local variant and a "remote" variant (see below).
The return type for your finder method should be Collection, Iterator or your EJB itself and depends on the type of interface, the local accessable and the remote accessable.
Typically the home interface (accessable outside the container) looks something likepublic interface TestEJBHome extends javax.ejb.EJBHome { public Test.TestEJB findByPrimaryKey(java.lang.String aKey) throws javax.ejb.FinderException, java.rmi.RemoteException; public TestEJB findById() throws java.rmi.RemoteException, javax.ejb.FinderException; }
The local home interface (accessable only from within the container, other EJBs, servlets,...) looks likepublic interface LocalTestEJBHome extends javax.ejb.EJBLocalHome { public Test.LocalTestEJB findByPrimaryKey(java.lang.String Key) throws javax.ejb.FinderException; public LocalTestEJB findById() throws javax.ejb.FinderException; }
Notice the difference in return type for the different interfaces !!
Local interfaces are preferred if you only want to access your EJB from within the container (application server) and not from the outsite (swing client application for example). In fact, local interface are more performant since they don't have RMI overhead. So think well, before you start coding ;-)
Since you are accessing your entity beans from within session beans in your project, local interfaces will be sufficient. What you get in your code is typically something likeLocalTestEJBHome home = context.lookup("ejb/TestEJB"); LocalTestEJB bean = home.create();
In your deployment descriptors, ejb/TestEJB must be linked to the LocalTestEJB off course.
This is probably not much information, but if you like I could take a look at a piece of code, specifically the part where you access your entity bean and the entity bean itself.
The corresponding deployment descriptor looks like<ejb-jar> <enterprise-beans> <entity> <display-name>TestEJB</display-name> <ejb-name>TestEJB</ejb-name> <home>Test.TestEJBHome</home> <remote>Test.TestEJB</remote> <local-home>Test.LocalTestEJBHome</local-home> <local>Test.LocalTestEJB</local> <ejb-class>Test.TestEJBBean</ejb-class> <persistence-type>Container</persistence-type> <prim-key-class>java.lang.String</prim-key-class> <reentrant>False</reentrant> <abstract-schema-name>TestEJB</abstract-schema-name> <cmp-field> <field-name>defaultField</field-name> </cmp-field> <primkey-field>defaultField</primkey-field> <query> <query-method> <method-name>findById</method-name> <method-params/> </query-method> <ejb-ql>SELECT Object(o) FROM TestEJB o</ejb-ql> </query> </entity> </enterprise-beans> </ejb-jar>
I don't have an example with me with multiple EARs, perhaps the J2EE tutorial found at Sun might be something useful for you...
I myself also run/ran into ClassCastExceptions, but mostly they were caused by JNDI reference errors in my descriptors.
Isn't it irritating to notice that the computer is always right ;-)
Cheers,
Kurt
Good luck in your search :-)