0

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

I have three domains arranged similar to the example below:

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
}

I have a controller that creates a bunch of objects and saves them simultaneously within a transaction. It works fine after saving about five or six different batches, but after a while I get the 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]

Here's the section of the controller code that saves the objects. Does anyone know what's going on?

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()
    }
}
Community
  • 1
  • 1
Jesse
  • 253
  • 1
  • 3
  • 12

1 Answers1

2

Your id mapping for ProgStartTime is something I haven't seen used often with Grails, but after looking it up it seems to me that by doing that composite id, you're making a guarantee of uniqueness on ProgStartTime that no two will share both a Computer and Program.

If you're violating that constraint, I would expect to see an error like the one you're seeing. Check what kind of data you're entering against what's in the ProgStartTime data table to see if that's the problem. If it's something else, I will do my best to assist further :)

Out of curiosity, what version of grails is this you're using?

Will Buck
  • 1,500
  • 16
  • 25
  • This is on Grails 2.0.1. I'm in the process of verifying the data right now. – Jesse Mar 13 '12 at 15:08
  • Probably should've just waited. Anyway, I've gone through the data and verified that there are no duplicates or conflicts. As far as I can tell, this shouldn't be happening, data-wise. It also only happens with certain data, and not random, but I can't figure out what's so special about the data. – Jesse Mar 13 '12 at 15:26
  • Hmm, interesting. Can you give an example of some of the data in the tables, and an example data input that's causing this error? – Will Buck Mar 13 '12 at 15:31
  • Not in a meaningful way. :-/ The **actual** code has more fields and a ton of data, which for both brevity and employment purposes I can't post here. Is there a way to enable Grails to more verbosely log or detail the cause of the exception? **Edit:** If you want some example data anyway, I can post some. Which domain(s) would you prefer, or do you want a little of each? – Jesse Mar 13 '12 at 16:53
  • 2
    Oh that probably won't be an easy exercise then will it :/ Try checking these links out and see if they help! http://stackoverflow.com/questions/4194350/a-different-object-with-the-same-identifier-value-was-already-associated-with-th http://grails.1312388.n4.nabble.com/a-different-object-with-the-same-identifier-value-was-already-associated-with-the-session-td1360032.html – Will Buck Mar 13 '12 at 17:02
  • I already saw these links, actually, but they refer to using the merge() method instead of save(). I'm okay with this, but I'd like to know how the objects I'm creating are falling out of the Hibernate session, since I'm doing all of this from a controller. – Jesse Mar 13 '12 at 17:06
  • Actually, it doesn't look like merge() works either. When I use merge(), _some_ ProgStartTime objects will throw a NullPointerException. I'm not sure what this means, especially since the object _does_ exist. – Jesse Mar 13 '12 at 17:16
  • I'm afraid that leaves me about out of ideas for the moment then, Jesse :/ If I think of anything else, I'll letcha know! – Will Buck Mar 13 '12 at 18:30