Opresor
Opresor

Reputation: 93

No qualifying bean of type 'javax.persistence.EntityManager' available:

I'm getting this error on launching my web app and I don't really get what is the problem, full track:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'usuarioController' defined in file [C:\Users\Carlos\eclipsejee-workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\RedSocial\WEB-INF\classes\es\unex\cum\mydai\redsocial\controller\UsuarioController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'usuarioServiceImpl': Unsatisfied dependency expressed through method 'setUsuarioDAO' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'usuarioDAOImpl' defined in file [C:\Users\Carlos\eclipsejee-workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\RedSocial\WEB-INF\classes\es\unex\cum\mydai\redsocial\dao\UsuarioDAOImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManager' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

Those are the classes:

UsuarioController

package es.unex.cum.mydai.redsocial.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import es.unex.cum.mydai.redsocial.services.UsuarioService;
import es.unex.cum.mydai.redsocial.vo.UsuarioVO;

@Controller
public class UsuarioController {
    private final UsuarioService usuarioService;

    @Autowired
    public UsuarioController(UsuarioService usuarioService) {
        this.usuarioService = usuarioService;
    }

    @RequestMapping(value = "/registro", method = RequestMethod.GET)
    public String registro(ModelMap model) {
        UsuarioVO usuario = new UsuarioVO();
        model.put("usuario", usuario);
        return "registro";
    }
}

UsuarioServiceImpl

package es.unex.cum.mydai.redsocial.services;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import es.unex.cum.mydai.redsocial.dao.UsuarioDAO;
import es.unex.cum.mydai.redsocial.vo.UsuarioVO;

@Component
public class UsuarioServiceImpl implements UsuarioService {

    private UsuarioDAO usuarioDAO;

    public UsuarioServiceImpl() {}



    public UsuarioDAO getUsuarioDAO() {
        return usuarioDAO;
    }


    @Autowired
    public void setUsuarioDAO(UsuarioDAO usuarioDAO) {
        this.usuarioDAO = usuarioDAO;
    }



    public UsuarioVO insertarUsuario(UsuarioVO usuario) {
        return this.usuarioDAO.create(usuario);
    }

    public UsuarioVO findbyId(Long id) {
        return this.usuarioDAO.read(id);
    }

    public UsuarioVO updateUsuario(UsuarioVO usuario) {
        return this.usuarioDAO.update(usuario);
    }

    public void borrarUsuario(UsuarioVO usuario) {
        this.usuarioDAO.delete(usuario);

    }

    public Set<UsuarioVO> findbyName(String name) {
        return this.usuarioDAO.findByName(name);
    }

    public UsuarioVO login(String username) {
        return this.usuarioDAO.login(username);
    }

}

UsuarioDAOImpl

package es.unex.cum.mydai.redsocial.dao;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Table;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import es.unex.cum.mydai.redsocial.vo.UsuarioVO;

@Component
@Transactional
public class UsuarioDAOImpl implements UsuarioDAO {
    @PersistenceContext
    protected EntityManager entityManager;

    public UsuarioDAOImpl(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public UsuarioVO create(UsuarioVO usuario) {
        this.entityManager.persist(usuario);
        return usuario;
    }

    public UsuarioVO read(Long id) {
        return this.entityManager.find(UsuarioVO.class, id);
    }

    public UsuarioVO update(UsuarioVO usuario) {
        this.entityManager.merge(usuario);
        return usuario;
    }

    public void delete(UsuarioVO usuario) {
        usuario = this.entityManager.merge(usuario);
        this.entityManager.remove(usuario);
    }

    @SuppressWarnings("unchecked")
    public Set<UsuarioVO> findByName(String name) {
        Query query = entityManager.createQuery(
                "SELECT * FROM "+UsuarioVO.class.getAnnotation(Table.class).name()+" WHERE name LIKE ?1 OR username LIKE ?2", UsuarioVO.class);
        query.setParameter(1, "%"+name+"%");
        query.setParameter(2, "%"+name+"%");
        return new HashSet<UsuarioVO>(query.getResultList());
    }

    public UsuarioVO login(String username) {
        Query query = entityManager.createQuery(
                "SELECT * FRO "+UsuarioVO.class.getAnnotation(Table.class).name()+" WHERE username = ?1", UsuarioVO.class);
        query.setParameter(1, username);
        return (UsuarioVO) query.getSingleResult();
    }

}

Upvotes: 1

Views: 1046

Answers (1)

duffymo
duffymo

Reputation: 309018

The message is quite clear:

@PersistenceContext
protected EntityManager entityManager;

You're auto wiring an EntityManager without defining it anywhere. You need a class with an @Configuration annotation.

Upvotes: 2

Related Questions