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 に答える