I am facing some issues when fetching the collection with applied criteria.
public class Ship
{
public virtual int Id { get; protected set; }
public virtual string Name { get; set; }
public virtual IList<Structure> Structures { get; set; }
public Ship()
{
Structures = new List<Structure>();
}
public virtual void AddStructure(Structure structure)
{
Structures.Add(structure);
}
}
public class Structure
{
public virtual int StructureId { get; protected set; }
public virtual string Name { get; set; }
public virtual Ship Ship { get; set; }
public virtual int Score { get; set; }
public virtual IList<StructureGeometry> StructuresGeomtries { get; set; }
public Structure()
{
StructuresGeomtries = new List<StructureGeometry>();
}
}
public class StructureGeometry
{
public virtual int StructureGeomId { get; protected set; }
public virtual string Name { get; set; }
public virtual Structure Structure { get; set; }
public StructureGeometry()
{
}
}
public class StructureMap : ClassMap<Structure>
{
public StructureMap()
{
Id(x => x.StructureId).GeneratedBy.Increment();
Map(x => x.Name);
Map(x => x.Score);
References(x => x.Ship);
HasMany(x => x.StructuresGeomtries)
.Cascade.All()
.Inverse();
}
}
DetachedCriteria query = DetachedCriteria.For(typeof(Structure), "structure");
Disjunction juction = Restrictions.Disjunction();
juction.Add(NHibernate.Criterion.Expression.Like("Score", 1));
juction.Add(NHibernate.Criterion.Expression.Like("Score", 3));
query.Add(juction);
Disjunction structureGeometryjuction = Restrictions.Disjunction();
structureGeometryjuction.Add(NHibernate.Criterion.Expression.Like("Name", "SGeom3"));
DetachedCriteria structuGeomCriteria = DetachedCriteria.For(typeof(StructureGeometry));
structuGeomCriteria.Add(structureGeometryjuction);
structuGeomCriteria.SetProjection(Projections.ProjectionList().Add(Projections.Property("Name"), "StructGeomName"));
query.CreateCriteria("structure.StructuresGeomtries", "structureGeometry")
.Add(Subqueries.Exists(structuGeomCriteria));
var structures = query.GetExecutableCriteria(session).List<Structure>();
structures[0].StructuresGeomtries
are fetched as per the association. I have not created any DTO, I am fetching the data from same entity(POCO).
I am not able to apply filter or criteria on collection property. Nhibernate by default selecting all the geometries as per the one to many association.
So I am able to fetch my root parent as per the criteria but not able to apply the criteria on collection properties.
Thanks,