I am trying to execute a query through NHibernate, where the criteria for the result depends on the table that is referenced. How should I do it? Consider a simple example:
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
public Bar ReferencedBar { get; set; }
}
public class Bar
{
public int Id { get; set; }
public string Name { get; set; }
}
Foo is then displayed in Bar:
public class FooMapping : ClassMap<Foo>
{
public FooMapping()
{
Id(c => c.Id).GeneratedBy.HiLo("1");
Map(c => c.Name).Not.Nullable().Length(100);
References(c => c.Bar);
}
}
Now I want to get all the Foo from the database that reference a particular bar. This function uses criteria, but please give examples using something else if you think it’s better:
public IList<Foo> GetAllFoosReferencingBar(Bar bar)
{
using (var tx = Session.BeginTransaction())
{
var result = Session.CreateCriteria(typeof(Foo))
.Add(Restrictions.)
.List<Foo>();
tx.Commit();
return result;
}
}
source
share