-->
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.  [ 14 posts ] 
Author Message
 Post subject: Problems with cascade many-to-many relationship
PostPosted: Wed Jun 16, 2004 10:09 am 
Newbie

Joined: Wed Jun 16, 2004 9:12 am
Posts: 9
Hi,
sorry for bothering you with another question regarding many-to-many relationships and cascading but since I have not found a solution for this problem, I dare to ask...

I have a quite simple inheritance hierarchy:

AbstractResource (top-level superclass)
- ID : long
- Title : String

Resource (extends AbstractResource)
- referencedBy : Set

Book, Serial, Article... (extends Resource)

There is a many-to-many relationship between Resource and AbstractResource stating that a Resource can be referencedBy other AbstractResources. The idea behind using AbstractResource is, that some information about a Resource might be missing but at least the title is mandatory. This allows for storing the title of the referencing entity but not "really storing" it in the system. So the business objects are restricted to Resources and their children.

The use case is as follows:

Create a new concrete resource (i.e. book, article, ...) and add some "referencing" AbstractResource to the set (referencedBy).
Save the newly created concrete resource and its "referencedBy abstract resources).

Using the following hibernate mappings...

Code:
<hibernate-mapping>
<class name="AbstractResource" table="abstractresource">
   <id name="id" column="ResourceID" type="java.lang.Long">
      <generator class="native"/>
   </id>   
   
   <property name="title" column="Title"/>

   <joined-subclass name="Resource" table="resource">
      <key column="ResourceID"/>
         <set name="referencedBy" table="referencedBy" cascade="all">
            <key column="ResourceID"/>
            <many-to-many column="ResourceID2" class="AbstractResource"/>      
         </set>

         <!-- omitted the other joined-subclasses on purpose, since they are not part of the problem -->

   </joined-subclass>
</class>
</hibernate-mapping>


I planned to use this java statements to store the objects:
Code:
session.save(r);
session.flush();


...I get the following error resp. StackTrace...
Code:
Hibernate Exception: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) for de.tub.cis.emn.prototype.model.resource.Resource instance with identifier: 0
java.lang.RuntimeException: net.sf.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) for de.tub.cis.emn.prototype.model.resource.Resource instance with identifier: 0
   at de.tub.cis.emn.prototype.db.resource.ResourceService.saveResource(ResourceService.java:340)
   at test.Test.main(Test.java:28)
Caused by: net.sf.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) for de.tub.cis.emn.prototype.model.resource.Resource instance with identifier: 0
   at net.sf.hibernate.persister.AbstractEntityPersister.check(AbstractEntityPersister.java:501)
   at net.sf.hibernate.persister.NormalizedEntityPersister.update(NormalizedEntityPersister.java:693)
   at net.sf.hibernate.persister.NormalizedEntityPersister.update(NormalizedEntityPersister.java:668)
   at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
   at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2407)
   at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2361)
   at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2229)
   at de.tub.cis.emn.prototype.db.resource.ResourceService.saveResource(ResourceService.java:331)
   ... 1 more
Exception in thread "main"


The sql queries were:
Code:
Hibernate: insert into abstractresource (Title) values (?)
Hibernate: insert into resource (ResourceID) values (?)
Hibernate: update abstractresource set Title=? where ResourceID=?
(causing exception)


It seems as if one 'insert' statement is missing, namely the one that inserts the referencing AbstractResource.

I am using mysql-4.0.18 and Hibernate 2.1.3

Though I don't see the solution, I think it has to do something with cascading and a wrong described mapping.

Finally, let me apologize for my bad english and lots of thanks in advance...

Florian


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 16, 2004 10:22 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
show entire java code, especially begin and commit transaction

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 16, 2004 10:31 am 
Newbie

Joined: Wed Jun 16, 2004 9:12 am
Posts: 9
Code:
Session s = sessionFactory.openSession();
try{

   // already tried to insert beginTransaction method here
   session.save(r);
   // and transaction commit here

   session.flush();
}catch (HibernateException e){
   System.err.println("Hibernate Exception: " + e.getMessage());
   throw new RuntimeException(e);
}finally{
   if (session != null){
      try{
         session.close();
      }catch (HibernateException e){
         System.err.println("Hibernate Exception" + e.getMessage());
         throw new RuntimeException(e);
      }
   }
}


I am not quite sure if I need transactions right now, because I am the only user that works on the database. Nevertheless, for future times I would be glad to receive any advice (especially if it solves the problem described above).

Thanks,
Florian


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 16, 2004 10:33 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
if you don't have a transaction, you don't have a commit, you don't persist the change... and it's ugly ;(

then , read about "understanding flushing"

then you should understand that by default, you don't need to call flush but commit...

so try all this

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 16, 2004 10:33 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Use:

session.beginTransaction()
session.save()
tx.commit()

No need to flush explicitly.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 16, 2004 10:34 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
oh, and also read about threadlocal pattern, it's a strange word but it's very easy to use... you really have to understand what a hibernate session is

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 16, 2004 10:49 am 
Newbie

Joined: Wed Jun 16, 2004 9:12 am
Posts: 9
Unfortunately this did not fix the problem. I am still getting the same StackTrace. Nevertheless I will read the passages about "understanding flushing" and "threadlocal pattern"... I suppose both are within the manual?!

If you have any other ideas, I really appreciate any help...

Thanks,
Florian


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 16, 2004 10:52 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
threadlocal is on th site, check wiki page.

I have an idea... you may have two concurrent unclosed transaction doing twice the job or something like that

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 16, 2004 10:53 am 
Newbie

Joined: Wed Jun 16, 2004 9:12 am
Posts: 9
Sorry, did make a mistake. I got a different StackTrace:

Code:
java.lang.RuntimeException: net.sf.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) for de.tub.cis.emn.prototype.model.resource.AbstractResource instance with identifier: 0
   at de.tub.cis.emn.prototype.db.resource.ResourceService.saveResource(ResourceService.java:348)
   at de.tub.cis.emn.prototype.struts.action.resource.NewResourceAction.execute(NewResourceAction.java:123)
   at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
   at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
   at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
   at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:284)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:257)
   at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
   at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
   at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199)
   at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
   at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:195)
   at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164)
   at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
   at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
   at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
   at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
   at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
   at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:206)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:700)
   at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:584)
   at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
   at java.lang.Thread.run(Unknown Source)
Caused by: net.sf.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) for de.tub.cis.emn.prototype.model.resource.AbstractResource instance with identifier: 0
   at net.sf.hibernate.persister.AbstractEntityPersister.check(AbstractEntityPersister.java:501)
   at net.sf.hibernate.persister.NormalizedEntityPersister.update(NormalizedEntityPersister.java:693)
   at net.sf.hibernate.persister.NormalizedEntityPersister.update(NormalizedEntityPersister.java:668)
   at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
   at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2407)
   at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2361)
   at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2229)
   at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
   at de.tub.cis.emn.prototype.db.resource.ResourceService.saveResource(ResourceService.java:338)
   ... 31 more


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 16, 2004 10:55 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
<id name="id" column="ResourceID" type="java.lang.Long">
and you have an id = 0

are you sure in your pojo, the property is type Long?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 16, 2004 10:59 am 
Newbie

Joined: Wed Jun 16, 2004 9:12 am
Posts: 9
anthony wrote:
<id name="id" column="ResourceID" type="java.lang.Long">
and you have an id = 0

are you sure in your pojo, the property is type Long?


It's a typo. I am only using primitive datatype (long) for the ids.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 16, 2004 11:02 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
in the mapping file you have Long so unsaved value = null
long can't be null....

change long to Long in your POJO

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 16, 2004 11:03 am 
Newbie

Joined: Wed Jun 16, 2004 9:12 am
Posts: 9
Florian wrote:
It's a typo. I am only using primitive datatype (long) for the ids.


Now, I am confused ;) Hibernate seems to be able to unwrap long values from Long objects. At least it worked for several different (smaller) applications without problems. So, in the mapping files I always use the Long class as datatype while in the objects itselfs I use primitive datatypes.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 16, 2004 11:19 am 
Newbie

Joined: Wed Jun 16, 2004 9:12 am
Posts: 9
Found some additional mistakes that confused Hibernate. First of all, I changed long to Long. In addition there were some attributes in extending classes that overwrote the ones from the superclass. I think that was most stupid...

Now it is working perfectly! Lots of thanks.

Florian


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