R – NHibernate: Change from lazy=true to fetch=join brings back the world

nhibernatenhibernate-mapping

I have a User object/mapping in my application. Each user has a list of contact information (phone, email etc)

the mapping for the user contains:

<bag name="ContactInfo" table="contact_info" lazy="true" cascade="all">
    <key column="contact_id"/>
    <one-to-many class="...ContactInfo, ..."/>
</bag>

this works fine but i get the n+1 select problem so i need to optimize it a little bit. But for some reason, when I change this to a join and perform some db operation, NH starts updating ALL contact_info objects in the database. When i say db operation i dont mean changinf a contact. i mean anything.

Anyone knows why? thx

EDIT: Just realized that it does it for lazy="true" as well but the second time, after the objects have been loaded. the question of why remains

Best Answer

I'm wondering if your cascades are causing the issue. Do you have cascade=all on your entire graph? If so you may want to re-evaluate your lifecycle strategy.

Here's a though from section 9.9 of NHibernate 1.2 reference (emphasis added)

Mapping an association (many-to-one, or collection) with cascade="all" marks the association as a parent/ child style relationship where save/update/deletion of the parent results in save/update/deletion of the child(ren). Futhermore, a mere reference to a child from a persistent parent will result in save / update of the child.

Related Topic