显示从引导程序初始化的ObjectId

时间:2013-09-12 10:08:18

标签: mongodb twitter-bootstrap grails

我想引导以下课程:

import org.bson.types.ObjectId

class Foo{  
 ObjectId id  
 String name 

}

现在我在BootStrap.groovy中放了:

import org.bson.types.ObjectId
...
def foo = new Foo(
id: new ObjectId("507f191e810c19729de860ea"),
name: "foo"
)

但是当我想检查脚手架结果时,我得到以下错误:

Error 500: Internal Server Error

Class
    java.lang.IllegalStateException
Message
    Cannot convert value of type [java.lang.Integer] to required type [org.bson.types.ObjectId] for property 'id': no matching editors or conversion strategy found

如何显示ObjectId

1 个答案:

答案 0 :(得分:0)

您可能应该为您的Foo类添加适当的映射(并使用已分配的String id而不是ObjectId类型)。例如:

class Foo{

String id
String name 

static mapping = {
    table "Foo" 
    id column: 'Id', generator: 'assigned'
    name column: "Name"
  }
}

然后在你的Bootstrap.groovy:

def foo = new Foo(
    id: "507f191e810c19729de860ea",
    name: "foo"
)
foo.save();