-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: NonUniqueObjectException!? Why?
PostPosted: Fri Feb 06, 2004 6:20 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
I'm using Hibernate 2.1.2.
Don't understand why is it giving me the following error:

Code:
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 1, of class: vo.Documento
   at net.sf.hibernate.impl.SessionImpl.checkUniqueness(SessionImpl.java:1642)
   at net.sf.hibernate.impl.SessionImpl.doUpdateMutable(SessionImpl.java:1414)
   at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1440)
   at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1364)
( ... )


Is there another way?
I need to make the two tests (if's) below.

------------------------------------------
Mapping files (generated with R3)
------------------------------------------

Documento
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
   
<hibernate-mapping>
<!--
    Created by Middlegen Hibernate plugin

    http://boss.bekk.no/boss/middlegen/
    http://hibernate.sourceforge.net/
-->

<class
    name="vo.Documento"
    table="documento"
>

    <id
        name="id"
        type="long"
        column="id"
    >
        <generator class="increment" />
    </id>

    <property
        name="nome"
        type="java.lang.String"
        column="nome"
        length="-1"
    />
   
    <!-- associations -->
    <!-- bi-directional one-to-one association to DocProxVersao -->
    <one-to-one
        name="docProxVersao"
        class="vo.DocProxVersao"
        outer-join="auto"
        constrained="false"
    />
    <!-- bi-directional one-to-many association to VersaoDocumento -->
    <set
        name="versaoDocumentos"
        lazy="true"
        inverse="true"
    >
        <key>
            <column name="documento_fk" />
        </key>
        <one-to-many
            class="vo.VersaoDocumento"
        />
    </set>
</class>
</hibernate-mapping>


VersaoDocumento
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
   
<hibernate-mapping>
<!--
    Created by Middlegen Hibernate plugin

    http://boss.bekk.no/boss/middlegen/
    http://hibernate.sourceforge.net/
-->

<class
    name="vo.VersaoDocumento"
    table="versao_documento"
>

    <composite-id name="comp_id" class="vo.VersaoDocumentoPK">
        <key-property
            name="id"
            column="id"
            type="long"
            length="8"
        />
        <!-- bi-directional many-to-one association to Documento -->
        <key-many-to-one
           name="documento"
           class="vo.Documento"
       >
           <column name="documento_fk" />
       </key-many-to-one>
    </composite-id>   

    <property
        name="descricao"
        type="java.lang.String"
        column="descricao"
        length="-1"
    />

    <!-- associations -->

</class>
</hibernate-mapping>


------------------
Testing code
------------------

Code:
( ... )
session = sessionFactory.openSession();
transaction = session.beginTransaction();
         
if((Documento)session.get(Documento.class, documento.getId()) == null) {
   throw new ObjectoNaoEncontradoException("Document doesn't exist.");
}
         
session.saveOrUpdate(documento);
         
if(documento.getVersaoDocumentos() != null) {
   VersaoDocumento versao_doc = (VersaoDocumento)documento.getVersaoDocumentos().iterator().next();
   session.saveOrUpdate(versao_doc);
}

transaction.commit();
( ... )


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 7:34 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You are doing some really strange crap here ...

Code:
( ... )
session = sessionFactory.openSession();
transaction = session.beginTransaction();
         
if((Documento)session.get(Documento.class, documento.getId()) == null) {
   throw new ObjectoNaoEncontradoException("Document doesn't exist.");
}
         
session.saveOrUpdate(documento);

Why? First you assert the object exists, and afterwards you do saveOrUpdate()? You allready know you will allways get an update, so use update()

Code:
     
if(documento.getVersaoDocumentos() != null) {
   VersaoDocumento versao_doc = (VersaoDocumento)documento.getVersaoDocumentos().iterator().next();
   session.saveOrUpdate(versao_doc);
}


Be careful with composite ids and saveOrUpdate. specify the unsaved-value mappings.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 7:43 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
gloeglm wrote:
You are doing some really strange crap here ...

Code:
( ... )
session = sessionFactory.openSession();
transaction = session.beginTransaction();
         
if((Documento)session.get(Documento.class, documento.getId()) == null) {
   throw new ObjectoNaoEncontradoException("Document doesn't exist.");
}
         
session.saveOrUpdate(documento);

Why? First you assert the object exists, and afterwards you do saveOrUpdate()? You allready know you will allways get an update, so use update()


Is it that strange?
I'm using this method for some months know.
Need to say that I'm using the saveOrUpdate() associated with the jdbc.batch_size=0. This way I only have update's with saveOrUpdate(). Got this at the forum some time ago when I was starting in the Hiberante world. :-)

If I assign the unsaved-value correctly I won't have this exception?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 7:49 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You will still have it, but your code is strange nontheless. The reason for the exception is:

Code:
session.get(Documento.class, documento.getId())

returns a Documento and associates it with the session.

Then you try calling update() with another Documento instance with the same id. Thats not allowed. You must not load a object and then call update with another one using the same id afterwards while the first is still associated with the session. Either evict the first one again, or use a find() which just does a count or projetion to the id or such.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 7:53 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
gloeglm wrote:
You will still have it, but your code is strange nontheless. The reason for the exception is:

Code:
session.get(Documento.class, documento.getId())

returns a Documento and associates it with the session.

Then you try calling update() with another Documento instance with the same id. Thats not allowed. You must not load a object and then call update with another one using the same id afterwards while the first is still associated with the session. Either evict the first one again, or use a find() which just does a count or projetion to the id or such.



You are absolutely right.
Did an evict to it and it worked well.

Thanks very much for the help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.