Note that there are some explanatory texts on larger screens.

plurals
  1. POGrails - DuplicateKeyException
    primarykey
    data
    text
    <p>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. </p> <p>Few lines of my code</p> <pre><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" </code></pre> <p>I have got the following error:</p> <pre><code>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] </code></pre> <p>Could you help me?</p> <p>Thanks</p> <hr> <pre><code>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] &lt;-- in ClassC domain c.save(failOnError: true, flush: true) } </code></pre> <p>error goes from line </p> <blockquote> <p>c.addToClassesD(d)</p> </blockquote> <p>:</p> <blockquote> <p>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)</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload