【问题标题】:How to save multiple object from one view using Grails如何使用 Grails 从一个视图中保存多个对象
【发布时间】:2012-01-08 08:10:30
【问题描述】:

这个问题是在跟进这个帖子Grails one to many relationship view

该示例建议在运行时不工作并引发以下异常

null id in blog.omarello.Phone entry (don't flush the Session after an exception occurs). Stacktrace follows:
Message: null id in blog.omarello.Phone entry (don't flush the Session after an exception occurs)
   Line | Method
->>  43 | doCall  in blog.omarello.ContactController$_closure4$$ENLORkU6
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
|   908 | run . . in     ''
^   662 | run     in java.lang.Thread

我认为,与其让示例工作,任何人都可以帮助我理解如何创建一个 GSP,它可以让我保存同一个域类的多个实例。例如,可以让我一次插入多个 Book 实例的 GSP?

【问题讨论】:

    标签: grails grails-orm one-to-many


    【解决方案1】:

    再次检查我在github 上链接的项目。它展示了执行此操作的一些最佳实践之一。特别是查看question/index,因为这就是视图的样子。实际的保存部分在QuestionService 中完成,由QuestionController 使用。该项目完全完成了您正在尝试做的事情。回顾一下。

    【讨论】:

      【解决方案2】:

      如下更改电话示例中的联系人类,它应该可以正常工作。

      package blog.omarello
      
      import org.apache.commons.collections.list.LazyList;
      import org.apache.commons.collections.FactoryUtils;
      
      class Contact {
      
          static constraints = {
              firstName(blank:false)
              lastName(blank:false)
          }
      
          String firstName
          String lastName
          String nickName
      
      
       List phones = LazyList.decorate(new ArrayList(),
                                        FactoryUtils.instantiateFactory(Phone.class));
      
      
      //    List phones = new ArrayList()
          static hasMany = [ phones:Phone ]
      
          static mapping = {
              phones cascade:"all-delete-orphan"
          }
      
      //    def getPhonesList() {
      //        return LazyList.decorate(
      //              phones,
      //              FactoryUtils.instantiateFactory(Phone.class))
      //    }
      
          def String toString() {
              return "${lastName}, ${firstName}"
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-15
        相关资源
        最近更新 更多