문제

I have several Grails 2.1 domain classes that include dateCreated and lastUpdated fields that GORM manages automatically, eg:

class Person {
    Date dateCreated
    Date lastUpdated
    String name
}

I want Grails to automatically fill in these fields at runtime, but I also want to create some test data where I can manually define the values of these dates. The trouble is that Grails automatically sets the values if these fields with an interceptor even when I have specifically set them.

I have seen this SO question which describes how to allow changes to dateCreated, but I need to change lastUpdated as well. Is this possible?

도움이 되었습니까?

해결책

Whoops, my mistake, the approach in the other question does work, but the entity in question was separately being saved somewhere else. It also seems that you need an explicit flush to make things work:

def withAutoTimestampSuppression(entity, closure) {
    toggleAutoTimestamp(entity, false)
    def result = closure()
    toggleAutoTimestamp(entity, true)
    result
}

def toggleAutoTimestamp(target, enabled) {
    def applicationContext = (ServletContextHolder.getServletContext()                                                           
                              .getAttribute(ApplicationAttributes.APPLICATION_CONTEXT))

    def closureInterceptor = applicationContext.getBean("eventTriggeringInterceptor")
    def datastore = closureInterceptor.datastores.values().iterator().next()
    def interceptor = datastore.getEventTriggeringInterceptor()

    def listener = interceptor.findEventListener(target)
    listener.shouldTimestamp = enabled
    null
}

def createTestPerson() {

    def luke = new Person(name: "Luke Skywalker")
    withAutoTimestampSuppression(luke) {

        def lastWeek = new Date().minus(7)
        luke.dateCreated = lastWeek
        luke.lastUpdated = lastWeek
        luke.save(failOnError: true, flush: true)
    }
}

다른 팁

If it is an integration test you can use an hql update statement to manually set lastUpdated.

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