gpt4 book ai didi

hibernate - Grails无法保存对象, session 中已存在该对象?

转载 作者:行者123 更新时间:2023-12-02 15:13:27 27 4
gpt4 key购买 nike

这个问题已经在这里有了答案:




已关闭8年。




Possible Duplicate:
Spring + Hibernate : a different object with the same identifier value was already associated with the session



我安排了三个域,类似于下面的示例:
class Computer {
static hasMany = [progStartTimes:ProgStartTime]
static belongsTo = User

static constraints = {
name()
operatingSystem()
processor()
}

User owner
OperatingSystem os
Processor processor
}

class OperatingSystem {
static hasMany = [computers:Computer]

static constraints = {
name(blank:false,unique:'versionType',maxSize:80)
versionType()
}

static mapping = {
versionType type:VersionTypeMapping
}

String name
VersionType versionType
}

class ProgStartTime implements Serializable {
static constraints = {
computer()
program()
duration()
}

static mapping = {
id composite:['computer','program']
duration type:DurationMapping
}

Computer computer
Program program
Duration duration
}

我有一个 Controller ,它创建一堆对象并在事务中同时保存它们。保存大约五到六个不同的批次后,它可以正常工作,但是一段时间后,我得到了异常(exception):
org.springframework.orm.hibernate3.HibernateSystemException: a different object
with the same identifier value was already associated with the session:
[diag.ProgStartTime#diag.ProgStartTime : null]; nested exception is
org.hibernate.NonUniqueObjectException: a different object with the same
identifier value was already associated with the session:
[diag.ProgStartTime#diag.ProgStartTime : null]

这是保存对象的 Controller 代码部分。有人知道发生了什么吗?
ownerInstance.withTransaction { status ->
try {
if (operatingSystem.id == null) {
operatingSystem.save()
}

if (processor.id == null) {
processor.save()
}

startTimes.each {
if (it.id == null) {
it.save()
}
} catch (Exception e) {
ownerInstance.errors.reject("Failed to save the necessary objects: " + e)
status.setRollbackOnly()
}
}

最佳答案

您对ProgStartTime的id映射是我在Grails上很少见到的,但是在我看来,通过执行该复合id,您可以保证ProgStartTime的唯一性,确保没有人会共享两个计算机和程序。

如果您违反了该限制,那么我希望看到的错误与您看到的一样。根据ProgStartTime数据表中的内容检查您要输入的数据类型,以查看是否存在问题。如果还有其他问题,我会尽力提供进一步的帮助:)

出于好奇,您使用的是哪个版本的grails?

关于hibernate - Grails无法保存对象, session 中已存在该对象? ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9640833/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com