my own id in GORM

佐手、 提交于 2019-11-30 11:20:48

That's a feature of databinding. You don't want submitted data to be able to change managed fields like id and version, so the Map constructor that you're using binds all available properties except those two (it also ignores any value for class, metaClass, and a few others).

So there's a bit of a mismatch here since the value isn't managed by Hibernate/GORM but by you. As you saw the workaround is that you need to create the object in two steps instead of just one.

I can't replicate this problem (used Grails 2.0.RC1). I think it might be as simple as a missing equal sign on your static mapping = { (you just have static mapping {)

Here's the code for a domain object:

class Book {
    String id
    String name

    static mapping = {
        id generator:'assigned'               
    }
}

And inside BootStrap.groovy:

def init = { servletContext ->
    new Book(name:"test",id:"123abc").save(failOnError:true)
}

And it works fine for me. I see the id as 123abc.

You need to set the bindable constraint to true for your id prop, e.g.

class Employee {
    Long id
    String name

    static constraints = {
        id bindable: true
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!