package DAO;

import Entities.User;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import java.util.List;

/**
 * Created by tarik on 1/24/2017.
 */
public class UserDAO {
    private Session session;
    private Transaction transaction;

    public UserDAO() {
        this.session = HibernateUtil.getSession();
        this.transaction =  session.getTransaction();
    }

    public void save( User user ) {
        this.transaction.begin();
        this.session.saveOrUpdate( user );
        this.transaction.commit();
    }

    public List<User> listAll() {
        return  this.session.createQuery( "from User" ).list();
    }

    public User getUser( int id ) {
        return ( User ) session.get( User.class, id );
    }

    public User getByLogin( String login ) {
        return ( User ) this.session.createQuery( "From User u Where u.login = :login ").setParameter("login", login).getSingleResult();
    }

    public void delete( int id ) {
        this.transaction.begin();

        User user = ( User ) session.load( User.class, id );
        this.session.delete( user );

        this.transaction.commit();
    }
}

@@ -96,42 +96,8 @@
 ion;
-%0Aimport org.hibernate.query.Query;
 %0A%0Aim
@@ -784,24 +784,41 @@
 g login ) %7B%0A
+        try%7B%0A    
         retu
@@ -937,24 +937,90 @@
 gleResult();
+%0A%0A        %7Dcatch (Exception e)%7B%0A            return null;%0A        %7D
 %0A    %7D%0A%0A    

update userDAO