JPA 继承和 EJB 多态性 [英] JPA Inheritance and EJB polymorphism

查看:34
本文介绍了JPA 继承和 EJB 多态性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

解决方案

This is an EJB Polymorfism, 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 InitialContex().lookup(lookupName);
        myInt.doSomething(c);
    }      
}      

这篇关于JPA 继承和 EJB 多态性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆