【问题标题】:Grails GORM problem: Object references an unsaved transient instanceGrails GORM 问题:对象引用了一个未保存的瞬态实例
【发布时间】:2010-12-20 16:17:04
【问题描述】:

下面的 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 ]
  }
}

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

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

    class Bar {
       static belongsTo = Foo
    
    }
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      • 2014-01-23
      • 2014-06-29
      相关资源
      最近更新 更多