Search in sources :

Example 6 with TeacherDaoException

use of com.remswork.project.alice.dao.exception.TeacherDaoException in project classify-system by anverliedoit.

the class TeacherDaoImpl method deleteTeacherById.

@Override
public Teacher deleteTeacherById(long id) throws TeacherException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Teacher teacher = session.get(Teacher.class, id);
        String[] table = new String[2];
        table[0] = Class.class.getSimpleName();
        table[1] = Formula.class.getSimpleName();
        if (teacher == null)
            throw new TeacherDaoException("Teacher with id : " + id + " does not exist.");
        for (String cell : table) {
            String hql = "delete from ".concat(cell).concat(" where teacher.id = :teacherId");
            Query query = session.createQuery(hql);
            query.setParameter("teacherId", id);
            query.executeUpdate();
        }
        teacher.setDepartment(null);
        session.delete(teacher);
        session.getTransaction().commit();
        session.close();
        return teacher;
    } catch (TeacherDaoException e) {
        session.close();
        throw new TeacherException(e.getMessage());
    }
}
Also used : TeacherException(com.remswork.project.alice.exception.TeacherException) Query(org.hibernate.Query) TeacherDaoException(com.remswork.project.alice.dao.exception.TeacherDaoException) Class(com.remswork.project.alice.model.Class) Session(org.hibernate.Session)

Aggregations

TeacherDaoException (com.remswork.project.alice.dao.exception.TeacherDaoException)6 TeacherException (com.remswork.project.alice.exception.TeacherException)6 Session (org.hibernate.Session)6 DepartmentException (com.remswork.project.alice.exception.DepartmentException)2 Date (com.remswork.project.alice.model.support.Date)2 Query (org.hibernate.Query)2 Class (com.remswork.project.alice.model.Class)1 ArrayList (java.util.ArrayList)1