Error with Hibernate when running server

Asked

Viewed 81 times

1

When running wildfly I have the following error snippet 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 he 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);
    }

}

From what I understood lack dependencies yet. Then I could not walk anymore. Someone can help?

  • It seems to me that cdi is unable to inject Entitymanager into your dao, you have noted the method that generates Entitymanager with @Produces?

  • @Geferson was exactly that. If you want to formalize the answer, I will then validate... Thank you.

1 answer

1


The injection of CDI dependencies, works basically by linking "Producer" x "Dependent" in your DAO, in this line:

@Inject
EntityManager em;

You were asking the server to inject an instance of Entitymanager, but for this to work, you would need to inform the CDI what is the Producer/Generator method of this dependency.

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

How To Solve?

In your method that generates Entitymanager instances, write down with @Produces.

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

Done this, solves the problem.

On the java application servers (Glassfish, JBOSS, etc.), you can also Inject an Entitymanager directly without writing the method, thus:

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

So you don’t need to use @Inject/Produces.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.