当尝试对Foo对象进行.save()
时,下面的Grails代码将引发以下异常:
org.hibernate.TransientObjectException/
org.springframework.dao.InvalidDataAccessApiUsageException:
object references an unsaved transient instance -
save the transient instance before flushing: Bar
我想我缺少一些与从HTTP参数自动填充域对象有关的GORM语义。
我的问题很简单:
模型:
class Foo {
Bar bar
}
View :
<g:form id="${foo.id}">
<g:select name="foo.bar.id" from="${Bar.list()}" />
</g:form>
Controller :
class FooController {
def fooAction = {
Foo foo = new Foo(params)
foo.save()
[ foo: foo ]
}
}
最佳答案
如果“Bar”仅存在于Foo的上下文中,则将以下行添加到Bar.groovy
class Bar {
static belongsTo = Foo
}
如果在其他上下文中使用“Bar”,则可以在Foo.groovy中使用
class Foo {
Bar bar
static mapping = {
bar cascade:'all-delete-orphan'
}
}
关于grails - Grails GORM问题:对象引用了未保存的 transient 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1803415/