简体   繁体   中英

CDI Generic DAO injection

I'm having problems with my project

Exception in thread "main" org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type JpaDAO with qualifiers @Default at injection point [BackedAnnotatedField] @Inject private teste.view.Principal.dao

JpaDAO(Just for test, not fully implemented) :

package teste.cdihibernate;

import java.util.List;
import javax.persistence.EntityManager;

public class JpaDAO<T> implements DAO<T>
{
    private EntityManager em;
    private final Class<T> classe;

    public JpaDAO(Class<T> classe, EntityManager em)
    {
        this.classe = classe;
        this.em = em;
    }

    @Override
    public void save(T entity)
    {
        em.persist(entity);
    }

    @Override
    public void update(T entity)
    {

    }

    @Override
    public void remove(T entity)
    {
        em.remove(entity);
    }

    @Override
    public T getById(Class<T> classe, Long pk)
    {
        return em.find(classe, pk);
    }

    @Override
    public List<T> getAll(Class<T> classe)
    {
        List<T> resultList = (List<T>) em.createQuery("select e from " + classe.getSimpleName() + " e").getResultList();
        return resultList;
    }

    @Override
    public T getByRestriction(Class<T> classe, String attribute, String filter)
    {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

My DAOFactory :

public class DAOFactory
{
    @Inject private EntityManager em;

    @SuppressWarnings({ "rawtypes", "unchecked" })    
    @Produces
    @Dependent
    public JpaDAO createJpaDAO(InjectionPoint point) throws ClassNotFoundException
    {
        ParameterizedType type = (ParameterizedType) point.getType();
        Class classe = (Class) type.getActualTypeArguments()[0];
        return new JpaDAO(classe, em);
    }
}

And my Principal.java :

@Inject private JpaDAO<Veiculo> dao;

What am I doing wrong?

The producer method's return type (JPaDAO) is not assignable to the required type for injection (JpaDAO<Veiculo>). I believe you need to add a type variable to your producer method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM