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