I have this 2 methods in the same service class:
boolean meth1(DomClass dom1) { //parameter is an instance of domain class DomClass ... meth2(dom1) ... dom1.delete(flush: true) ... return true } boolean meth2(DomClass dom1) { ... dom1.changeSomeProperty dom1.save(flush:true) return true }
The problem is that on the line calling dom1.delete(flush: true)
the program crashes with deleted object would be re-saved by cascade (remove deleted object from associations)
.
Now, I don't know Hibernate very well, but my guess is that either method is creating a new transaction, and meth1 has the first transaction, and meth2 the second one. And indeed, if I remove the dom1.save
everything works well.
Now, my question: I can make the meth1 to contain all the code from meth2, but this will mean that I duplicate code a lot (in my real example I want to reuse some logic in many places). How can I reuse the code on one method by making all the services methods stack run in the same transaction (if this is really the problem, else: "what is the problem?")