javax.persistence.TransactionRequiredException
Answer: use @Transactional annotation
@Repository annotation open a new transaction for us but I think it is an bug or smth.
sometimes it does not (if you have more info please correct me) so we need to hande situation and we need to tell our method need transaction so we use @Transactional annotation .
@Transactional(readOnly=false ,rollbackFor=Exception.class) public void replaceEmployeeIDForDuplicateTCKNO(Employee employee) { Query query=null; int rowCount; List<Employee> list = getAllDuplicate(employee); for (Employee emp : list) { if(!emp.equals(employee)){ query =entityManager.createNativeQuery("update ABSENCE abs SET abs.EMPLOYEE_ID =:employeeID where abs.EMPLOYEE_ID =:exchangeEmpID") .setParameter("employeeID", employee.id) .setParameter("exchangeEmpID", emp.id); rowCount= query.executeUpdate(); System.out.println("ABSENCE Rows affected: " +rowCount); query =entityManager.createNativeQuery("update ASSIGNMENT assgn SET assgn.EMPLOYEE_ID =:employeeID where assgn.EMPLOYEE_ID =:exchangeEmpID") .setParameter("employeeID", employee.id) .setParameter("exchangeEmpID", emp.id); rowCount= query.executeUpdate(); System.out.println("ASSIGNMENT Rows affected: " +rowCount);
Hiç yorum yok:
Yorum Gönder