Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Grails/Hibernate:DuplicateKeyException和UnuniqueObjectException_Java_Hibernate_Grails - Fatal编程技术网

Java Grails/Hibernate:DuplicateKeyException和UnuniqueObjectException

Java Grails/Hibernate:DuplicateKeyException和UnuniqueObjectException,java,hibernate,grails,Java,Hibernate,Grails,我有两种方法可以使用GORM检索数据并进行更新和保存。将抛出以下错误 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.Dupl

我有两种方法可以使用GORM检索数据并进行更新和保存。将抛出以下错误

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
我的方法是: (一)

(二)

(三)


我添加了
@Transactional
job.attach(),
job.refresh()
,但没有任何效果。

请在保存作业实例之前尝试合并它。Merge()获取传递的实例,并将其与会话关联的具有相同id的任何对象合并。

作业看起来如何?有双向关系吗?这些是急切地获取的吗?是的,它具有双向关系和急切地获取数据,但在持久化作业之前不会更新这些数据。我刚刚读了关系表的值。如果存在这样的关系,则很可能是非唯一对象异常的原因,因为该对象在会话中加载了两次…请尝试删除“fetch:eager”,并为作业中的关系创建一个getter。在需要关系的地方使用getter。关键是,在创建
Job.findByStatus…
时,确保只有一个作业实例加载到会话中。也就是说,您不能从作业->作业状态->作业(如果这是您的关系):)
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
}
@Synchronized
Job changeStatusByJob(Job job, JobStatus jobStatus) {
    job.refresh()
    job.status = jobStatus

    if (persistJob(job)) {
        log.info("The status is changed.")
    }
    return job
}
 @Transactional
 Job getPendingJob() throws JobException {
     return Job.findByStatusAndCreatedLessThanEquals(READY, new Date(), [sort: "type"])
 }