-->
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.  [ 13 posts ] 
Author Message
 Post subject: One to Many Association problem
PostPosted: Wed Sep 24, 2003 9:07 am 
Newbie

Joined: Wed Sep 10, 2003 10:24 am
Posts: 5
Hi All,

I got a problem when I was mapping two associations 1:N from one entity and I'd like to know if you have an solution. It seens to be quite simple.

The diagram is like that: B <-*------ A ------*--> C

But when I try so make some operation with C the following exception is raised by the persistence layer:

"...Found shared references to a collection"



The descriptor of A is the following:

<hibernate-mapping>
<class name="A" table="a">
<id name="oid" type="long" column="id" unsaved-value="0" >
<generator class="identity"/>
</id>

<property name="name">
<column name="name" sql-type="varchar(64)" not-null="true"/>
</property>

<set name="bs" table="operacao" inverse="false" cascade="all-delete-orphan">
<key column="id_a"/>
<one-to-many class="b"/>
</set>

<set name="cs" table="c" inverse="false" cascade="all-delete-orphan">
<key column="id_a"/>
<one-to-many class="c"/>
</set>

</class>
</hibernate-mapping>


What should I do ?

Thanks in adv

Gustavo


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2003 10:29 am 
Newbie

Joined: Wed Sep 10, 2003 10:24 am
Posts: 5
Could it be raised beacause, probabily, there are two instances of A loaded in memory ?

As it is stated in Hibernate Reference, 5.4: "An instance of the contained entity class may not belong to more than onte instance of the collection".

Since A holds collections of B and C, it would be the possibly cause.

If my assumption is correct, the question is: Why hibernate loads to instances of A ?


Thanks

Gustavo


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2003 10:32 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The Hibernate documentation explains that you may not have two persistent object which refer to the same collection instance. The error message is very self-explanatory: you have two references to a single collection.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2003 10:50 am 
Newbie

Joined: Wed Sep 10, 2003 10:24 am
Posts: 5
But the problem is that I have only one persistent object that holds two diferent collections. 'A' holds two collections 'bs' and 'cs'. The first holds instances of 'B'. The second holds instances of 'C'.
I couldn't understand yet where are the shared references.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2003 11:01 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Ummm. I think I just explained to you that that is NOT the problem.

I suggest you get out your debugger and look for shared references TO collections.


Top
 Profile  
 
 Post subject: 'Shared References' bug?
PostPosted: Fri Oct 03, 2003 1:42 am 
Newbie

Joined: Fri Oct 03, 2003 1:10 am
Posts: 10
I believe that Gustavo has found a real problem here.

We have had similar situations where Hibernate throws the "Found shared references to a collection" exception when we are certain that we do not have two entities sharing a persistent collection, nor a circular reference in our data model.

I have tweaked the Hibernate code that is striving to protect itself from "silly users who set up circular or shared references between/to collections" (see SessionImpl.java). When I change the "updateReachableCollection()" method to simply return instead of throwing the exception, everything works fine.

One way to get the problem seems to be to override an accessor of a persistent class such that the override does an HQL query. For some reason(?) Hibernate calls the accessors of an entity as the entity is being hydrated, and when this one is invoked, the result is a 'query within a query' which leads to the above problem. (Why does it invoke the accessors? I want to write expensive computations into these so that they look like simple properties to users of the entity, but can't afford to have Hibernate calling them behind my back.)

Overall we are very high on Hibernate and have just made a complete conversion from Entity Beans to Hibernate + Spring AOP in a large project we have in development.

If one of the Hibernate folks wants a test case to demonstrate the above problem, we can assemble one next week.

Jon


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 03, 2003 5:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
One way to get the problem seems to be to override an accessor of a persistent class such that the override does an HQL query.


You call queries from accessor methods?!

Don't.

If you really must do this kind of thing use Hibernate 2.1's new direct field access feature. But really your domain model should not be written to depend upon Hibernate APIs.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 03, 2003 9:06 am 
Newbie

Joined: Fri Oct 03, 2003 1:10 am
Posts: 10
You call queries from accessor methods?!

Don't.


Recall the 'formula' attribute of a mapped property where you can encode SQL, along with the update=false, insert=false attributes. What I am talking about here is just an elaboration of this idea, which I call a computed field.

If a property is declared with (update=false, insert=false) it seems to me that Hibernate has no reason to ever invoke the accessor. If it would not do so, problem solved.

In any case, I'll try to find a simpler example where Hibernate incorrectly detects a shared reference to a collection.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2003 4:44 pm 
Newbie

Joined: Fri Oct 03, 2003 1:10 am
Posts: 10
I've now concluded that making HQL queries in methods that are mapped by Hibernate was the root of my problems. Doing so lead to re-entrance into the Hibernate session code. Even though we found ways of avoiding the (erroneous) 'shared reference to a collection' Exception, silent corruption of the session took place instead. This lead to objects not getting persisted and other mayhem.

It would be nice if Hibernate would detect re-entrance and throw a meaningful Exception, instead of allowing silent corruption - just in case someone else falls into this trap.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2004 6:45 pm 
Beginner
Beginner

Joined: Wed Jan 28, 2004 2:06 am
Posts: 33
Can anybody let me know how the "shared reference to a collection" problem was solved. Please do.

Thanks

Kic


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 10:28 am 
Newbie

Joined: Fri Oct 03, 2003 1:10 am
Posts: 10
The solution is to get rid of your collection and use a HQL query to obtain the set of child objects. I.e., work around this bug that Mr. King refuses to acknowledge.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 10:31 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
jonjs, comments like that will get you banned superfast.


Of course you should not access the session from an accessor method used by Hibernate. Of course this is not a bug.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 11:17 am 
Newbie

Joined: Fri Oct 03, 2003 1:10 am
Posts: 10
Reprimand accepted, will try to be more P.C.

It's just that another member of our team wasted several hours on a wild goose chase due to the message ""Found shared references to a collection" when the actual problem was unsafe use of the session.


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