littlecharva
littlecharva

Reputation: 4244

NHibernate apply filter to bag

I have a parent/child relationship I'm mapping in XML. A Booking can have many payments, so my Booking model has a IList Payments {get;set;} property and I'm mapping it in XML like this:

<bag name="Payments" inverse="true">
  <key column="BookingId"/>
  <one-to-many class="Payment, NHibernateOneToMany"/>
</bag>

This works fine, but now I need to introduce the concept of a soft-delete, so I've added a Deleted bool column to Payment and I want the Payments on Booking to only contain the non-deleted ones, so I want to do something like this:

<bag name="Payments" inverse="true">
  <key column="BookingId"/>
  <one-to-many class="Payment, NHibernateOneToMany"/>
  <sql>SELECT * FROM Payment WHERE Deleted = 0 AND BookingId = bookingId</sql>
</bag>

I've looked into using Filters, but they only seem to offer the ability to use the filter if enabled - I want this property to exclude all deleted items EVERY time, so there's no chance a mistake could be made down the line.

What's the best way to achieve this?

Upvotes: 1

Views: 705

Answers (1)

Petr Adam
Petr Adam

Reputation: 1437

You can use simple where of the bag mapping. If Deleted is a mapped property on the Payment, then this mapping should work:

<bag name="Payments" inverse="true" where="Deleted = 0">
  <key column="BookingId"/>
  <one-to-many class="Payment, NHibernateOneToMany"/>
</bag>

Upvotes: 5

Related Questions