Grails独特的复合关键问题 [英] Grails unique compound key issues

查看:103
本文介绍了Grails独特的复合关键问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个GORM类:

    class PriceSheet {

    Client client
    Population population
    Product product
    RevenueModelType modelType

    BigDecimal price

    static constraints = {
        client(unique: ['population', 'product', 'modelType'])
    }
}

我希望仅当客户,总体,产品和型号类型唯一时,才保存/更新PriceSheet. (对于客户,人口,产品和modelType的组合,应该只有一个价目表项目.)

I'm wanting a PriceSheet to be saved/updated only if the client, population, product and modelType are unique. (There should only be one pricesheet item for combination of client, population, product and modelType).

密钥是在mySQL中创建的.

The key is being created in mySQL.

我的问题是grails验证通过,但是保存失败.

My issue is the grails validation passes, but the save fails.

    priceSheetInstance.validate()

    if (priceSheetInstance.hasErrors()) {
        respond priceSheetInstance.errors, view:'create'
        return
    }

    priceSheetInstance.save flush:true

有什么想法或建议吗?验证后,我将调试器放在断点上,然后看到错误为空.

Any ideas or suggestions? I put the debugger on a breakpoint after validate and see errors are empty.

Grails 2.3.10

Grails 2.3.10

推荐答案

最终的解决方案是将NewSession包装在控制器中以进行保存:

Final solution was wrapping withNewSession in controller for saves:

    PriceSheet.withNewSession {

        priceSheetInstance.validate()

        // needed to call save in order to check the unique compound key
        // validate alone wasn't working
        // also needed to wrap withNewSession above
        // PriceSheet GORM object implements Serializable as well
        // spent way more time than expected on this.... wrestled with Grails

        if (priceSheetInstance.hasErrors() || (priceSheetInstance.save(failOnError: true) && priceSheetInstance.hasErrors())) {
            respond priceSheetInstance.errors, view:'create'
            return
        }

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'priceSheet.label', default: 'Price Sheet'), priceSheetInstance?.id])
                redirect (action: 'index')
            }
            '*' { respond priceSheetInstance, [status: CREATED] }
        }
    }

我不必包装newNewSession来进行更新...事实上,包装newNewSession来引起StaleObjectExceptions

I didn't have to wrap withNewSession for updates... in fact, wrapping withNewSession for updates was causing StaleObjectExceptions

这篇关于Grails独特的复合关键问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆