Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Found two representations of same collection:

 
Robert Fry
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh nooo this dreaded exception. I've looked on the internet for help on this one and found only great scratching of heads. Including on here in the past, but I shall try anyway!

First off, the relevant annotations.



(Odd with the cascade annotations but that worked before, one of them is presumably unncessary).



The problem - I'm trying to code up an undo command, to undo deleting a floor.
First off the deletion bit works fine. Its just a Session.delete call on a floor object.

The undo code works by keeping a reference to the (detached) floor object. Should the user want to undo the deletion, essentially the code just calls Session.saveOrUpdate on the detached, saved object, which should reattach the object to the persistence context. This works with other persistent objects so I presume this is indeed the way to go.

However when I undo a floor object deletion, I get the exception, "Found two representations of the same collection", the collection in question being "txes" in the code above.
It seems to be a session caching issue because the SQL back end doesn't have any entries in it (the deletion itself works fine, as mentioned), its just that the persistence context refuses to 'let go' of the collection in the floor. Even calling Session.clear() does not resolve the problem. Even more oddly the collection doesn't actually have any objects in it necessarily, so its empty, yet the error still occurs.

I'm presuming that this is a mapping problem (again - ) with the cascading options.

Looking at what it produces...

Hibernate: insert into FLOOR (FLOOR_ID, BUILDING_ID, FLOOR_TREEINDEX, FLOOR_IMAGE_SOURCE, FLOOR_NAME, FLOOR_ORIGIN_X, FLOOR_ORIGIN_Y, FLOOR_SCALE_MULTIPLIER) values (null, ?, ?, ?, ?, ?, ?, ?)
Hibernate: call identity()
Hibernate: delete from FLOOR where FLOOR_ID=?
Found two representations of same collection: com.preciselyso.planview.datamodel.Floor.txInFloor

The undo code itself isnt very interesting - essentially all that goes on is a copy of the deleted object has saveOrUpdate called on it. The txes array in the detached object is empty, though not null, if you follow my meaning...
 
Robert Fry
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was doing some other stuff but am now back my good friend, Hibernate.

Curiouser and curiouser, this is a bit of test code



And this spits out

Session contains deleted object? false
List of saved floor Tx objects. []
Is this also in the cache? false
Found two representations of same collection: com.preciselyso.planview.datamodel.Floor.txInFloor (this one being dropped by the other thread)

Gah. It isn't in it, yet it complains it is. What is it doing..?
 
Robert Fry
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I fixed it! I found the solution to be quite odd.

Basically the problem was in the saving. Session.saveOrUpdate() was being called on the floor, and that was causing the error.

What I actually needed to do was first called Session.merge() and then after that call Session.saveOrUpdate(). I think this is very odd but that is definitely what is needed, minus merge gives you the error, and minus saveorupdate means it doesn't persist, despite what merge implies.

I was hinted at this route by looking in the good book, Java Persistence with Hibernate, which implies that only merge or update are acceptable ways to reattach detached objects. As saveOrUpdate was neither of these (I thought it would count as update hence why I used it here) I investigated further.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic