5

I've got two objects a parent and a child list. In my fluent nhibernate mapping for the parent I want to load the list of the children.

However I want this to be conditional, a column in the child table is called "IsDeleted" and I only want to return the children where "IsDeleted" is false.

Is it possible to set up a mapping to do this? If not is it possible to do it in just standard nhibernate?

Thanks

lancscoder
  • 8,658
  • 8
  • 48
  • 68

1 Answers1

12

Yes, you can use a Where constraint in Fluent NHibernate to map this. Somehting like:

HasMany(x => x.Children).Where("IsDeleted = 0");

The Where constraint should use SQL syntax not HQL. For tables that allow soft deletes it's probably easier to map a view that filters the deleted records out.

Jamie Ide
  • 48,427
  • 16
  • 81
  • 117
  • Thank, that's what I needed but how do I specific the column in the parent object to pass to the child? If its a reference its something like References( a => a.Supplier ).Column( "SupplierNo" ); but HasMany doesn't have a Column property. – lancscoder Feb 23 '10 at 16:38
  • I'm not sure I understand the question and I'd need to know more about your model and database to give a good answer. HasMany has a KeyColumn method if you're asking how to specify the FK column on a child object. – Jamie Ide Feb 23 '10 at 16:43
  • No I want to specify the column on the parent object as it doesnt use the primary key. My parent object has a pk, name, secondary id, this secondary links to the FK in the child. I know its not a good db design but its what I've inherited. So when I do a HasMany its passing my PK value when I want it to pass my secondary ID. Thanks – lancscoder Feb 23 '10 at 20:01
  • OK, I understand. It appears that Fluent NHibernate has added a PropertyRef method to HasMany but it's not in 1.0RTM you'll need to get a more recent build. See http://groups.google.com/group/fluent-nhibernate/browse_thread/thread/7dbe122aba8c2620/c786f369ed1ce117?show_docid=c786f369ed1ce117. You should post this as a new question so that a search will find it. – Jamie Ide Feb 23 '10 at 22:53
  • Thanks for the link and advice. Upgrading to the latest build and propertyref worked perfectly. Thanks – lancscoder Feb 24 '10 at 09:53