2
votes

I want to use in maven project an implementation of CDI+JSF+HIBERNATE+PRIMEFACES. my pom.xml is like this:

    <dependencies>
    <!-- PROJECT LOMBOK -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.6</version>
        <scope>provided</scope>
    </dependency>
    <!-- MYSQL -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.9</version>
    </dependency>
    <!-- HIBERNATE -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.3.Final</version>
    </dependency>
    <!-- CDI -->
    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <version>1.2</version>
    </dependency>
    <!-- SLF4j -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.12</version>
    </dependency>
    <!-- PRIMEFACES -->
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>5.2</version>
    </dependency>
</dependencies>

The structure of my project is like this, I use a generic interface when I implement in different Class. One interface that I implement in 3 classes IGenericDAO I implement it in UserDao + PersonDao + RoleDao like this

public interface IGenericDAO<T> {

void save(T object);

void update(T object);

T getObjectById(int id);

List<T> getAll();  }

the implementation is

public class UserDAOImpl implements IGenericDAO<User> {

@Override
public void save(User user) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();
        session.save(user);
        transaction.commit();
    } catch (Exception e) {
        if (transaction != null)
            transaction.rollback();
        throw e;
    } finally {
        session.close();
        HibernateUtil.getSessionFactory().close();
    }
}

@Override
public void update(User user) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();
        session.update(user);
        transaction.commit();
    } catch (Exception e) {
        if (transaction != null)
            transaction.rollback();
        throw e;
    } finally {
        session.close();
        HibernateUtil.getSessionFactory().close();
    }
}

@Override
public User getObjectById(int id) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        return (User) session.createCriteria(User.class).add(Restrictions.eq("id", id)).list().get(0);
    } catch (Exception e) {
        throw e;
    } finally {
        session.close();
        HibernateUtil.getSessionFactory().close();
    }
}

@SuppressWarnings("unchecked")
@Override
public List<User> getAll() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        return session.createCriteria(User.class).list();
    } catch (Exception e) {
        throw e;
    } finally {
        session.close();
        HibernateUtil.getSessionFactory().close();
    }
}}

like this I code the Service to, I use a Generic Interface and I implement the code in 3 classes separetaded like this:

public interface IGenericService<T> {

void save(T object);

void update(T object);

T getObjectById(int id);

List<T> getAll(); }

the implementation

@RequestScoped
public class UserServiceImpl implements IGenericService<User> {
@Setter
@Inject
private IGenericDAO<User> dao;

@Override
public void save(User user) {
    dao.save(user);
}

@Override
public void update(User user) {
    dao.update(user);
}

@Override
public User getObjectById(int id) {
    return dao.getObjectById(id);
}

@Override
public List<User> getAll() {
    return dao.getAll();
}}

And finally, the ManagedBean is like this:

@Named(value = "authentication")

@RequestScoped public class AuthenticationBean implements Serializable {

private static final long serialVersionUID = 2288439665206779666L;

@Getter
@Setter
private String message;

@Setter
@Inject
private IGenericService<User> userService;

@PostConstruct
public void init() {
    message = userService.getObjectById(1).getLastname();
}}

Everytime I run the glassfish server i have this error : Exception 0 : org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type IGenericDAO with qualifiers @Default at injection point [BackedAnnotatedField] @Inject private ma.salaried.service.impl.PaymentServiceImpl.dao

2
yes I am a begginner in CDI. But, If I understood correctly, I have to do Inject (Implementation) insted of Inject (Interface) ?Mahmoud
I try your proposition but there is no change Exactlly the error is : org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type IGenericDAO<User> with qualifiers Default at injection point [BackedAnnotatedField] Inject private ma.salaried.service.impl.UserServiceImpl.dao org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type IGenericDAO<Payment> with qualifiers Default at injection point [BackedAnnotatedField] @Inject private ma.salaried.service.impl.PaymentServiceImpl.daoMahmoud
org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type IGenericDAO<Payment> with qualifiers Default at injection point [BackedAnnotatedField] @Inject private ma.salaried.service.impl.PaymentServiceImpl.daoMahmoud
Exception 0 : org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type IGenericDAO<Employee> with qualifiers Default at injection point [BackedAnnotatedField] @Inject private ma.salaried.service.impl.EmployeeServiceImpl.daoMahmoud
I solve my problem by adding @RequestScoped in Implementation of DAO. and I Inject ithe interface not an implementation oh themMahmoud

2 Answers

1
votes

I solved this problem by adding @RequestScoped in implementation of DAO and Implementation od Service Like this (In all my DAO IMPL and Service Impl)

@RequestScoped public class EmployeeDAOImpl implements IGenericDAO<Employee> {

@Override
public void save(Employee Employee) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();
        session.save(Employee);
        transaction.commit();
    } catch (Exception e) {
        if (transaction != null)
            transaction.rollback();
        throw e;
    } finally {
        session.close();
        HibernateUtil.getSessionFactory().close();
    }
}

@Override
public void update(Employee Employee) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();
        session.update(Employee);
        transaction.commit();
    } catch (Exception e) {
        if (transaction != null)
            transaction.rollback();
        throw e;
    } finally {
        session.close();
        HibernateUtil.getSessionFactory().close();
    }
}

@Override
public Employee getObjectById(int id) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        return (Employee) session.createCriteria(Employee.class).add(Restrictions.eq("id", id)).list().get(0);
    } catch (Exception e) {
        throw e;
    } finally {
        session.close();
        HibernateUtil.getSessionFactory().close();
    }
}

@SuppressWarnings("unchecked")
@Override
public List<Employee> getAll() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        return session.createCriteria(Employee.class).list();
    } catch (Exception e) {
        throw e;
    } finally {
        session.close();
        HibernateUtil.getSessionFactory().close();
    }
}

}

and in service like this

@RequestScoped public class EmployeeServiceImpl implements IGenericService {

@Setter
@Inject
private IGenericDAO<Employee> dao;

@Override
public void save(Employee Employee) {
    dao.save(Employee);
}

@Override
public void update(Employee Employee) {
    dao.update(Employee);
}

@Override
public Employee getObjectById(int id) {
    return dao.getObjectById(id);
}

@Override
public List<Employee> getAll() {
    return dao.getAll();
}}
0
votes

I have met the same error, but with Wildfly and ejb functionality. The reason was - in web.xml file there was metadata-complete="true" attribute. With this attribute set to false the server does not scan ejb and servlet annotations. All functionality, managed with them not working.

What is interesting - cdi annotations do work correctly.

Solution: either remove it, or set to false.