deltaspike repository entitymanagerresolver错误,两个生成entityManger

时间:2014-02-17 22:22:24

标签: cdi deltaspike

对不起我的英语,我正在使用cdi 1.0,deltaspike 0.5和primefaces 4.0进行战争项目,当我尝试将@Respository与@EntityManagerConfig一起使用时,因为我有两个用于不同@Qualifiers的entityManager产生,我将glassfish 4用于应用程序服务器,这是Repository,Produces和EntityManagerResolver的代码:

存储库:

@Repository(forEntity = User.class)
@EntityManagerConfig(entityManagerResolver = HbsWebEntityManagerResolver.class, flushMode = FlushModeType.AUTO)
public abstract class UserRepository extends
    AbstractEntityRepository<User, Integer> {

   @Query(named = User.BY_LOGIN)
   public abstract User findByLoginEqual(@QueryParam("login") String login);

}

EntityManagerResolver:

public class HbsWebEntityManagerResolver implements EntityManagerResolver{

    @Inject
    @HbsWeb
    private EntityManager hbsWebEntityManager;

    @Override
    public EntityManager resolveEntityManager() {       
        return hbsWebEntityManager;
}

}

产地:

@ApplicationScoped
public class EntityManagerProducer {

    @PersistenceUnit(unitName="HBS")
        private EntityManagerFactory hbsEntityManager;

    @PersistenceUnit(unitName="HBSWEB")
    private EntityManagerFactory hbsWebEntityManager;

    @Produces
    @Hbs    
    protected EntityManager createHbsEntityManager() {
        return hbsEntityManager.createEntityManager();
    }

    protected void closeHbsEntityManager(
            @Disposes @Hbs EntityManager entityManager) {
        if (entityManager.isOpen()) {
            entityManager.close();
        }
    }

    @Produces
    @HbsWeb 
    protected EntityManager createHbsWebEntityManager() {
        return hbsWebEntityManager.createEntityManager();
    }

    protected void closeHbsWebEntityManager(
            @Disposes @HbsWeb EntityManager entityManager) {
        if (entityManager.isOpen()) {
            entityManager.close();
        }
    }

}

当我尝试使用@inject的存储库时,我得到以下错误:

org.apache.deltaspike.data.impl.handler.QueryInvocationException: Exception calling Repository: [Repository=class    co.com.compuhelmac.hbs.repository.UserRepository_$$_javassist_0,method=findByLoginEqual],exception=class org.jboss.weld.exceptions.AmbiguousResolutionException,message=WELD-001318 Cannot resolve an ambiguous dependency between [Producer Method [EntityManager] with qualifiers [@Hbs @Any] declared as [[BackedAnnotatedMethod] @Produces @Hbs protected co.com.compuhelmac.hbs.database.EntityManagerProducer.createHbsEntityManager()], Producer Method [EntityManager] with qualifiers [@HbsWeb @Any] declared as [[BackedAnnotatedMethod] @Produces @HbsWeb protected co.com.compuhelmac.hbs.database.EntityManagerProducer.createHbsWebEntityManager()]]
    at org.apache.deltaspike.data.impl.handler.QueryHandler.invoke(QueryHandler.java:86)
    at org.apache.deltaspike.partialbean.impl.PartialBeanAbstractMethodHandler.invoke(PartialBeanAbstractMethodHandler.java:44)
    at org.apache.deltaspike.partialbean.impl.MethodHandlerProxy.invoke(MethodHandlerProxy.java:35)
    at com.sun.proxy.$Proxy549.invoke(Unknown Source)
    at co.com.compuhelmac.hbs.repository.UserRepository_$$_javassist_0.findByLoginEqual(UserRepository_$$_javassist_0.java)
    at co.com.compuhelmac.hbs.security.HbsLoginEJB.login(HbsLoginEJB.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4695)
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:630)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

Caused by: org.jboss.weld.exceptions.AmbiguousResolutionException: WELD-001318 Cannot resolve an ambiguous dependency between [Producer Method [EntityManager] with qualifiers [@Hbs @Any] declared as [[BackedAnnotatedMethod] @Produces @Hbs protected co.com.compuhelmac.hbs.database.EntityManagerProducer.createHbsEntityManager()], Producer Method [EntityManager] with qualifiers [@HbsWeb @Any] declared as [[BackedAnnotatedMethod] @Produces @HbsWeb protected co.com.compuhelmac.hbs.database.EntityManagerProducer.createHbsWebEntityManager()]]
    at org.jboss.weld.manager.BeanManagerImpl.resolve(BeanManagerImpl.java:1154)
    at org.jboss.weld.manager.BeanManagerImpl.getBean(BeanManagerImpl.java:798)
    at org.jboss.weld.bean.builtin.InstanceImpl.get(InstanceImpl.java:79)
    at org.apache.deltaspike.data.impl.handler.EntityManagerLookup.lookupFor(EntityManagerLookup.java:51)
    at org.apache.deltaspike.data.impl.handler.QueryHandler.createContext(QueryHandler.java:97)
    at org.apache.deltaspike.data.impl.handler.QueryHandler.invoke(QueryHandler.java:74)

有谁知道为什么会发生这种错误?

2 个答案:

答案 0 :(得分:0)

通过堆栈跟踪

org.apache.deltaspike.data.impl.handler.EntityManagerLookup.lookupFor(EntityManagerLookup.java:51)

并查看DeltaSpike 0.5的来源,我会说这是0.5版本中的一个错误,因为从EntityManager获得的EntityManagerResolver从未使用过。

再次尝试使用DeltaSpike 0.6-SNAPSHOT - 数据模块有许多改进,现在这段特殊代码看起来更正确。

答案 1 :(得分:0)

您的EM解析器正在调用,但问题是在尝试注入

@Inject
@HbsWeb
private EntityManager hbsWebEntityManager;

由于以下问题之间的解决问题:createHbsWebEntityManager()和createHbsEntityManager()

如何定义@HbsWeb和@Hbs?他们有关系吗?