Grails,save在服务中更新对象时不会保存

时间:2017-11-20 09:41:45

标签: grails gorm

拥有一个从服务操作然后尝试保存的域,即使我使用:save(flush:true, failOnError:true)validate()也会返回true,而hasErrors()返回false

我从bootstrap打来电话:

def timeKeeperService
def init = { servletContext ->
    def TimeKeeper tk = TimeKeeper.findByName('MAIN')?:new TimeKeeper(name:'MAIN').save(flush:true, failOnError:true)
    tk = timeKeeperService.initialize()
    tk = timeKeeperService.workStart()

域名:

class TimeKeeper {

    String name
    Date dateCreated
    Date dateUpdated
    Date workStart
    Date workEnd

    def initialize() {
        println("TimeKeeper initialize...")
        workStart = null
        workEnd = null
    }


    def workStart() {
       if (workStart == null) {
          println("TimeKeeper - workStart")
          workEnd = null
           workStart = new Date()  
       } else {

       }
           println("TimeKeeper - workStart: "+workStart)
    }

    def workEnd() {
        if (workEnd == null) {
            workStart = null
            workEnd = new Date()
        } else {

        }

    }

    def beforeInsert() {
        dateCreated = new Date()
        dateUpdated = new Date()
        println("TimeKeeper - beforeInsert workStart: "+workStart)
    }

    def beforeUpdate() {
        println("TimeKeeper - beforeUpdate workStart: "+workStart)
        dateUpdated = new Date()
    }

    static constraints = {
        dateCreated nullable:true
        workStart nullable:true
        workEnd nullable:true
    }
}

服务:

@Transactional
class TimeKeeperService {

    def TimeKeeper getTimeKeeper() {
        TimeKeeper.findByName('MAIN')
    }

    def TimeKeeper initialize() {
        def TimeKeeper TK = getTimeKeeper()
        TK.initialize()
    //    def s = TK.save(flush:true)
    //    println("TimeKeeperService - initialize - save: "+s )
       return TK
    }

    def TimeKeeper workStart() {
       def TimeKeeper TK = getTimeKeeper()
       TK.workStart()
       println("TimeKeeperService - validate workStart: "+TK.validate())
    //   TK.save(flush:true, failOnError: true)
         println("TimeKeeperService - workStart: "+TK.workStart+" Errors: "+TK.hasErrors())
       return TK
   }

当从引导程序创建对象时,它可以工作 - 创建并保存对象。 但即使我制作了以下服务方法initializeworkStart也不会保存:

tk.save(flush:true, failOnError:true) 

在bootstrap中。

但是如果我在bootstrap中设置属性然后调用save就可以了。

    tk.workStart = new Date()
    tk.save(flush:true, failOnError: true)

我已经挣扎了好几个小时而没有成功。如果我能帮助解决这个问题,我将非常高兴。

4 个答案:

答案 0 :(得分:3)

我在Grails 3.3.0中遇到了同样的问题。调用域内的方法不会使实例成为dirty。对于解决方法,我试图在控制器中设置值,它从那里起作用:(

如果您调试,您可能会看到,值已更改但实例未更新,因为hibernate未将其更新为dirty

我怀疑它是Grails 3.3.0中的一个错误

我有gradle属性 -

grailsVersion=3.3.0
gormVersion=6.1.6.RELEASE
gradleWrapperVersion=3.5

答案 1 :(得分:0)

我在您的服务方法代码中发现了一些问题 -

def TimeKeeper initialize() {
    def TimeKeeper TK = getTimeKeeper()
    TK.initialize()
//    def s = TK.save(flush:true)
//    println("TimeKeeperService - initialize - save: "+s )
   return TK
}

看来这个方法将被递归调用,而不会随时调用save。

答案 2 :(得分:0)

很可能你的服务方法返回null而不是你期望它返回的TimeKeeper

    def TimeKeeper getTimeKeeper() {
        TimeKeeper.findByName('MAIN')
    }

如果在约束条件下找不到TimeKeeper,这个方法将只返回null。

我会修改此方法,以确保返回TimeKeeper

def TimeKeeper getTimeKeeper() {
        TimeKeeper.findOrCreateByName('MAIN')
    }

答案 3 :(得分:-1)

使用

hibernate:
    flush:
        mode: AUTO

application.yml