• Post Reply Bookmark Topic Watch Topic
  • New Topic

Polymorphism in EJB  RSS feed

 
Navaid Ahmed
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is seen that EJBs dont show polymorphic behaviour. Is there any way out. Need ur suggestions! :roll:
 
Byron Estes
Ranch Hand
Posts: 313
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Navaid,
I understand what you are saying, but there are some ways to work around it.
Take a look at the PDF file from a session at JavaOne for some ideas:
Link to presentation
Do a search for polymorphism.
Regards,
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO if you find you want polymorphism from an EJB, then you are using EJB's incorrectly. Remember EJB design Rule #1 -- EJB's are NOT domain objects. What is your design problem, and why do you think you need polymorphism in an EJB?
Kyle
 
Dave Coombes
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
On this subject, on the application I am working on, we model customers as entity beans. Customers are either Corporate Customers or Personal Customers which have some properties in common and then some specific properties.
Because the finder methods on an entity bean are non-polymorphic and the specific implentation class is defined in the deployment descriptor, we are in the situation where we have three entity beans defined in the DD, one for each specific type of Customer and one for 'CustomerBase' which we use if you want to find a customer irrespective of its type.
This does not seem ideal and it would be nice if you we had one Customer EJB whose home interface returned specific types which the client could then downcast if they wanted to call specific methjods on the sub-class.
Am I missing something?
Thanks
Dave
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the question is -- if your base home returned a heterogenous collection of EJB's, how would you know which subclass to downcast each instance to? You could try to use instanceof with the remote interfaces, but that gets nasty. Or, you could have some sort of "discriminator" attribute, but that gets nasty too.
There are two basic ways of handling inheritance in database mappings; either you have all attributes in all subclasses drawn from a single table (with a discriminator column added in) and then do separate SELECT statements, or you use what is called "root-leaf" mapping, where the base class maps to a customer "root" table and the subclasses's unique attributes map to "leaf" tables tha share the same primary key with the root table.
The issue is that neither of these approaches is supported by the Entity model. Therefore, unless you happen to be lucky and work in an environment like WebSphere Studio where this can be handled for you, you should just avoid the issue. Don't use Entity beans here. Use DAO's and do it yourself.
Alternatively, this could be handled in EJB 2.0 with dependent objects -- If you're using the approach of storing all attributes in a single table, then you could have different dependent objects for each subtype.
The fact is that EJB's, even Entity EJB's are not domain objects. They are database wrappers. Admit that, and often the problems become more understandable.
Kyle
 
Navaid Ahmed
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to use Enity Bean here in this situation and I know that they are used as a wrapper of tables. I am implementing Reference Information Model in EJB. The solution I found is to use Strategy Pattern. Every Strategy Object is selected for every differnt kind of entity.
Looking forward for ur replies !
Navaid. :roll:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!