Возможный дубликат:
Spring + Hibernate: другой объект с тем же значением идентификатора уже был связан с сеансом
У меня есть три домена, похожие на пример ниже:
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
}
У меня есть контроллер, который создает кучу объектов и одновременно сохраняет их в транзакции. Он отлично работает после сохранения пяти или шести разных пакетов, но через некоторое время я получаю исключение:
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]
Вот раздел кода контроллера, который сохраняет объекты. Кто-нибудь знает, что происходит?
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()
}
}