hibernate - Grails域delete()是否更新事务中子代的数量

标签 hibernate grails gorm

我将通过测试自己回答这个问题,但以为我会参加stackoverflow社区的比赛...

在grails域事务中,子记录的数量在事务内部是否发生更改(例如,使用delete)

MyDomain.withTransaction{status->
    try{
       while(parent.children.size()>4){
          parent.children[0].delete(flush:true);
      }
    }
    catch(org.springframework.dao.DataIntegrityViolationException e){
        status.setRollbackOnly();
     }

}

最佳答案

答案是肯定的。尽管grails不允许 child 接触 child [0]。我必须通过使用children.collect {it} [0]或
children.iterator()。next()

测试:

 def p=new TestParentDomain();
 def i=0
 while(i<3){
     def child=new TestChildDomain();
     m.addToChildren(child);
    i++
 }
 m.save(flush:true);
 println p.children.size() //3   

 TestParentDomain.withTransaction{status->
    while(m.children.size()>1){
       m.removeFromChildren(m.children.iterator).next()).save(flush:true)
    }
 }
 println m.children.size();//1

关于hibernate - Grails域delete()是否更新事务中子代的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41307652/

相关文章:

grails - 满足多个表条件

hibernate - 自定义嵌入字段的列名

java - 如何将单个实体类动态映射到多个表?

java - Spring Boot 。找不到类型为 'org.hibernate.SessionFactory' 的 bean

mysql - hibernate 设置属性以触发数据库更新

java - org.springframework.beans.factory.BeanCreationException : Error creating bean with name 'appConfig' : Injection of autowired dependencies failed

sql - Grails&GORM:如何在域类上指定等效的CREATE INDEX?

grails - 如何使用 Vaadin 菜单栏导航页面?

grails - 如何通过遍历数组或映射来保存多行数据?

grails - Grails/Gorm-namedQuery hasMany关系