0

在使用应用标准获取集合时,我遇到了一些问题。

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 默认情况下根据一对多关联选择所有几何图形。

因此,我能够根据标准获取我的根父级,但无法将标准应用于集合属性。

谢谢,

4

1 回答 1

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();
于 2013-06-10T16:19:27.207 回答