com.qfix.vms.dao.impl.AccidentsDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.qfix.vms.dao.impl.AccidentsDaoImpl.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.qfix.vms.dao.impl;

import com.qfix.vms.dao.AbstractDao;
import com.qfix.vms.dao.AccidentsDao;
import com.qfix.vms.model.Accidents;
import java.io.Serializable;
import java.sql.Date;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Ruwani Jayarathna
 */
@Repository("accidentsDao")
public class AccidentsDaoImpl extends AbstractDao<Serializable, Accidents> implements AccidentsDao {
    @SuppressWarnings("unchecked")

    @Override
    public void saveAccidents(Accidents accidents) {
        persist(accidents);

    }

    @Override
    public List<Accidents> findAllAccidents() {
        Criteria criteria = createEntityCriteria();
        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        List<Accidents> accidents = (List<Accidents>) criteria.list();
        return accidents;
    }

    @Override
    public List<Accidents> findAccidents(Date from, Date to) {
        Session session = sessionFactory.openSession();

        Transaction tx = null;

        tx = session.beginTransaction();
        Criteria cr = session.createCriteria(Accidents.class);

        cr.add(Restrictions.between("date", from, to));
        List accidents = cr.list();
        return accidents;
    }

}