grails - 如何使用Grails从一个 View 保存多个对象

标签 grails gorm one-to-many

这个问题是这个帖子的后续行动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?

最佳答案

如下更改电话示例中的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}"
    }
}

关于grails - 如何使用Grails从一个 View 保存多个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8776212/

相关文章:

html - 将 GSP View 呈现为紧凑的 html 文件

grails - HSQL数据库管理器不在Grails 2.0中出现

java - 同一实体上的 JPA 一对一

c++ - 我如何知道 C++ 中所有者对象的地址?

oracle - 在 GORM 中使用不同的数据库用户

grails - 从 IntelliJ 运行 Grails 集成测试

Grails 3 和 IntelliJ 无法启动项目

hibernate - 使用Grails引导具有多个数据库的数据时没有 hibernate session

hibernate - GORM中子查询的平均值

python - SQLalchemy 中不同表的关系