如何使用 Grails 从一个视图保存多个对象

发布于 2024-12-25 12:41:57 字数 786 浏览 1 评论 0原文

这个问题是这篇文章的后续问题 Grails 一对多关系视图

示例表明在运行时不起作用并抛出以下异常

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,它可以让我保存同一域类的多个实例,而不是使示例工作。例如,一个 GSP 可以让我一次插入多个 Book 实例吗?

This question is in follow up to this post Grails one to many relationship view

The example suggested there is not working and throwing following exception at run time

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

I think rather then making the example work can any one help me understand how can I create a GSP which can let me save multiple instances of same domain class. For example, a GSP which can let me insert multiple Book instances at once?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倚栏听风 2025-01-01 12:41:57

再次检查我在 github 上链接的项目。它演示了执行此操作的一些更好的实践。特别是,请查看 问题/ index,因为这就是视图的样子。实际的保存部分是在 QuestionService,由 QuestionController。这个项目完全完成了您想要做的事情。回顾一下。

Once again, examine the project I linked on github. It is a demonstration of some of the one of the better practices for doing this. Particularly, look at the question/index, as this is what the view can look like. The actual saving piece is done in the QuestionService, used by the QuestionController. This project does exactly what you're trying to do. Review it.

轻许诺言 2025-01-01 12:41:57

按如下所示更改电话示例中的 Contact 类,它应该可以正常工作。

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}"
    }
}

Change the Contact class in the phone example as follows and it should work fine.

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}"
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文