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());
}
}
Aggregations