I'm getting the following error:
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: pt.comseal.arsol.vo.VersaoDocumentoPK@b113c7[id=1,documento=pt.comseal.arsol.vo.Documento@1cf662f[id=2]], of class: pt.comseal.arsol.vo.VersaoDocumento
at net.sf.hibernate.impl.SessionImpl.checkUniqueness(SessionImpl.java:1605)
( ... )
To try to solve this I did a "session.evict(doc_aux);" below in the "Method code" section but with no success.
I also tried to use a second Session and going a "get" on the
Documento with that session instance and still with no success.
I first do a "get" on
Documento to see if it exists and then I do a "get on
VersaoDocumento to see if it exists also. The second "get" is the one that's throwing the exception because
VersaoDocumento has a composite-id
VersaoDocumentoPK which has a
Documento as one of its components.
Please help. Don't understand the error.
------------------------------------------
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-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>
-------------------
Method code
-------------------
Code:
( ... )
SessionFactory sessionFactory = HibernateFactory.createFactory();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
Documento doc_aux = (Documento)session.get(Documento.class, documento.getId());
if(doc_aux == null) {
throw new ObjectoNaoEncontradoException("Error.");
}
VersaoDocumento versao_doc = (VersaoDocumento)documento.getVersaoDocumentos().iterator().next();
session.evict(doc_aux);
if((VersaoDocumento)session.get(VersaoDocumento.class, versao_doc.getComp_id()) == null) {
throw new ObjectoNaoEncontradoException("Error.");
}
session.saveOrUpdate(documento);
session.saveOrUpdate(versao_doc);
transaction.commit();
session.close();
( ... )