保存引用 Grails 域中另一条记录的记录
Save record having reference to another record in Grails Domain
我有2个域名类:A和B,定义如下:
class A {
String data
static hasMany = [bs: B]
}
class B {
String data
static belongsTo = [a: A]
}
现在我们在 table B 中有字段 a_id。
问题是我已经有了 a_id,我怎样才能正确插入一个新的 B 记录 a_id 作为外键?
目前我是这样的:
B b = new B(
data: "data",
a: A.get(a_id)
)
b.save()
=> 为了能够插入 B 记录,我必须再做一个查询来获取整个 A 对象,这样会浪费时间和内存,因为 a_id一个人就够了。
非常感谢。
可悲的是,没办法做到这一点。
如果要优化,请考虑使用 B.executeUpdate 甚至原始 SQL
Sql sql = new Sql(dataSource)
sql.executeUpdate("insert into....")
我有2个域名类:A和B,定义如下:
class A {
String data
static hasMany = [bs: B]
}
class B {
String data
static belongsTo = [a: A]
}
现在我们在 table B 中有字段 a_id。 问题是我已经有了 a_id,我怎样才能正确插入一个新的 B 记录 a_id 作为外键?
目前我是这样的:
B b = new B(
data: "data",
a: A.get(a_id)
)
b.save()
=> 为了能够插入 B 记录,我必须再做一个查询来获取整个 A 对象,这样会浪费时间和内存,因为 a_id一个人就够了。
非常感谢。
可悲的是,没办法做到这一点。
如果要优化,请考虑使用 B.executeUpdate 甚至原始 SQL
Sql sql = new Sql(dataSource)
sql.executeUpdate("insert into....")