-->
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.  [ 10 posts ] 
Author Message
 Post subject: Problems with transitive persistence
PostPosted: Sun Sep 05, 2004 7:21 pm 
Newbie

Joined: Sun Sep 05, 2004 6:54 pm
Posts: 3
I'm using hibernate 2.1, integrated with Spring. I'm having problems with transitive persistence. I have a many-to-many relationship, with a join table. I'm creating an "parent" object that has a collection of "children" objects, but when I try to save or update the parent object, anything happend with the children, just the others parent's attributes are saved. Any trace are printed. I have already tried use cascade="all-delete-orphan", cascade="save-update", but anything happend with the children.


<class
name="br.com.sansys.infraestrutura.dominio.SegUsuario"
table="SEG_USUARIO"
>

<id
name="idUsuario"
type="long"
column="ID_USUARIO"

>
<generator class="native" />
</id>

</set>
<!-- bi-directional one-to-many association to SegPerfilUsuario -->
<set
name="segPerfilUsuarios"
lazy="true"
inverse="true"
cascade="all-delete-orphan"
>
<key>
<column name="ID_USUARIO" />
</key>
<one-to-many
class="br.com.sansys.infraestrutura.dominio.SegPerfilUsuario"
/>
</set>
</class>


<class
name="br.com.sansys.infraestrutura.dominio.SegPerfilUsuario"
table="SEG_PERFIL_USUARIO"
>

<composite-id name="comp_id" class="br.com.sansys.infraestrutura.dominio.SegPerfilUsuarioPK">
<!-- bi-directional many-to-one association to SegPerfilAcesso -->
<key-many-to-one name="segPerfilAcesso" class="br.com.sansys.infraestrutura.dominio.SegPerfilAcesso"
>
<column name="ID_PERFIL_ACESSO" />
</key-many-to-one>
<!-- bi-directional many-to-one association to SegUsuario -->
<key-many-to-one
name="segUsuario"
class="br.com.sansys.infraestrutura.dominio.SegUsuario"
>
<column name="ID_USUARIO" />
</key-many-to-one>
</composite-id>


<!-- associations -->

</class>




We don't need to control the session because the Spring is suppose to do it for us.

There's no stack trace, just anything happend with the children objects

SQL Server 2000

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 06, 2004 10:45 am 
Regular
Regular

Joined: Tue Jul 13, 2004 2:27 am
Posts: 73
Location: Singapore
I am having the similar problem. Do not know why it is like this.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 06, 2004 11:39 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
your children equals hashcode implem is broken.
Please refer to the equals/hashcode page in the wiki area.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 06, 2004 1:37 pm 
Newbie

Joined: Sun Sep 05, 2004 6:54 pm
Posts: 3
emmanuel wrote:
your children equals hashcode implem is broken.
Please refer to the equals/hashcode page in the wiki area.


I'm using middlegen to generate my bean classes. The methods equals and hash code looks like this.

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
.
.
.

public boolean equals(Object other) {
if ( !(other instanceof SegPerfilUsuario) ) return false;
SegPerfilUsuario castOther = (SegPerfilUsuario) other;
return new EqualsBuilder()
.append(this.getComp_id(), castOther.getComp_id())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getComp_id())
.toHashCode();
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 06, 2004 2:34 pm 
Newbie

Joined: Sun Sep 05, 2004 6:54 pm
Posts: 3
To solve the problem I tried set the inverse attribute to false. The mapping looks like this.

<class
name="br.com.sansys.infraestrutura.dominio.SegUsuario"
table="SEG_USUARIO"
>

<!-- bi-directional one-to-many association to SegPerfilUsuario -->
<set
name="segPerfilUsuarios"
lazy="true"
inverse="false"
cascade="all-delete-orphan"
>
<key>
<column name="ID_USUARIO" />
</key>
<one-to-many
class="br.com.sansys.infraestrutura.dominio.SegPerfilUsuario"
/>
</set>

</class>
<class
name="br.com.sansys.infraestrutura.dominio.SegPerfilUsuario"
table="SEG_PERFIL_USUARIO"

>

<composite-id name="comp_id" class="br.com.sansys.infraestrutura.dominio.SegPerfilUsuarioPK">
<!-- bi-directional many-to-one association to SegPerfilAcesso -->
<key-many-to-one
name="segPerfilAcesso"
class="br.com.sansys.infraestrutura.dominio.SegPerfilAcesso"

>
<column name="ID_PERFIL_ACESSO" />
</key-many-to-one>
<!-- bi-directional many-to-one association to SegUsuario -->
<key-many-to-one
name="segUsuario"
class="br.com.sansys.infraestrutura.dominio.SegUsuario"
>
<column name="ID_USUARIO" />
</key-many-to-one>
</composite-id>


<!-- associations -->

</class>

Something different happened, when I tried to save a SegUsuario object (parent), the hibernate tried to update the SegPerfilAcesso object (child) instead of insert, of course that an exception was threw because there's no register in database for that.

Thanks in advanced.

Hibernate SQL:
Hibernate: insert into SEG_USUARIO (NU_MATRICULA, NM_USUARIO, NM_LOGIN, NM_SENHA, NM_CPF, NM_EMAIL, FL_COLABORADOR, ID_ESTRUTURA_EMPRESA, CH_CARGO, CH_ATIVO) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) select scope_identity()
Hibernate: update SEG_PERFIL_USUARIO set ID_USUARIO=? where ID_PERFIL_ACESSO=? and ID_USUARIO=?

full stack trace
org.springframework.orm.hibernate.HibernateSystemException: SQL insert, update or delete failed (row not found); nested exception is net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:524)
at net.sf.hibernate.impl.ScheduledCollectionRecreate.execute(ScheduledCollectionRecreate.java:23)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2418)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2375)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2240)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at org.springframework.orm.hibernate.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:386)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:316)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:211)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:138)
at br.com.sansys.infraestrutura.util.interceptador.SansysInterceptador.invoke(SansysInterceptador.java:45)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:138)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:148)
at $Proxy0.gravar(Unknown Source)
at br.com.sansys.seguranca.apresentacao.ManterUsuarioAction.gravar(ManterUsuarioAction.java:99)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:309)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:231)
at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:131)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:449)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:264)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1176)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:472)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:536)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 06, 2004 4:28 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Put inverse="false" but add cascade="save-update" if you want the children to be saved when the parent is.

For your current problem this is an incorrect unsaved-value setting, check the FAQ on that subject

_________________
Emmanuel


Top
 Profile  
 
 Post subject: child is saved but foreign key is not set
PostPosted: Tue Sep 07, 2004 8:33 am 
Regular
Regular

Joined: Tue Jul 13, 2004 2:27 am
Posts: 73
Location: Singapore
yes, after changing inverse to false and cascade to save-update, I can see that the child is saved when I just call saveParent.
However, noticed that the foreign is not updated correctly (still NULL).

My parent has a list of children, something like this:
parent {
protected List children = new ArrayList();
......
}

The children contains a list of Children:
children {
//properties

}

My parent.hbm.xml is something like this:
-------------------------------------------------
<class name="Parent" table="parents">
<id name="id" type="long" column="TID" unsaved-value="0">
<generator class="identity"/>
</id>

<property name="name" type="string" not-null="true" column="NAME"/>
<set name="children" table="childrens" cascade="save-update" inverse="false" lazy="true">
<key column="TID"/>
<one-to-many class="net.canal.core.persistence.AttributeGroup"/>
</set>

</class>
-----------------

Child hbm file:
-----------------
<class name="children" ble="childrens">
<id name="id" type="long" column="AGID" unsaved-value="0">
<generator class="identity"/>
</id>

<property name="name" type="string" not-null="true" column="NAME"/>

</class>
-----------------


So it is unidirectional association. before I save the parent, I put the children into the list. Whatelse do I need to do ?

thanks
li xin


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 07, 2004 8:35 am 
Regular
Regular

Joined: Tue Jul 13, 2004 2:27 am
Posts: 73
Location: Singapore
part of the log file where I suspect that some problem happened:

......
07.09.04 20:18:00 [http8080-Processor24] DEBUG net.sf.hibernate.impl.SessionImpl - Flushing entities and processing referenced collections
07.09.04 20:18:00 [http8080-Processor24] INFO org.springframework.transaction.interceptor.TransactionInterceptor - Invoking rollback for transaction on method 'saveParent' in class [net.canal.core.service.ParentManager] due to throwable [java.lang.ClassCastException]
......

it seems that there is a Class cast problem. not sure if it causes the problem. Will debug.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 07, 2004 11:36 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Oh, if it is a unidir, the set inverse="false". I thought you were using a bidir one.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 08, 2004 10:25 am 
Regular
Regular

Joined: Tue Jul 13, 2004 2:27 am
Posts: 73
Location: Singapore
I found the problem for my case: the hbm uses 'set' but the Java codes use 'list'. This cause the ClassCastException and the result is that the records are inserted into the table but the foriegn keys are not updated (still null).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.