hibernate - Grails - DuplicateKeyException -


i have got following piece of code adds new object database. firstly takes object db , add final object.

few lines of 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 following error:

aaa bbb  2013-07-31 13:57:14,279 error jobrunshell - job default.1 threw unhandled exception:   org.springframework.dao.duplicatekeyexception: different object same identifier value associated session: [xxx.classd#15]; nested exception org.hibernate.nonuniqueobjectexception: different object same identifier value associated session: [xxx.classd#15] 

could me?

thanks


classc.withtransaction {     classc c = classc.findbyname(cname)      // find record name: "whatever" or create new 1 if there 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 line

c.addtoclassesd(d)

:

java.lang.illegalstateexception: no thread-bound request found: referring request attributes outside of actual web request, or processing request outside of receiving thread? if operating within web request , still receive message, code running outside of dispatcherservlet/dispatcherportlet: in case, use requestcontextlistener or requestcontextfilter expose current request. @ org.springframework.web.context.request.requestcontextholder.currentrequestattributes(requestcontextholder.java:131) @ org.springframework.web.context.request.requestcontextholder$currentrequestattributes.call(unknown source) @ xxx.auditlogservice.getcurrentuser(auditlogservice.groovy:127) @ xxx.auditlogservice$getcurrentuser.callstatic(unknown source) @ xxx.auditlogservice$_closure2.docall(auditlogservice.groovy:58) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25) @ java.lang.reflect.method.invoke(method.java:597) @ org.springsource.loaded.ri.reflectiveinterceptor.jlrmethodinvoke(reflectiveinterceptor.java:1243) @ org.codehaus.groovy.reflection.cachedmethod.invoke(cachedmethod.java:90) 2013-08-02 09:39:11,110 error errorlogger - job (default.1 threw exception. org.quartz.schedulerexception: job threw unhandled exception. @ org.quartz.core.jobrunshell.run(jobrunshell.java:224) @ org.quartz.simpl.simplethreadpool$workerthread.run(simplethreadpool.java:557) caused by: java.lang.illegalstateexception: no thread-bound request found: referring request attributes outside of actual web request, or processing request outside of receiving thread? if operating within web request , still receive message, code running outside of dispatcherservlet/dispatcherportlet: in case, use requestcontextlistener or requestcontextfilter expose current request. @ org.springframework.web.context.request.requestcontextholder.currentrequestattributes(requestcontextholder.java:131) @ org.springframework.web.context.request.requestcontextholder$currentrequestattributes.call(unknown source) @ xxx.auditlogservice.getcurrentuser(auditlogservice.groovy:127) @ xxx.auditlogservice$getcurrentuser.callstatic(unknown source) @ xxx.auditlogservice$_closure2.docall(auditlogservice.groovy:58) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25) @ java.lang.reflect.method.invoke(method.java:597) @ org.springsource.loaded.ri.reflectiveinterceptor.jlrmethodinvoke(reflectiveinterceptor.java:1243) @ org.codehaus.groovy.reflection.cachedmethod.invoke(cachedmethod.java:90)

you should inside 1 transaction only.

as error getting, assume have unique set name -- you'll error when there record name "whatever" already.

you want instead:

classc.withtransaction {     classc c = classc.findbyname(cname)      // find record name: "whatever" or create new 1 if there none     classd d = classd.findorcreatewhere(name: "whatever")      c = c.merge()     d.classc = c     d.save(failonerror: true, flush: true) } 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -