Grails - DuplicateKeyException [英] Grails - DuplicateKeyException

查看:109
本文介绍了Grails - DuplicateKeyException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了下面一段代码,它将新对象添加到数据库中。首先它从DB中取出另一个对象并添加到最终对象中。



我的代码的几行

  ClassC c = ClassC.findByName (cName)

ClassD d =新的ClassD(
名称:WHATEVER,
classC:c


printAAA \\\


ClassC.withTransaction {
c = c.merge()
// c.save(failOnError:true,flush:true)
}

printBBB\\\


// ClassD.withTransaction {
// d = d.merge()
//}
// printCCC\\\


ClassD.withTransaction {
d.save(failOnError:true,flush:true)
}

printDDD \\\

我得到以下错误:

  AAA 
BBB

2013-07-31 13:57:14,279错误JobRunShell - 作业DEFAULT.1抛出未处理的异常:
org.springframework.dao.DuplicateKeyException:具有相同标识符值的另一个对象已经与该会话关联:[xxx.ClassD#15];嵌套异常是org.hibernate.NonUniqueObjectException:具有相同标识符值的不同对象已与会话相关联:[xxx.ClassD#15]

您能帮助我吗?

谢谢




  ClassC.withTransaction {
ClassC c = ClassC.findByName(cName)

//找到名称为 WHATEVER或创建一个新的
ClassD d = ClassD.findOrCreateWhere(名称:WHATEVER)

c = c.merge()
c.addToClassesD( d)// static hasMany = [classesD:ClassD]< - 在ClassC域中
c.save(failOnError:true,flush:true)
}

错误从行


c.addToClassesD(d )




java.lang .IllegalStateException:找不到线程绑定的请求:
是指引用外的请求属性一个实际的Web请求,
或处理原始接收线程之外的请求?如果
实际上在web请求中运行,并且仍然收到此
消息,那么您的代码可能在
之外运行DispatcherServlet / DispatcherPortlet:在这种情况下,请使用
RequestContextListener或RequestContextFilter公开当前的
请求。
org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
at
org.springframework.web.context.request.RequestContextHolder $ currentRequestAttributes.call(未知
来源)在
xxx.AuditLogService.getCurrentUser(AuditLogService.groovy:127)在
xxx.AuditLogService $ getCurrentUser.callStatic(未知来源)在
xxx.AuditLogService $ _closure2
处的.doCall(AuditLogService.groovy:58)sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法) b $ b sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)at
org.springsource.loaded.ri .ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
at
org.codehaus.groovy.reflection.CachedMethod.invoke(Ca chedMethod.java:90)
2013-08-02 09:39:11,110错误ErrorLogger - 作业(DEFAULT.1抛出
异常。 org.quartz.SchedulerException:作业抛出未处理的
异常。在org.quartz.core.JobRunShell.run(JobRunShell.java:224)
at
org.quartz.simpl.SimpleThreadPool $ WorkerThread.run(SimpleThreadPool.java:557)
引起:java.lang.IllegalStateException:没有线程绑定的请求
找到了:您是否在实际的
Web请求之外引用请求属性,或者在最初的
接收线程之外处理请求?如果您实际上在Web请求
中运行并仍然收到此消息,则您的代码可能在DispatcherServlet / DispatcherPortlet的
之外运行:在这种情况下,请使用
RequestContextListener或RequestContextFilter来显示当前
请求。
org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
at
org.springframework.web.context.request.RequestContextHolder $ currentRequestAttributes.call(未知
来源)在
xxx.AuditLogService.getCurrentUser(AuditLogService.groovy:127)在
xxx.AuditLogService $ getCurrentUser.callStatic(未知来源)在
xxx.AuditLogService $ _closure2
处的.doCall(AuditLogService.groovy:58)sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法) b $ b sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)at
org.springsource.loaded.ri .ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
at
org.codehaus.groovy.reflection.CachedMethod.invoke(Ca chedMethod.java:90)


解决方案

您可能应该只在一个事务中执行所有操作。



至于你得到的错误,我假设你有一个名为的独特如果已经有一个名字为WHATEVER的记录,那么就会出现这个错误。



你可能想这样做:

  ClassC.withTransaction {
ClassC c = ClassC.findByName(cName)

//找到名字为: WHATEVER或创建一个新的如果没有
ClassD d = ClassD.findOrCreateWhere(name:WHATEVER)

c = c.merge()
d.classC = c
d.save(failOnError:true,flush:true)
}


I have got the following piece of code which adds new object to database. Firstly it takes another object from DB and add to the final object.

Few lines of my code

            ClassC c = ClassC.findByName(cName)

            ClassD d = new ClassD(
                    name: "WHATEVER",
                    classC: c
            )

            print "AAA\n"

            ClassC.withTransaction {
                c = c.merge()
                // c.save(failOnError: true, flush: true)
            }

            print "BBB\n"

            // ClassD.withTransaction {
            //     d = d.merge()
            // }
            // print "CCC\n"

            ClassD.withTransaction {
                d.save(failOnError: true, flush: true)
            }

            print "DDD\n"

I have got the following error:

AAA
BBB

2013-07-31 13:57:14,279 ERROR JobRunShell - Job DEFAULT.1 threw an unhandled Exception: 
 org.springframework.dao.DuplicateKeyException: a different object with the same identifier value was already associated with the session: [xxx.ClassD#15]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [xxx.ClassD#15]

Could you help me?

Thanks


ClassC.withTransaction {
    ClassC c = ClassC.findByName(cName)

    // find the record with name: "WHATEVER" or create a new one if there is none
    ClassD d = ClassD.findOrCreateWhere(name: "WHATEVER")

    c = c.merge()
    c.addToClassesD(d) // static hasMany = [classesD: ClassD] <-- in ClassC domain
    c.save(failOnError: true, flush: true)
}

error goes from line

c.addToClassesD(d)

:

java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) at org.springframework.web.context.request.RequestContextHolder$currentRequestAttributes.call(Unknown Source) at xxx.AuditLogService.getCurrentUser(AuditLogService.groovy:127) at xxx.AuditLogService$getCurrentUser.callStatic(Unknown Source) at xxx.AuditLogService$_closure2.doCall(AuditLogService.groovy:58) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90) 2013-08-02 09:39:11,110 ERROR ErrorLogger - Job (DEFAULT.1 threw an exception. org.quartz.SchedulerException: Job threw an unhandled exception. at org.quartz.core.JobRunShell.run(JobRunShell.java:224) at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557) Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) at org.springframework.web.context.request.RequestContextHolder$currentRequestAttributes.call(Unknown Source) at xxx.AuditLogService.getCurrentUser(AuditLogService.groovy:127) at xxx.AuditLogService$getCurrentUser.callStatic(Unknown Source) at xxx.AuditLogService$_closure2.doCall(AuditLogService.groovy:58) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)

解决方案

You probably should do it all inside one transaction only.

As for the error you are getting, I assume you have an unique set up for name -- you'll get that error when there is already a record with the name "WHATEVER" already.

You probably want to do something like this instead:

ClassC.withTransaction {
    ClassC c = ClassC.findByName(cName)

    // find the record with name: "WHATEVER" or create a new one if there is none
    ClassD d = ClassD.findOrCreateWhere(name: "WHATEVER")

    c = c.merge()
    d.classC = c
    d.save(failOnError: true, flush: true)
}

这篇关于Grails - DuplicateKeyException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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