Hibernate version: 1.2 (beta 2)
Hi,
For projections + stronly typed results i was previously using SELECT NEW (hql) . Now i see that ICriteria also supports projections so i am trying to unify and use only criterias.
I have a simple POCO that maps the projected fields (i still want to have a strongly typed object to work with).
* using critera.List() --> returns a list of objects ( i do not know if there is a way to specify that the result should be of type "CustomView"..)
If I use the new syntax (generic) : i get an exception that System.Object can not be casted to my poco class (CustomView)
IList<CustomView> _cvs = _critera.List<CustomView>();
Make sense, the point is that i do not know if this is possible...
Below the code to use the new Projection stuff (great!)
Code:
public ICriteria GetBasicCriteria()
{
ICriteria _criteria = Session.CreateCriteria(typeof(Products)); if (_criteria == null) throw new Exception("Failed to create criteria for CustomView1");
_criteria.CreateCriteria("suppliers","suppliers",NHibernate.SqlCommand.JoinType.LeftOuterJoin);
_criteria.SetProjection( GetProjectionList() );
return _criteria;
}
protected virtual IProjection GetProjectionList()
{
ProjectionList _properties = Projections.ProjectionList();
_properties.Add(NHibernate.Expression.Property.ForName("productID"));
_properties.Add(NHibernate.Expression.Property.ForName("productName"));
_properties.Add(NHibernate.Expression.Property.ForName("supplierID"));
_properties.Add(NHibernate.Expression.Property.ForName("suppliers.contactName"));
_properties.Add(NHibernate.Expression.Property.ForName("suppliers.contactTitle"));
return _properties;
}