Reputation: 1
Sorry for my english, I've a war project with cdi 1.0, deltaspike 0.5 and primefaces 4.0, when I'm trying to use @Respository with @EntityManagerConfig because I've two produces for entityManager with diferent @Qualifiers, I'm using glassfish 4 for application server this is de code for Repository, Produces and EntityManagerResolver:
Repository:
@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;
}
}
Produces:
@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();
}
}
}
When I try to use the repository with @inject I get the follow error:
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)
Does anyone know why this error happens?
Upvotes: 0
Views: 3318
Reputation: 106
Your EM resolver is getting called, but the problem is when trying to inject to
@Inject
@HbsWeb
private EntityManager hbsWebEntityManager;
Due to a resolution problem between : createHbsWebEntityManager() and createHbsEntityManager()
How are the @HbsWeb and @Hbs defined? are they related?
Upvotes: 0
Reputation: 12865
Going by your stacktrace
org.apache.deltaspike.data.impl.handler.EntityManagerLookup.lookupFor(EntityManagerLookup.java:51)
and taking a look at the sources of DeltaSpike 0.5, I'd say this is a bug in the 0.5 release, since the EntityManager
obtained from the EntityManagerResolver
is never used.
Try again with a DeltaSpike 0.6-SNAPSHOT - there are many improvements in the Data module, and this particular piece of code looks more correct now.
Upvotes: 0