简体   繁体   English

Grails /休眠模式:DuplicateKeyException和NonUniqueObjectException

[英]Grails/Hibernate: DuplicateKeyException and NonUniqueObjectException

I have a couple of methods that retreive data using GORM and updates and saves. 我有两种方法可以使用GORM检索数据并进行更新和保存。 The following errors are thrown. 引发以下错误。

2017-02-14 01:39:02,316 [ERROR ]    A different object with the same identifier value was already associated with the session : [com.example.Job#3365090]; nested exception is org.hibern$
org.springframework.dao.DuplicateKeyException: A different object with the same identifier value was already associated with the session : [com.example.Job#3365090]; nested exception is org.hibernate.Non$
        at com.example.job.JobDaoService.$tt__persistJob(JobDaoService.groovy:88)
        at com.example.job.JobDaoService.$tt__changeJobStatusByJob(JobDaoService.groovy:225)

...
Caused by: org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [com.example.Job#3365090]
        ... 23 more

My methods are: (1) 我的方法是:(1)

Job persistJob(Job job) throws JobException {
    job.save(flush: true)

    if (job.hasErrors()) {
        throw new JobException("Error (ID: ${job?.id}). Details: ${job.errors}", JOB_CANNOT_BE_CREATED_EXCEPTION)
    }
    return job
}

(2) (2)

@Synchronized
Job changeStatusByJob(Job job, JobStatus jobStatus) {
    job.refresh()
    job.status = jobStatus

    if (persistJob(job)) {
        log.info("The status is changed.")
    }
    return job
}

(3) (3)

 @Transactional
 Job getPendingJob() throws JobException {
     return Job.findByStatusAndCreatedLessThanEquals(READY, new Date(), [sort: "type"])
 }

I added @Transactional , job.attach() , job.refresh() and nothing worked. 我加了@Transactionaljob.attach() job.refresh()并没有什么工作。

Try to merge your job instance before saving it. 在保存之前,请尝试合并您的作业实例。 Merge() takes the instance passed and merges it with any object of the same id associated with the session. Merge()接受传递的实例,并将其与与该会话关联的具有相同ID的任何对象合并。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM