Grails hasOne and hasMany with same domain and cascade operation

馋奶兔 提交于 2019-12-13 20:25:44

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/38665175/grails-hasone-and-hasmany-with-same-domain-and-cascade-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!