在使用应用标准获取集合时,我遇到了一些问题。
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
。我没有创建任何DTO,我从同一实体(POCO)获取数据。
我无法对集合属性应用过滤器或条件。默认情况下,Nhibernate根据一对多关联选择所有几何。
因此,我可以根据条件获取我的根父级,但无法对集合属性应用条件。
谢谢,
答案 0 :(得分:0)
您必须反转您的选择 - 您在查询中所做的是选择具有与您的条件匹配的几何的结构。但是,这不会过滤掉所选结构的几何形状。
您可以使用集合上的过滤器来实现此目的,但我认为在StructureGeometry上执行查询可能更容易,例如:
var geoms = session.CreateCriteria<StructureGeometry>()
.Add(Expression.Like("Name", "SGeom3"))
.CreateAlias("Structure", "structure")
.Add(Expression.Like("structure.Score", 1) ||
Expression.Like("structure.Score", 3))
.List();