views:

76

answers:

1

Hi, I'm looking for way in Fluent NHibernate to get a list of all object of type PARENT using criteria. I have a father object and a derived child. The father contains a list of childs.

The problem is that when I use:

ICriteria crit = session.CreateCriteria(typeof(Parent))
IList<Parent> myRes = crit.List<Parnet>()

NH return back the list of both parent elements and the derived children elements, which is "right" b/c that is what I've asked, but that is not what I need. (the children elements should be only inside the father object, but since they are of type parent as well - since they derived from it... NH brings them as well using this method.)

How can I get the list of all my "father" elements without the derived children ?

This is from the first answer (@Stefan Steinegger's)

session
  .CreateQuery("from Parent where Parent.class == :class")
  .AddType(typeof(Parent));

It looks like I need something like that - but it doesn't work in Fluent NHibernate.

Thanks, Dani

A: 

the question actually is: how do you determine if a Parent is a root parent? there are various approaches:

  • You keep your model and define: a root is a Parent that is not inherited and is not included in any other Parent.

The part "is not inherited" might be easy to determine, but is actually a poor definition. When using inheritance, you should actually not care if an object you get as a certain type is actually inherited or not, this is the nature of inheritance.

The part "is not included in any other Parent" is hard to find out in an efficient way.

  • You set a reference to an objects parent. A root is a Parent where its parent references null.

  • You derive your Root from a common Base class. A Child is not a Root anymore, and a Root is a Root.

I suggest to take the last option.


BTW: you can filter for the exact type, but only using HQL.

session
  .CreateQuery("from Parent where Parent.class == :class")
  .AddType(typeof(Parent));
Stefan Steinegger
Thanks for the answer.Actually - the parent object has no property of "ParentSeqID" as it's only the child's property. (or a property of parent which maps to ParentSeqId)How do I add this to the criteria ? is there a "has no property" option ?
Dani
I think you can't. You can only filter for a type using HQL, I added another section to my answer.
Stefan Steinegger
The Session.CreateQuery("").AddType will not compile (there is no AddType There)
Dani
Sorry, I think it is called AddClass. Since it is ported from Java, there are sometimes such terms.
Stefan Steinegger
Sorry, Doesn't work, not AddType or AddClass....
Dani

related questions