Development With A Dot

Blog on development in general, and specifically on .NET

Sponsors

News

My Friends

My Links

Permanent Posts

Portuguese Communities

NHibernate Pitfalls: Adding to a Bag Causes Loading

Updated. Thanks Kenneth!

This is part of a series of posts about NHibernate Pitfalls. See the entire collection here.

When adding an item to a one to many uninitialized lazy bag, all items are loaded. You would expect this to happen in a set, because NHibernate must check that the new item does not already exist in the database, but not on a bag.

The way to prevent this from happening is to specify lazy=”extra” in the mapping for the bag collection:

   1: <bag cascade="all" inverse="true" lazy="extra" name="Details">
   2:   <key column="`ORDER_ID`" />
   3:   <one-to-many class="OrderDetail" />
   4: </bag>

Bookmark and Share

Comments

Kenneth said:

But you are showing at set collection in the example?

# June 10, 2011 2:26 AM

Ricardo Peres said:

@Kenneth:

Thanks! My fault! ;-)

Are you enjoying this series?

RP

# June 10, 2011 4:42 AM