com.portal.dao.EmployeeDAOImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.portal.dao.EmployeeDAOImpl.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 com.portal.dao;

import java.util.List;
import com.portal.entity.Employee;
import org.hibernate.Query;
import org.hibernate.SessionFactory;

/**
 *
 * @author Evgen
 */
public class EmployeeDAOImpl implements EmployeeDAO {

    private SessionFactory sessionFactory;

    public EmployeeDAOImpl(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public void saveEmployee(Employee employee) {
        sessionFactory.getCurrentSession().saveOrUpdate(employee);
    }

    @Override
    public Employee loadEmployee(int id) {
        Employee employee = null;
        try {
            employee = (Employee) sessionFactory.getCurrentSession().get(Employee.class, id);
        } catch (Exception e) {

        }
        return employee;
    }

    @Override
    public void deleteEmployee(int id) {
        Query query = sessionFactory.getCurrentSession().createQuery("delete from Employee where id=:id");
        query.setInteger("id", id);
        query.executeUpdate();
    }

    @Override
    public List<Employee> loadEmployes() {
        return sessionFactory.getCurrentSession().createQuery("from Employee E ORDER BY E.department, E.id").list();
    }

}