I am having a problem when updating a particular entry inside a loop using hibernateSessionFactory.currentSession getting a error i.e hibernateNonUniqueException a different object with the same identifier value is already associated with a session.The following situtaion is depicted below
public Login saveTeacher(TeacherDto teacherObject, List<ExamDto> examsObject, Teacher_DeptDto departmentsObject) throws Exception { Session session = HibernateSessionFactory.currentSession(); Transaction tx = session.beginTransaction(); Login lg = new Login(); try { for(Iterator iterator = examsObject.iterator();iterator.hasNext();){ ExamDto exobj=(ExamDto)iterator.next(); String Query = "update ExamDto e set e.id = :teacherId where e.examId = :eId"; Query query = session.createQuery(Query); query.setParameter("teacherId", teacherObject.getId()); query.setParameter("eId", exobj.getExamId()); session.update(exobj); //***This the area where the problem occurs } session.save(teacherObject); session.save(departmentsObject); tx.commit();
Please help how this can be rectified ...i n case when I am using openSession() the problem doesn't occur.
|