package EntitiesDao;

import HibernateEntities.ClientsEntity;
import HibernateEntities.StudentsEntity;
import org.hibernate.Session;
import org.hibernate.query.Query;


import java.util.ArrayList;
import java.util.List;

import static HibernateUtil.HibernateUtil.getSession;

/**
 * Created by Кинетик on 01.01.2017.
 */
public class StudentDao {
    public static List<StudentsEntity> loadStudent(Integer grId){
        Session session = getSession();
        Query query = session.createQuery("FROM StudentsEntity where stGroup="+grId);
        List<StudentsEntity> res = query.list();
        session.close();
        return res;
    }
    public static void createNewStudent(StudentsEntity student) {
        Session session = getSession();
        Query query = session.createQuery("Select max(studentsId) FROM StudentsEntity ");
        Integer res = (Integer)query.uniqueResult();
        student.setStudentsId(res+1);
        session.beginTransaction();
        session.save(student);
        session.getTransaction().commit();
        session.close();
    }
    public static boolean checkIsStudent(StudentsEntity student){
        Session session = getSession();
        Query query = session.createQuery("FROM StudentsEntity Where stFamily="+student.getStFamily()+" and stFirstName="+student.getStFirstName()+" and stSecName="+student.getStSecName()+" and stGroup="+student.getStGroup());
        List<StudentsEntity> res = query.list();
        if(res.size()==0){
            return false;
        }
        return true;
    }
}
