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)
------------------------------------------
DocumentoCode:
<?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();
( ... )