com.bs.dao.SalaryDao.java Source code

Java tutorial

Introduction

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

import com.bs.model.SalaryModel;
import com.bs.modelPojo.SeslesReport;
import com.bs.pojo.Salary;
import com.bs.util.HibernateUtil;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

@Service
public class SalaryDao implements SalaryModel {

    @Override
    public boolean doInsertSalary(Salary salary) {
        Session s = HibernateUtil.getSessionFactory().openSession();
        try {
            s.beginTransaction();
            s.save(salary);
            s.getTransaction().commit();
            return true;
        } catch (Exception e) {
            return false;

        } finally {
            s.close();
        }
    }

    @Override
    public boolean doUpdateSalary(Salary salary) {
        Session s = HibernateUtil.getSessionFactory().openSession();
        try {
            s.beginTransaction();
            s.update(salary);
            s.getTransaction().commit();
            return true;
        } catch (Exception e) {
            return false;

        } finally {
            s.close();
        }
    }

    @Override
    public boolean doDeleteSalary(Salary salary) {
        Session s = HibernateUtil.getSessionFactory().openSession();
        try {
            s.beginTransaction();
            s.delete(salary);
            s.getTransaction().commit();
            return true;
        } catch (Exception e) {
            return false;

        } finally {
            s.close();
        }
    }

    @Override
    public List<Salary> findAllSalary() {
        Session s = HibernateUtil.getSessionFactory().openSession();
        try {
            s.beginTransaction();
            Query q = s.createQuery("From Salary");
            List<Salary> aList = q.list();
            s.getTransaction().commit();
            return aList;
        } catch (Exception e) {
            return null;
        } finally {
            s.close();
        }
    }

    @Override
    public List<Salary> findSalaryById(int salaryId) {
        Session s = HibernateUtil.getSessionFactory().openSession();
        try {
            s.beginTransaction();
            Query q = s.createQuery("From Salary  where salaryId=" + salaryId + "");
            List<Salary> aList = q.list();
            s.getTransaction().commit();
            return aList;
        } catch (Exception e) {
            return null;
        } finally {
            s.close();
        }
    }

}