-->
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.  [ 3 posts ] 
Author Message
 Post subject: Why are shared references to collections bad?
PostPosted: Wed Nov 30, 2005 9:16 am 
Regular
Regular

Joined: Sat Nov 19, 2005 2:46 pm
Posts: 69
This conversation thread explains the cause of the exception "Found shared references to a collection":

http://forums.hibernate.org/viewtopic.php?t=944335

However, I'm curious as to why shared references to collections are a problem? Is there something inherent in DB relations that make it impossible, or is it a design decision by the Hibernate team to encourage best practice?
In short, why does Hibernate throw an exception in this circumstance?

I can think of several examples of object model where you might want different entities to reference collections that end up the same. An example might be product SKUs in stock (on the warehouse object) and product SKUs manufactured by ACME (on the manufacturer object) where all that's left in stock are ACME products.

How does hibernate cope with this - or have I missed something in understanding mapped associations?

Thanks,

_________________
Stewart
London, UK


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 10:58 am 
Senior
Senior

Joined: Mon Aug 22, 2005 5:45 am
Posts: 146
There are numerous issues involved.
According to my experience you are free to reference instances in different collections at the same time as long as they are attached (via proxy) to the same hibernate session.
You gonna run into severe trouble once you need to re-attach a persistent collection to a new session and an instance contained in this collection is referenced elsewhere in another session.

hth.

_________________
Please don't forget to give credit, if my posting helped to solve your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 11:14 am 
Regular
Regular

Joined: Sat Nov 19, 2005 2:46 pm
Posts: 69
Is that so? If this is true, then it's news to me. This would explain why I've sometimes been getting session errors, and sometimes shared reference errors.

Presumably I can share as many references as I like if I can sort out the session situation?

It's not what seems obvious to me, based upon what I've experienced though. I would expect that the statement:
Code:
session.createCriteria(clazz).list();
would all occurr within the same session, but I was getting a "collection is not associated with any session" because that class contains a list of references to all instances of itself - meaning each object refers to the same list - a perfectly feasible thing in "unpersisted" java. (If I add a restriction to the above criteria, the exception becomes "Found shared references to a collection".

_________________
Stewart
London, UK


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