문제

In a grails Domain a have implemented the beforeDelete as follow

class Shop {
    def beforeDelete() {
        Shop.withNewSession {
            Client.findAllByShop(this)*.shop = null                 
        }    
    }
}

But the client shop null value is not persisted to the DB.

If I add a manual session flush

class Shop {
    def beforeDelete() {
        Shop.withNewSession { s2->
            Client.findAllByShop(this)*.shop = null         
            s2.flush()  
            s2.clear()
        }
    }
}

It works, the client shop value are nulled in the db.

Is this a Grails bug or I have misunderstand the documentation? Doesn't withNewSession imply an automatic flush?

도움이 되었습니까?

해결책

The documentation (scroll down a bit to the beforeDelete example here) seems to imply that flushing or clearing the session is not required.

Burt Beckwith has also indicated on the Grails mailing list (see thread here) that manual calls to flush() and clear() aren't necessary in a withNewSession closure.

With that said, there does appear to be a bug report (see details here) using withNewSession starting with Grails 2.2.1.

다른 팁

withNewSession gets you a new Hibernate session, but it isn't necessarily transactional. It sounds like you want to use withTransaction instead of withNewSession.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top