File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes [EJB3] Stateless Session Bean & Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "[EJB3] Stateless Session Bean & Inheritance " Watch "[EJB3] Stateless Session Bean & Inheritance " New topic
Author

[EJB3] Stateless Session Bean & Inheritance

antonio each
Greenhorn

Joined: Apr 05, 2011
Posts: 9
Hi all

I'm developing a web application with: JSF,EJB3 and JPA (without DAO Layer).

So the idea is to create a FATHER ABSTRACT SESSION BEAN which expose all principal CRUD commons operation such as INSERT,REFRESH,DELETE ENTITY etc:



So each SPECIALIZED SESSION EJB needs to extends this AbstractBaseService.



Ok...This is what we have tryed ,but WEBLOGIC application server fails to run:
Reading ejb3 specification on sun site i have understood why:

Specification says:
"A Session Bean class annotated with @Stateless can't be ABSTRACT and can't INHERIT from another @Stateless session bean"

My answer is:

Is there a way to accomplish my goal?

I thought:

and if i don't declare my AbstractBaseService as @Stateless session bean....???
What happen to @Stateless session bean which extends it?

Any help is appreciate!

Antonio
Victor Herrera
Greenhorn

Joined: Apr 08, 2011
Posts: 5
Hi, you can safely remove the Stateless annotation in the abstract class. Inheritance will provide you with the functionality of the parent class but it doesn't need to be an EJB. For the parent interface you can remove Local annotation too.

Think that the the abstract base service can not be a working component by their own.

Nico Van Brandt
Ranch Hand

Joined: Mar 31, 2011
Posts: 66

That is true.

You cannot define a bean abstract or final because the container needs full control of them.
But because beans are like POJOs you are supposed to inherit from other POJOs.


Oracle Java SE6 Certified Programmer
Oracle Java EE5 Certified Web Component Developer
antonio each
Greenhorn

Joined: Apr 05, 2011
Posts: 9
Thank you for clear explanation!

I confirm all what you have posted here and I'm very happy for the architecture of my application!

Antonio
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Antonio, could you tell me how did you access the EntityManager from your non-EJB superclass (AbstractBaseService)? Or maybe you didn't use the EntityManager for these CRUD operations?

Cheers!


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9343
    
111

Pedro Kowalski wrote:Antonio, could you tell me how did you access the EntityManager from your non-EJB superclass (AbstractBaseService)? Or maybe you didn't use the EntityManager for these CRUD operations?

Cheers!


1) If the base class is abstract he won't be able to create an instance of that. So accessing from the base class in that case is ruled out. i.e. you'll need to lookup the bean which extends that class and then let that bean instance access the injected entitymanager.

2) If the base class is not abstract and itself is a bean and also has a subclass which is a bean, then there are 2 bean implementation classes. The user can lookup either of them separately and use the injected entity manager.

[My Blog] [JavaRanch Journal]
antonio each
Greenhorn

Joined: Apr 05, 2011
Posts: 9
Jaikiran Pai wrote:
Pedro Kowalski wrote:Antonio, could you tell me how did you access the EntityManager from your non-EJB superclass (AbstractBaseService)? Or maybe you didn't use the EntityManager for these CRUD operations?

Cheers!


1) If the base class is abstract he won't be able to create an instance of that. So accessing from the base class in that case is ruled out. i.e. you'll need to lookup the bean which extends that class and then let that bean instance access the injected entitymanager.

2) If the base class is not abstract and itself is a bean and also has a subclass which is a bean, then there are 2 bean implementation classes. The user can lookup either of them separately and use the injected entity manager.


Hi Pedro, The point 1 of jakiran post is the way:
the BASE bean is abstract,so it never can be instantiated! Each bean which extends base class retrieve an instance of entity manager to perform operations with db.

Bye
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Thanks Jaikiran and Antonio!

I didn't know that EJB's let you do so gnarly stuff! So, take a look at this example:



So, right now in my code I can use:



And invoke all CRUD operations and business methods - all with CRUD operations implementation reusing.

Is this approach valid, or does it have any flaws? The EntityManager in each of these fields is the same object, so I guess it's fine, right?

By the way - I'm really suprised that the EJB container will inject EntityManager into GenericCrudBean's private field even if it's not annotated as EJB. Really nice!

Cheers and thanks in advace!
Christian Gosch
Greenhorn

Joined: May 15, 2013
Posts: 4
Look at the quote below, comments on EntityManager declarations are added by me:

Piotr Nowicki wrote:





I wonder if the "concrete EM" declared in ConcreteServiceBean is necessary!

What happens if no EM is declared in ConcreteServiceBean ? The EM declared in GenericCrudBean should be visible also here?

Thanks for useful answers (because this "architecture" fully matches what I am doing also :-) )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [EJB3] Stateless Session Bean & Inheritance
 
Similar Threads
Help EJB3
Subclassing rules for Session beans
javax.naming.NamingException: Could not dereference object
implementing same interface by more than one beans (glashfish v2 + netbeans 6.7)
Session EJB 3.1 inheritance