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?