| Author |
Casting EJBs in a Collection returned by a finder method
|
Mark Herschberg
Sheriff
Joined: Dec 04, 2000
Posts: 6037
|
|
I'm finding EJBs extremely annoying and/or counter-intuitive at the moment. Here's the problem. I have a CMP 2.0 entity EJB called LiveOrder which contains information about an order (runs on Weblogic 7.0). These are stored in an Oracle 9i DB. There are times when I wish to retrieve a sorted subset of these orders. Unfortunately GROUPBY won't be supported until the EJB 2.1 spec, and I don't want to start writing JDBC calls directly. Well, the obvious solution is to use Collections. I created a findBy method and passed in the EJB-SQL commands using JBuilder 8 Enterprise. This returns a Collection. So far so good. Now here's the great part, I just need to make that object (LiveOrder) implement the Comparable interface and then dump it in a TreeSet and it will do the sorting for me. Easy! (The level of sorting is a simple two level sort based on numbers, even easier then the example they give in the Collection's tutorial.) Here's where the problem comes in. I wrote the following code: There is a big problem. The Collection isn't of LiveOrder, but rather some class called LiveOrderBean_lg3vj2_ELOImpl -- one of those autogenerated EJB classes. This gives me a [i]ClassCastException. (Also, it was using the toString() of the new class and not the one I overrode in LiveOrder, but that's fixable by creating another method like getString().) So then I tried to be clever. I created a [/]LiveOrderComparableInterface[/i] as follows This had all the methods I needed to access. So now my sort method can look like The only problem is that I still get a ClassCastException! Even more interesting, when I got the interfaces after calling getClass().getInterfaces(), I found the following information: class: LiveOrderBean_lg3vj2_ELOImpl interfaces: LiveOrder weblogic.utils.PlatformConstants Serializable Surely others have been down this road before? Does anyone have any ideas? Do EJBs really suck this much, not allowing simple intuitive actions, or am I missing something? --Mark [ January 22, 2003: Message edited by: Mark Herschberg ]
|
 |
Chris Mathews
Ranch Hand
Joined: Jul 18, 2001
Posts: 2712
|
|
For the purpose of this discussion I going to assume you are using local interfaces. For multi-object finders defined in the entity bean�s local home interface, the result type is a Collection of objects implementing the entity bean�s local interface. Is LiveOrder the local interface or the bean implementation class? Sorry if the question seems to basic, I just don't want to overlook anything.
|
 |
Mark Herschberg
Sheriff
Joined: Dec 04, 2000
Posts: 6037
|
|
Wow you're fast. I was just thinking I'll bet the first question I'd get is "did I declare the methods in the local interface?" Everything is being done through local interfaces, as this is all way back end. Here's what I put in the JBuilder wizard (I know you're not a big fan): Bean name: LiveOrder Bean class: com.hbs.simtrade.server.trading.LiveOrderBean Local home interface class: com.hbs.simtrade.server.trading.LiveOrderHome Local interface class: com.hbs.simtrade.server.trading.LiveOrder Primary key class: com.hbs.simtrade.server.trading.LiveOrderPK (And to answer my question, all methods are in the local interface.) --Mark
|
 |
Mark Herschberg
Sheriff
Joined: Dec 04, 2000
Posts: 6037
|
|
I should probably be more explicit about the problem since I think I only implied it. The actually error comes from the following call: Where col is the Collection I got from the finder method. I'm thinking the problem is it doesn't realize that this is supposed to implement the Comparable interface. --Mark
|
 |
Mark Herschberg
Sheriff
Joined: Dec 04, 2000
Posts: 6037
|
|
Oh son of &*#^$!!! Even thought JBuilder made the Bean class implement the Comparable interface, and even though all methods were in the bean's interface, the bean's interface wasn't listed as supported the Comparable interface. Grrrr.... I'm becoming less and less of a JBuilder fan every day. --Mark
|
 |
Chris Mathews
Ranch Hand
Joined: Jul 18, 2001
Posts: 2712
|
|
Originally posted by Mark Herschberg: Grrrr.... I'm becoming less and less of a JBuilder fan every day.
I have an idea. So problem solved then? [ January 22, 2003: Message edited by: Chris Mathews ]
|
 |
Mark Herschberg
Sheriff
Joined: Dec 04, 2000
Posts: 6037
|
|
Yes, problem solved. I'm tempted. I don't think I'll switch half way through the project, with a big deadline in two months. But my next project, I am going to look into it. --Mark
|
 |
Chris Mathews
Ranch Hand
Joined: Jul 18, 2001
Posts: 2712
|
|
Originally posted by Mark Herschberg: I don't think I'll switch half way through the project, with a big deadline in two months.
Agreed. I did this once on a project a couple years ago and it was a real pain because we were not building with ANT (or any other build tool). If we were using a build tool separate from our IDE then it would not have been a problem, but we weren't ...
|
 |
 |
|
|
subject: Casting EJBs in a Collection returned by a finder method
|
|
|