dev.archiecortez.jfacelog.services.EmployeeDTO.java Source code

Java tutorial

Introduction

Here is the source code for dev.archiecortez.jfacelog.services.EmployeeDTO.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 dev.archiecortez.jfacelog.services;

import dev.archiecortez.jfacelog.entities.DTREmployee;
import dev.archiecortez.jfacelog.entities.DTREmployee;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author Administrator
 */
@Component
public class EmployeeDTO {

    private final EntityManagerFactory emf;
    private final EntityManager em;

    @Autowired
    public EmployeeDTO(EntityManagerFactory entityManagerFactory) {
        this.emf = entityManagerFactory;
        em = this.emf.createEntityManager();
    }

    public void saveEmployee(DTREmployee emp) {
        em.getTransaction().begin();
        em.persist(emp);
        em.getTransaction().commit();
    }

    public List<DTREmployee> listEmployees() {
        Query q = em.createQuery("select e from DTREmployee e");
        return q.getResultList();
    }

    public void deleteEmployee(DTREmployee emp) {
        em.getTransaction().begin();
        em.remove(emp);
        em.getTransaction().commit();
    }

    public void updateEmployee(DTREmployee emp) {
        em.getTransaction().begin();
        em.merge(emp);
        em.getTransaction().commit();
    }
}