3

I have a many-to-many association defined like:

Parent.hbm.xml:

    <set name="children" table="child_parent_map" lazy="true">
        <cache usage="nonstrict-read-write" />
        <key column="parent_id" />
        <many-to-many class="Child">
            <column name="child_id" not-null="true" index="child_parent_by_child"/>
        </many-to-many>
    </set>

Child.hbm.xml:

    <set name="parents" table="child_parent_map" lazy="true">
        <cache usage="nonstrict-read-write" />
        <key column="child_id" />
        <many-to-many column="parent_id" class="Parent"  lazy="false"/>
    </set>

I am quite sure I am initializing Parent.children by walking the collection. Something like:

for(Child child : parent.getChildren()) {
    Hibernate.initialize(child.getAnotherProperty());
}

Parent has six children. However, in one session parent appears to have only five, and in another (2 seconds later, nothing changed in DB or in another session) - all six. Actually, I discovered it after detaching these entities from session with a custom cloner.

I thought that lazy collections are either completely initialized (i.e. all elements are), or not. Is it possible that somehow only a part of the collection was initialized? Can it be an issue with caching?

EDIT: This session handles a fairly large data set (a few thousands of entities). Is it possible that this is because some already-loaded entities got evicted from the session?

4
  • 2
    Have you set some value to the property "default_batch_fetch_size" ? Commented Mar 5, 2011 at 7:57
  • 1
    Could you post the code for Parent and Child? I'd start with looking at hashCode() and equals(). Commented Mar 7, 2011 at 21:13
  • Michal is right, you probably have something in hashCode() and/or equals() that causes your object to behave strangely. Commented Mar 8, 2011 at 11:06
  • I did have an ID-based equals and hashCode. When I removed it the issue disappeared. Thanks guys, feel free to post it as the answer. Commented Mar 8, 2011 at 11:18

1 Answer 1

1

Start by checking your hashCode() and equals() methods, incorrect implementation of these methods often cause this kind of behavior.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.