Grails“具有相同标识符值的不同对象已经与会话相关联”错误 [英] Grails "a different object with the same identifier value was already associated with the session" error

查看:117
本文介绍了Grails“具有相同标识符值的不同对象已经与会话相关联”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:


Possible Duplicate:
Hibernate: different object with the same identifier value was already associated with the session

$ b同一个标识符值已经被关联了


$ b

在我的控制器中的Grails中,失败的代码如下:具有相同标识符值的不同对象已与会话错误消息。
我已经访问了几页,它说我必须在调用save之前调用merge,这会导致该错误提供的id类com.easytha.QuizTag的错误类型。预期:类java.lang.Long,得到类org.hibernate.action.DelayedPostInsertIdentifier

I have the following code in my controller in Grails that is failing with "a different object with the same identifier value was already associated with the session" error message. I have already visited few pages where it says that I must call "merge" before calling save which ends up with this error Provided id of the wrong type for class com.easytha.QuizTag. Expected: class java.lang.Long, got class org.hibernate.action.DelayedPostInsertIdentifier

有人建议grails搜索插件可能会导致这和我应该删除searchable = true形式我的领域类不是一个选项(请参考上一篇文章 grails可搜索插件搜索内部hasMany类

Someone has suggested that grails searchable plugin might be causing this and I should remove searchable = true form my domain class which is not an option (refer to the previous post here grails searcheable plugin search in inner hasMany class)

有一点需要注意的是,不会引发错误在调用q.save()的时候,而是在调用重定向重定向的时候抛出它(action:show,id:id)!!

One thing to point is that error is not thrown at the time of calling q.save() rather it's thrown while calling redirect redirect(action:"show",id:id)!!

有什么建议吗? p>

Any suggestions?

def addTags(String tags,Long id){
        if(tags){
            String[] strTags = tags.split(",");
            Quiz q = Quiz.get(id)           
            for(String t in strTags){
                Tag tagToAdd = Tag.findByTag(t)

                if(!tagToAdd){
                    tagToAdd = new Tag(tag:t)
                    tagToAdd.save()
                }

                println "---> "+tagToAdd +" Quiz"+q?.id
                def qt = QuizTag.findByQuizAndTag(q,tagToAdd)
                if(!qt){
                    qt = new QuizTag(quiz:q,tag:tagToAdd);
                    q.addToTags(qt)
                }

            }           
            q.save()        
            redirect(action:"show",id:id)
        }
    }

-----------编辑---------------

-----------EDIT---------------

Final code that worked with searchable plugin
        def addTags(String tags,Long id){
        if(tags){
            String[] strTags = tags.split(",");
            Quiz q = Quiz.get(id)           
            for(String t in strTags){
                if (q.tags.any { QuizTag qt -> qt.tag.tag == t }) { continue; }
                    Tag tagToAdd = Tag.findOrSaveByTag(t);
                    QuizTag qt = new QuizTag(quiz:q,tag:tagToAdd)
                    q.addToTags(qt)
                }           
            q.save(flush:true)      
            redirect(action:"show",id:id)
        }
    }


推荐答案

默认情况下,Grails会使用Hibernate代理加载集合。因此,可能会重复创建 QuizTag 代理(或代理和虚拟对象),这会导致您的问题。

Grails by default lazy loads collections with Hibernate proxies. So maybe duplicate QuizTag proxies (or proxies and inflated objects) are being created, which is causing your issue.

您可以尝试如下所示:

You could try something like:

Quiz q = Quiz.get(id)         
for(String t in strTags){
    // Check if the tag is already joined to this quiz and
    // skip a dynamic finder load later
    if (q.tags.any { QuizTag qt -> qt.tag.tag == t }) { continue; }
    // Find or create-save the tag to join
    Tag tagToAdd = Tag.findOrSaveByTag(t);
    QuizTag qt = new QuizTag(quiz:q,tag:tagToAdd)
    qt.save()
    q.addToTags(qt)
}

这篇关于Grails“具有相同标识符值的不同对象已经与会话相关联”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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