Search code examples
hibernategrailsgrails-orm

How to prevent Hibernate from persisting Object when passed to service using GORM


I have a controller, inside the controller I pass the Domain Object to a service to check a version and some other logic. My controller/service call is message = service.updateExisting(domainObj, un, other).

When I run domainObj.version in the controller I get a version lower than when I run it in the service. Also the changes are persisted regardless of the checks I do in the updateExisting method.

I have also tried setting grails.gorm.autoFlush = false in the Config.groovy and def transactional=false in the service neither seems to help. can anyone see what I am missing?

I also tried static transactional=false in the controller and in the service


Solution

  • I found my answer here. It turns out that the controller ends its Hibernate session when it calls the service persisting the data. If I do the following...

    domainObj.discard()
    message = service.updateExisting(domainObj, un, other)
    

    This was a little counter-intuitive for me because I thought the discard would discard the changes as well but that doesn't seem to be the case. I then call the .save() in the service and everything persists as expected. Thanks to everyone for trying to help out.