Error with hibernate when running server

1

When running wildfly I have the following error excerpt in the log:

15:46:18,297 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-1) MSC000001: Failed to start service jboss.deployment.unit."evolutionary.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."evolutionary.war".WeldStartService: Failed to start service
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type EntityManager with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject br.com.evolutionary.negocio.HabilidadeDAO.em
  at br.com.evolutionary.negocio.HabilidadeDAO.em(HabilidadeDAO.java:0)

    at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:359)
    at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:281)
    at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)
    at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:155)
    at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:518)
    at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:68)
    at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:66)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:63)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:56)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at org.jboss.threads.JBossThread.run(JBossThread.java:320)

The class it accuses of error is:

package br.com.evolutionary.negocio;

import java.util.logging.Logger;

import javax.inject.Inject;
import javax.persistence.EntityManager;

import br.com.evolutionary.modelo.Habilidade;

public class HabilidadeDAO implements DAO<Habilidade, String> {

    @Inject
    EntityManager em;
    @Inject
    private Logger log;


    public Habilidade insert(Habilidade x) throws Exception {
    log.info("Persistindo " +x);
        em.persist(x);
        return x;
    }

    public Habilidade update(Habilidade x) throws Exception {
        log.info("Atualizando..." +x);
        return em.merge(x);

    }

    public void delete(Habilidade x) throws Exception {
        em.remove(x);

    }

    public Habilidade find(String y) throws Exception {
        log.info("Procurando...."+y);
        return em.find(Habilidade.class, y);
    }

}

So I understand that there are no dependencies yet. Then I could not walk anymore. Can anyone help?

    
asked by anonymous 30.05.2016 / 20:50

1 answer

1

Injection of CDI dependencies, basically works by linking "Producer" vs. "Dependent" on your DAO in this line:

@Inject
EntityManager em;

You were prompting the server to inject an instance of EntityManager, but for this to work, you would need to tell the CDI which is the Producer / Generator method of that dependency.  

Cdi traverses all injectable objects and checks if any of them meet the requested dependency.

  

How to resolve?

In your method that generates instances of EntityManager, note with @Produces.

@Produces
public EntityManager getEM(){...}

Done that, solve the problem.

In Java application servers (Glassfish, JBOSS, etc.), you can also inject an EntityManager directly without writing the method, like this:

@PersistenceContext(unitName = "NomeDoPU")
private EntityManager em;

So you do not need to use @Inject / Produces.

    
31.05.2016 / 14:12