Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJB3 - Inheritance and polymorphism in beans

 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon/Raghu,

In EJB2.x i had read in some articles that inheritance in beans (one bean extending from another) was not recommended (however, i cant say whether the article was a very credible one). But, i hadnt seen any examples where i could see bean extending from some other bean. I dont know what was the exact reason why this was considered bad in EJB2.x. However in the EJB3 spec i found this statement in the "Whats new in EJB3" section:

Support for light-weight domain modeling, including inheritance and polymorphism.


So does this mean one bean extending some other bean is no longer an issue in EJB3? If so, what prevented this behaviour in EJB2.x?

Also, i would be more than happy if i could find some example, of this sort of inheritance of beans, somewhere - maybe even in your book
 
JigaR Parekh
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With 2.1 Inheritance was not natural as there was no POJO with CMP,
I dont think that spec says not to use Inheritance. I have seen sun blueprint pattern which has BMP and CMP in same class where BMP extends from CMP and you can easily switch between implementation.

But with 3.0 now as there is POJO so Inheritance can be achived very easily and it will be natural.

Below code i found from one slide show from EJB 3, Here there are three entity
1) Employee with ID attribute
2) FullTime employee with one more attribute salary
3) Parttime employee with attribute rate


[ October 27, 2006: Message edited by: JigaR Parekh ]
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below code i found from one slide show from EJB 3, Here there are three entity
1) Employee with ID attribute
2) FullTime employee with one more attribute salary
3) Parttime employee with attribute rate


Thanks JigaR for that example.

In EJB3 there are no longer any entity *beans*, there are just entities. The code that you mentioned looks logical when applied to these entities. My question is more about applying inheritance in session beans.
[ October 27, 2006: Message edited by: Jaikiran Pai ]
 
Jon Wetherbee
author
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jaikiran-
There is nothing preventing you from using Java inheritance when implementing your session beans, or your business interfaces. I haven't tried it, but if you were to have one class annotated @Stateless, say, extend another class annotated @Stateful (or @Stateless!), I think they would get deployed as separate session beans. For the subclass session bean, the @Stateful/@Stateless annotation on its base class would be ignored, and it would simply treat it as an ordinary base class.

Since there is no notion of persistent state, only session state, you don't run into issues with polymorphism, or in this case, observing session bean behavior through the super class that is attributed to its session bean meta data.

I think what JigaR was pointing out is that, in general, inheritance is much more interesting in the JPA realm, where you can choose from several built-in inheritance mapping strategies, and you can have polymorphic queries, relationships, etc.

Regards,
Jon
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by : Jon Wetherbee
There is nothing preventing you from using Java inheritance when implementing your session beans, or your business interfaces. I haven't tried it, but if you were to have one class annotated @Stateless, say, extend another class annotated @Stateful (or @Stateless!), I think they would get deployed as separate session beans. For the subclass session bean, the @Stateful/@Stateless annotation on its base class would be ignored, and it would simply treat it as an ordinary base class.

Since there is no notion of persistent state, only session state, you don't run into issues with polymorphism, or in this case, observing session bean behavior through the super class that is attributed to its session bean meta data.


Thanks Jon, for the detailed explanation. Just wanted to get these things cleared from you guys.

Originally posted by : Jon Wetherbee
I think what JigaR was pointing out is that, in general, inheritance is much more interesting in the JPA realm, where you can choose from several built-in inheritance mapping strategies, and you can have polymorphic queries, relationships, etc.


I agree.

Thanks again Jon and JigaR. Your answers helped.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic