mx.com.misha.demo.trabajadornomina.persistence.TrabajadorDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for mx.com.misha.demo.trabajadornomina.persistence.TrabajadorDaoImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mx.com.misha.demo.trabajadornomina.persistence;

import java.util.List;
import mx.com.misha.demo.trabajadornomina.model.Trabajador;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Munek
 */
@Transactional
@Repository
public class TrabajadorDaoImpl implements TrabajadorDao {
    protected EntityManager em;

    @PersistenceContext(name = "TrabajadorNominaPU", type = PersistenceContextType.TRANSACTION)
    public void setEntityManager(EntityManager em) {
        this.em = em;
    }

    @Override
    public void insertar(Trabajador t) {
        em.persist(t);
    }

    @Override
    public List<Trabajador> buscarTodos() {
        String query = "select t from Trabajador t";
        Query q = em.createQuery(query);

        return q.getResultList();
    }

    @Override
    public Trabajador buscarId(Long id) {
        return em.find(Trabajador.class, id);
    }

}