Quantcast

How essential is 'belongsTo'? (a case of NonUniqueObjectException)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

How essential is 'belongsTo'? (a case of NonUniqueObjectException)

Iulian Dogariu
Hello all,

Been playing with Grails for some time now, and so far my experience is
quite positive. Except today I hit a brick wall.

Here's the scenario: There are two domain classes, Camera and
CameraManufacturer. Camera essentially looks like this:

class Camera {
       Long id
       Long version
       /* snip */
       String name
       Boolean alive = false

       /* many-to-one, no belonging specified */
       CameraManufacturer manufacturer
}

ApplicationBootStrap.groovy successfully creates and saves several
Camera objects, all having the same CameraManufacturer.

There is also a job which periodically checks that the cameras are
"alive". It's here that I get:

org.hibernate.NonUniqueObjectException: a different object with the same
identifier value was already associated with the session:
[CameraManufacturer#1]

// grails-app/jobs/CameraCheckerJob.groovy
class CameraCheckerJob {
        def startDelay = 20000
        def timeout = 10000

        def execute() {
            Camera.findAll().each { cam |
                // Ping the cam. Assume ping is successful
                println "Cam ${cam} is alive"
                cam.alive = true;
                cam.save()  // Exception thrown HERE
                            // at the second iteration
            }
            println "Job well done"
        }
}

By poking around, I found that the exception no longer occurs if I add
'def belongsTo = CameraManufacturer' to Camera.

Can anyone shed some light on this, please? Is this expected behaviour,
or might I have run into a bug? I have Java experience, but am pretty
new to Hibernate, and the info about NonUniqueObjectException on the
Hibernate forums is not particularly enlightening.

Thanks!

iulian



---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How essential is 'belongsTo'? (a case of NonUniqueObjectException)

graemer
This is a combination of a bug and the correct behavoir believe it or not :-)

The bug is that we are not wrapping an open/close transaction sequence
around the execution of the job. This means that a new session is
being opened multiple times which can cause errors like this.

But, you should have belongsTo on the CameraManufacturer as if you
don't deleting a camera will result in the manufacturer being deleted
too (along with many other cameras) due to the cascading nature of
updates.

On 9/16/06, Iulian Dogariu <[hidden email]> wrote:

> Hello all,
>
> Been playing with Grails for some time now, and so far my experience is
> quite positive. Except today I hit a brick wall.
>
> Here's the scenario: There are two domain classes, Camera and
> CameraManufacturer. Camera essentially looks like this:
>
> class Camera {
>        Long id
>        Long version
>        /* snip */
>        String name
>        Boolean alive = false
>
>        /* many-to-one, no belonging specified */
>        CameraManufacturer manufacturer
> }
>
> ApplicationBootStrap.groovy successfully creates and saves several
> Camera objects, all having the same CameraManufacturer.
>
> There is also a job which periodically checks that the cameras are
> "alive". It's here that I get:
>
> org.hibernate.NonUniqueObjectException: a different object with the same
> identifier value was already associated with the session:
> [CameraManufacturer#1]
>
> // grails-app/jobs/CameraCheckerJob.groovy
> class CameraCheckerJob {
>         def startDelay = 20000
>         def timeout = 10000
>
>         def execute() {
>             Camera.findAll().each { cam |
>                 // Ping the cam. Assume ping is successful
>                 println "Cam ${cam} is alive"
>                 cam.alive = true;
>                 cam.save()  // Exception thrown HERE
>                             // at the second iteration
>             }
>             println "Job well done"
>         }
> }
>
> By poking around, I found that the exception no longer occurs if I add
> 'def belongsTo = CameraManufacturer' to Camera.
>
> Can anyone shed some light on this, please? Is this expected behaviour,
> or might I have run into a bug? I have Java experience, but am pretty
> new to Hibernate, and the info about NonUniqueObjectException on the
> Hibernate forums is not particularly enlightening.
>
> Thanks!
>
> iulian
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


--
Graeme Rocher
Grails Project Lead
http://grails.org

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Loading...