注入DAO时CDI:ProxyObject ClassCastException

时间:2015-04-22 17:59:25

标签: java-ee jpa cdi websphere-8

我正在尝试使用Websphere 8.0中的CDI 1.0来实例化DAO类。在我的Workspace中,我有一个位于EJB项目中的EJB类,它调用位于DAO项目中的DAO类。我跟着使用Producer方法的this example(葡萄牙语,对不起)。这些是类:

DAO课程(在DAO项目中):

public class NaturezaDAO extends BaseDAO<NaturezaORM> {

    @Inject
    public NaturezaDAO(EntityManager em) throws DAOException {
        super(NaturezaORM.class, em);
    }

    // ...

}

Producer类(在DAO项目中):

import java.io.Serializable;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

/**
 * code extracted from 
 * http://blog.caelum.com.br/acessando-multiplos-bancos-de-dados-com-jpa-e-cdi/
 */
@ApplicationScoped
public class EntityManagerProducer implements Serializable {

    @PersistenceUnit
    private EntityManagerFactory factory;

    @RequestScoped
    @Produces
    public EntityManager createEntityManager() {
        System.err.println("\t\tEntityManagerProducer.createEntityManager()");
        return factory.createEntityManager();
    }

    public void closeEntityManager(@Disposes EntityManager manager) {
        System.err.println("\t\tEntityManagerProducer.closeEntityManager()");
        if (manager.isOpen()) {
            manager.close();
        }
    }

}

EJB类(在EJB项目中):

@Stateless
public class NaturezaServiceBean extends BaseSB implements NaturezaService {

    @Inject
    private NaturezaDAO naturezaDAO;

    @Override
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public List<NaturezaORM> listNaturezasPropositura() throws BusinessException, EntityNotFoundException {
        try {
            return this.naturezaDAO.listByTipoAtivoDeputado(
                TipoNaturezaEnum.ME, SimNaoEnum.S, SimNaoEnum.S);
        } catch (ObjectNotFoundDAOException e) {
            throw new EntityNotFoundException(e);
        } catch (DAOException e) {
            throw new BusinessException(e);
        }
    }

    // ...

}

初始化服务器时,显然没有问题。但是,当我调用EJB方法时,我得到了这个例外:

[22/04/15 14:39:26:838 BRT] 0000001a InjectInjecti E   CWOWB0102E: Ocorreu um erro do JCDI: org.javassist.tmp.java.lang.Object_$$_javassist_0 incompatible with javassist.util.proxy.ProxyObject
[22/04/15 14:39:26:839 BRT] 0000001a BusinessExcep E   CNTR0019E: EJB threw an unexpected (non-declared) exception during invocation of method "listNaturezasPropositura". Exception data: javax.ejb.EJBException: Injection failure; nested exception is: java.lang.ClassCastException: org.javassist.tmp.java.lang.Object_$$_javassist_0 incompatible with javassist.util.proxy.ProxyObject
java.lang.ClassCastException: org.javassist.tmp.java.lang.Object_$$_javassist_0 incompatible with javassist.util.proxy.ProxyObject

我不确定它是否有任何区别,但在两个项目中beans.xml目录中都有META-INF个文件。

关于这里缺少什么的任何建议?

谢谢,

Rafael Afonso

0 个答案:

没有答案