0

I am using spring-4 with hibernate-5 with annotation based configuration. in my application I have one dao function which will save/update by saveorupdate() of session object . from service layer I am calling dao function twice : 1) for inserting a new object and 2) updating same object with some property change one more time. and my service function is in @Transactional.

problem : I am able to insert first time but after that its not updating same object. but its updating if I am using @Transactional at dao method(but its not correct approach).

I have given a sample example(same as my actual code) as below.

@Service
public class TransactionService{
public boolean calculate(){
  Object obj = new Object
  insertOrUpdate(obj);
  obj.setNewProperty();
  insertOrUpdate(obj);
}

@Transactional
 public void insertOrUpdate(Object obj){
   dao.saveOrUpdate(obj);
 }
}

can you guys help me out for this issue. Thanks,

  • Why not add @Transaction on calculate() method instead of insertOrUpdate method? – maruf571 Sep 08 '17 at 15:59
  • 1
    The `@Transactional` is ignored if it is called by method in the same class, as it can't be proxied. – NiVeR Sep 08 '17 at 19:50
  • Hello Maruf Hassan, by Adding @Transactional on calculate, it worked, but I was doing session.close() for every crud operation in dao layer. i just commented that close() code, it worked fine for me. but I still have one doubt, where should I close the session. – Saroj Kumar Moharana Sep 09 '17 at 09:12

0 Answers0