com.portal.dao.PositionDAOImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.portal.dao.PositionDAOImpl.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.Position;
import com.portal.exception.PortalException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.exception.ConstraintViolationException;

/**
 *
 * @author Evgen
 */
public class PositionDAOImpl implements PositionDAO {

    private SessionFactory sessionFactory;

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

    @Override
    public void savePosition(Position position) {
        sessionFactory.getCurrentSession().saveOrUpdate(position);
    }

    @Override
    public Position loadPosition(int id) {
        Position position = null;
        try {
            position = (Position) sessionFactory.getCurrentSession().get(Position.class, id);
        } catch (Exception e) {

        }
        return position;
    }

    @Override
    public void deletePosition(int id) throws Exception {
        try {
            Query query = sessionFactory.getCurrentSession().createQuery("delete from Position where id=:id");
            query.setInteger("id", id);
            query.executeUpdate();
        } catch (ConstraintViolationException ex) {
            throw new PortalException(
                    "?  ?.  ? ?   ? ?.");
        }
    }

    @Override
    public List<Position> loadPositions() {
        return sessionFactory.getCurrentSession().createQuery("from Position").list();
    }

}