Hibernate version:
Annotations 3.2.0.CR1 + compatible Hibernate (3.2.1CR4?)
The system I am building has 40 persistent types with many different kinds of relationships. I have been wrestling with a problem for some time: I am having trouble with a transitive ManyToMany collection - it is probably my lack of understanding of one of the subtler aspects of Hibernate so a little help and guidance would be appreciated.
The broad overview of this section is roughly:
MonsterObject -> ObjectCollection -> GameObject
monster -> contents -> it's stuff
A MonsterObject (a derived class from GameObject) holds a collection of other GameObjects (it's "inventory")
The definition for the contents collection within the MonsterObject class is:
@ManyToMany ( fetch=FetchType.EAGER, cascade = { CascadeType.ALL} )
@JoinTable ( name="collection_objs" )
public List<GameObject> getContents() {
#1) When the MonsterObject is deleted(), it's contents should also be deleted(), so the collection is marked CascadeType.ALL. However, when the delete() happens I get an exception:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:93)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:79)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:92)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:87)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:218)
This suggests that an object that has already been deleted is being deleted again. However, from looking at the data, this is not the case unless one of the contained object in this collection is being deleted twice. GameObjects without contained objects delete fine so this collection is the culprit. What I'd like to know is is there another interpretation for this exception?
#2) Interestingly (and perhaps unrelatedly), if I change the definition for this collection to:
@ManyToMany ( fetch=FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE } )
@JoinTable ( name="collection_objs" )
public List<GameObject> getContents() {
I get a TransientObjectException trying to persist the MonsterObject instead - does this cascade definition not indicate the contents of the collection should be transitively persisted? I have been working with this version of Hibernate for some months and have noticed that if you change from CascadeType.ALL to any other setting, transitive persistence is often lost. Is this the expected behaviour i.e. there is more to CascadeType.ALL than just { CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE, CascadeType.REMOVE } ?
Thanks,
Matt
|