Search code examples
design-patternsjakarta-eejpaejbpolymorphism

JPA Inheritance and EJB polymorphism


I have a Java EE 6 web application using container-managed transactions and container-propagated Persistence Context through

@PersistenceContext
private EntityManager em;

In the JPA layer I have an inheritance strategy, where MyExtendedClassA and MyEntendedClassB both extend abstract MyClass.

I use stateless facade service classes for implementing the data access through the methods find, findAll, merge, persist, remove:

@Stateless
public class MyExtendedClassAFacade {
    @PersistenceContext
    private EntityManager em;
    public void persist(MyExtendedClassA a) {
        // ...
    }
    // other methods
}

So far, so good. Now I have to implement a polymorphism on the behavior of the extended classes. This behavior consists in managing some other entities in the database, so needs the PersistenceContext (and hence I need to use other Stateless EJBs):

@Stateful
public class MyBean {

    @EJB
    private MyClassFacade myClassFacade;   // stateless facade service class

    public void doSomething() {
        for (MyClass c : myClassFacade.findAll()) {
            // here I need to perform operations on the db. 
            // The implementation differs on each extended class.
            // I want to avoid a brute-force coding like:
            if (c.getClass().equals(MyExtendedClassA.class)) {
                @EJB MyExtendedClassAFacade myClassFacadeA;
                myClassFacadeA.doSomething((MyExtendedClassA) c);
            } else if (c.getClass().equals(MyExtendedClassB.class))
                @EJB MyExtendedClassBFacade myClassFacadeB;
                myClassFacadeB.doSomething((MyExtendedClassB) c);
            }
            // Instead, I would like to write something like:
            @EJB AnotherStatelessBean asb;
            asb.doSomething(c);
        }
    }
}

Is there any abstraction pattern I can use for this goal?


Solution

  • This is an EJB Polymorphism and can be applied to other situations. It doesn't relate to JPA inheritance directly.

    The trick is to use @Local interface for EJBs:

    @Local
    public interface MyClassFacadeInterface {    
        public void doSomething(MyClass c);
    }
    

    and let the existing Facade Stateless beans for the extended classes implement that interface.

    After that, the Facade stateless implementation classes must be looked for using InitialContext.lookup("java:module/MyExtendedClassAFacade"). Here, the trick is to give the facade classes names that are related to the entity classes in order to facilitate the lookup. The code to be used in the business layer is:

    public void doSomething() {      
        for (MyClass c : myClassFacade.findAll()) {      
            String lookupName = getNameFromClassName(c.getClass().name());
            MyClassFacadeInterface myInt =  (MyClassFacadeInterface) new InitialContext().lookup(lookupName);
            myInt.doSomething(c);
        }      
    }