Package ru.drakeface.springmvchibernate.dao

Source Code of ru.drakeface.springmvchibernate.dao.ContactDAOImpl

/*
$Author$
$Date$
$Revision$
$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 ru.drakeface.springmvchibernate.dao;

import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import ru.drakeface.springmvchibernate.domain.Contact;

/**
*
* @author ivc_ShherbakovIV
*/
@Repository
/*
    Аннотация показывает, что класс функционирует как репозиторий и требует наличия прозрачной трансляции исключений.
    Преимуществом трансляции исключений является то, что слой сервиса будет иметь дело с общей иерархией исключений от Спринга (DataAccessException)
    вне зависимости от используемых технологий доступа к данным в DAO слое.
*/

public class ContactDAOImpl implements ContactDAO {

    @Autowired
    /*
        Аннотация позволяет автоматически установить значение поля SessionFactory.
    */
    private SessionFactory sessionFactory;

    @Override
    public void addContact(Contact contact) {
        sessionFactory.getCurrentSession().save(contact);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Contact> listContact() {

        return sessionFactory.getCurrentSession().createQuery("from Contact")
                .list();
    }

    @Override
    public void removeContact(Integer id) {
        Contact contact = (Contact) sessionFactory.getCurrentSession().load(
                Contact.class, id);
        if (null != contact) {
            sessionFactory.getCurrentSession().delete(contact);
        }

    }

}
TOP

Related Classes of ru.drakeface.springmvchibernate.dao.ContactDAOImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.