views:

294

answers:

2

hi all,

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

+1  A: 

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.

Michael Gattuso
but why would this happen? I am not changing anything so whats the update for?in any case, i changed it to cascade=none and it made no difference.thanks for your reply
Yannis
A: 

it turns out that an enum field in ContactInfo was the problem. i didnt mind if that particular filed was a string so changing it resolved this issue.

Yannis