Grails hasOne和hasMany具有相同的域和级联操作 [英] Grails hasOne and hasMany with same domain and cascade operation

查看:164
本文介绍了Grails hasOne和hasMany具有相同的域和级联操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class父类{
字符串名称
static hasOne = [firstChild:Child]
static hasMany = [otherChildren:Child]
}


class Child {
字符串名称
static belongsTo = [parent:Parent]
}

现在,当我尝试运行一个简单的代码:

 父p =新父(名称:parent,firstChild:new Child(name:'child'))
p.addToOtherChildren(new Child(name:child2));
p.addToOtherChildren(new Child(name:child3));
p.save(flush:true)

它保存对象,但是当我尝试在Parent上做一个列表操作,它会抛出这个错误:

  org.springframework.orm.hibernate4.HibernateSystemException:多行与给定的标识符被发现:2,为类:test.Child;嵌套异常是org.hibernate.HibernateException:找到了多于一行的给定标识符:2,for class:test.Child 

这里的问题是hasOne在Parent中存储父id,就像hasMany在belongsTo中一样,现在多于一个的子实例具有相同的父id,因此很难确定哪一个用于firstChild。

我也试过这个解决方案,但它会抛出这个例外:

org.springframework.dao.InvalidDataAccessApiUsageException:非空属性引用瞬态值 - 在当前操作之前必须保存瞬态实例:test .Child.parent - > test.Parent;嵌套异常是org.hibernate.TransientPropertyValueException:非空属性引用瞬态值 - 在当前操作之前必须保存瞬态实例:test.Child.parent - > test.Parent



有没有更好的方法可以做,或者我做错了什么?

我想用first级联保存firstChild和otherChildren。根据错误信息(瞬态),您需要 save 父母在添加子女之前:

 父母p =新父母parent)

if(p.save()){
p.firstChild = new Child(name:'child');
p.addToOtherChildren(new Child(name:child2));
p.addToOtherChildren(new Child(name:child3));
p.save(flush:true)
}


Is there any way of making following structure:

class Parent {
   String name
   static hasOne = [firstChild: Child]
   static hasMany = [otherChildren: Child]
}


class Child{
   String name
   static belongsTo = [parent: Parent]
}

Now when i try to run a simple code:

Parent p = new Parent(name: "parent", firstChild: new Child(name: 'child'))
p.addToOtherChildren(new Child(name: "child2"));
p.addToOtherChildren(new Child(name: "child3"));
p.save(flush: true)

It saves the object but when I try to do a list operation on Parent, it throws this error:

org.springframework.orm.hibernate4.HibernateSystemException: More than one row with the given identifier was found: 2, for class: test.Child; nested exception is org.hibernate.HibernateException: More than one row with the given identifier was found: 2, for class: test.Child

The problem here is hasOne stores the Parent id in Child, as does hasMany with belongsTo, now more than one child instance has same parent id, hence it is finding difficult to decide which one is for firstChild.

I have tried this solution as well but it throws this exception:

org.springframework.dao.InvalidDataAccessApiUsageException: Not-null property references a transient value - transient instance must be saved before current operation : test.Child.parent -> test.Parent; nested exception is org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : test.Child.parent -> test.Parent

Is there any better way of doing it or am i doing anything wrong?

I want to cascade save firstChild and otherChildren with parent.

解决方案

According to error message (transient), You need to save parent before adding children :

 Parent p = new Parent(name: "parent")

 if(p.save()){
     p.firstChild=new Child(name: 'child');
     p.addToOtherChildren(new Child(name: "child2"));
     p.addToOtherChildren(new Child(name: "child3"));
     p.save(flush: true)
}

这篇关于Grails hasOne和hasMany具有相同的域和级联操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆