views:

199

answers:

2

so, i am really clutching at straws for ideas here. (also note: this is an intermittent problem)

i have a 'parent' object with a collection of 'child' objects:

<parent stuff>
...
<set name="children" inverse="true" order-by="child_id">
<cache usage="read-write"/>
<key column="parent_id"/>
<one-to-many class="ChildClass"/>
</set>
...
</parent>

<child stuff>
...
<many-to-one name="parent"
column="parent_id"
insert="true"
update="true"
not-null="true"
class="ParentClass"
index="idx_child_parent"/> ...
</child>

if i run a query that is like: "select children from ChildClass child " + "where child.parent = :parent"; it finds all the children. perfect.

however, if i search for the parent object (and find it), hibernate goes through and creates my ParentClass, but only populates ONE (the first) child of children. i actually debugged into the hibernate code and found that infact the call to setChildren() on ParentClass was only being sent a 1 element collection - when i know there are more from the first query.

so, i am using EHCache, and clearing it does no good: calling CacheManager.getInstance().clearAll();

restarting my application DOES work. only for it to break shortly afterwards on a seemingly random instance of a parent.

there isnt anything (that i know of) that is happening to directly cause this. does anyone know of a hibernate bug that would cause this, or an EHCache bug. am i calling clear properly on my cache?

any pointers would be good.

i am using hibernate 3.2.6, and the same problem is happening on mysql and postgres.

edit: i should also add that this collection with only 1 element is in a sort of 'never existed' state. ie. there was never a transaction that resulted in only 1 child element

+1  A: 

Does your Child class implement hashcode() and equals() correctly? Could it be possible that Hibernate is seeing multiple Child classes that are attached to the same Parent as equivalent and thus only persisting one of them?

matt b
sorry for the dull comment, but `hashCode()` :)
Bozho
if these were incorrect, it would be (presumably) consistently returning the wrong children. however, i double checked anyway, and they are correct.thanks for the idea
gcrain
A: 

It turns out that at one point we are doing a "join fetch" when getting a Review. A join fetch means that, for the collection of children, only the children that match my where clause are returned. So say I have 3 children > 10 and 1 child < 10, if my where clause says child < 10, it returns the parent object, with a set of children containing ONLY that one child. Thats fine, thats what "join fetch" is for.

The things thats wrong is that Ehcache doesnt know that this parent object is "incomplete", and so it stores this incomplete value in the cache. Next time i want the "full" parent object, i dont get it, i get the incomplete one, and Hibernate doesnt tell me (doesnt know) otherwise.

edit: note, it was just by chance that every time it happened, it was only 1 child element. it possible for there to be more.

gcrain