0

下面的 Grails 代码在尝试.save()Foo 对象时抛出以下异常:

org.hibernate.TransientObjectException/
org.springframework.dao.InvalidDataAccessApiUsageException: 
object references an unsaved transient instance - 
save the transient instance before flushing: Bar

我想我错过了一些与从 HTTP 参数自动填充域对象相关的 GORM 语义。

我的问题很简单:

  • 填充和保存 Foo 对象的正确方法是什么,而不会出现异常?

模型:

class Foo {
  Bar bar
}

看法:

<g:form id="${foo.id}">
  <g:select name="foo.bar.id" from="${Bar.list()}" />
</g:form>

控制器:

class FooController {
  def fooAction = {
    Foo foo = new Foo(params)
    foo.save()
    [ foo: foo ]
  }
}
4

1 回答 1

4

如果 'Bar' 仅存在于 Foo 的上下文中,则将以下行添加到 Bar.groovy

class Bar {
   static belongsTo = Foo

}

如果在其他上下文中使用“Bar”,您可以在 Foo.groovy 中使用

class Foo {
  Bar bar
  static mapping = {
    bar cascade:'all-delete-orphan'
  }


}
于 2009-11-26T13:22:38.567 回答