如何使用NHibernate.SqlCommand.JoinType.RightOuterJoin?

如何使用NHibernate.SqlCommand.JoinType.RightOuterJoin?,nhibernate,nhibernate-criteria,Nhibernate,Nhibernate Criteria,我有项目和客户模型。 项目模型具有映射为的客户对象。在数据库中,这可能是客户存在但在项目表中没有条目的情况 我正在使用CustomerSearch功能,用户可以在其中输入项目或客户名称。如果用户仅输入customername,则我需要与customername匹配的所有记录,而不考虑客户是否有项目 我在服役时试过这个 ICriteria criteria = session.CreateCriteria(typeof(Project),"Project")

我有项目和客户模型。 项目模型具有映射为
的客户对象。在数据库中,这可能是客户存在但在项目表中没有条目的情况

我正在使用CustomerSearch功能,用户可以在其中输入项目或客户名称。如果用户仅输入customername,则我需要与customername匹配的所有记录,而不考虑客户是否有项目

我在服役时试过这个

ICriteria criteria = session.CreateCriteria(typeof(Project),"Project")
                    .CreateAlias("Project.customer","customer",NHibernate.SqlCommand.JoinType.RightOuterJoin)
                    .CreateAlias("Project.Coordinator", "Coordinator", NHibernate.SqlCommand.JoinType.InnerJoin);

                if (!string.IsNullOrEmpty(custName))
                {
                    criteria.Add(Expression.Like("customer.Name", custName, MatchMode.Start));
                }
                if (!string.IsNullOrEmpty(projectName))
                {
                    criteria.Add(Expression.Like("Project.Name", projectName, MatchMode.Start));
                }

                projectList = criteria.List<Project>().ToList();
ICriteria-criteria-criteria=session.CreateCriteria(typeof(Project),“Project”)
.CreateAlias(“Project.customer”、“customer”、NHibernate.SqlCommand.JoinType.RightOuterJoin)
.CreateAlias(“Project.Coordinator”、“Coordinator”、NHibernate.SqlCommand.JoinType.InnerJoin);
如果(!string.IsNullOrEmpty(custName))
{
添加(Expression.Like(“customer.Name”、custName、MatchMode.Start));
}
如果(!string.IsNullOrEmpty(projectName))
{
添加(Expression.Like(“Project.Name”、projectName、MatchMode.Start));
}
projectList=criteria.List().ToList();
我使用了RightOuterJoin to Customer,因为项目表可能并没有Customer的记录,但Customer表必须有这些记录

如果我看到
projectList
它有一个列表,但项目表中没有记录的某些项为空,那么该代码也不会给出任何错误


如何获取此类记录?

使用right join,项目的所有字段都为null,因此NH无法建立有效的项目对象(缺少id),因此返回null

您必须将值投影到某个对象中,以便NH知道如何处理空项目,或者单独查询没有项目的客户(使用Futures不需要单独的往返)

类搜索结果
{
公共int ProjectId{get;set;}
公共字符串ProjectName{get;set;}
public int CustomerId{get;set;}
公共字符串CustomerName{get;set;}
}
ICriteria标准=session.CreateCriteria(项目类型))
.CreateAlias(“客户”、“客户”、JoinType.RightOuterJoin)
.CreateAlias(“协调器”、“协调器”、NHibernate.SqlCommand.JoinType.InnerJoin);
如果(!string.IsNullOrEmpty(custName))
{
添加(Expression.Like(“customer.Name”、custName、MatchMode.Start));
}
如果(!string.IsNullOrEmpty(projectName))
{
添加(Expression.Like(“Name”、projectName、MatchMode.Start));
}
criteria.SetProjection(Projection.List()
.Add(Projections.Property(“Id”),“ProjectId”)
.Add(Projections.Property(“名称”),“ProjectName”)
.Add(Projections.Property(“customer.Id”),“CustomerId”)
.Add(Projections.Property(“customer.Name”)、“CustomerName”);
项目列表=标准
.SetTransforer(Transformers.AliasToBean())
.List();

感谢您的回复。你能告诉我如何将值投影到某个对象中,这样NH就知道如何处理空项目了。这对我有很大帮助。添加了代码。现在你总是能得到一个有效的对象,只是可能有ProjectId和ProjectName emptyIt起作用。是否有必要使用单独的属性,如ProjectId、ProjectName、CustomerName、CustomerId。我们不能将项目和客户作为对象。否,因为aliastobean只能投影到平面对象中,没有层次结构。或者您切换到Linq或hqlBy Linq意味着使用QueryOver API?
class SearchResult
{
    public int ProjectId { get; set; }
    public string ProjectName { get; set; }
    public int CustomerId { get; set; }
    public string CustomerName { get; set; }
}


ICriteria criteria = session.CreateCriteria(typeof(Project))
    .CreateAlias("Customer","customer", JoinType.RightOuterJoin)
    .CreateAlias("Coordinator", "Coordinator", NHibernate.SqlCommand.JoinType.InnerJoin);

if (!string.IsNullOrEmpty(custName))
{
    criteria.Add(Expression.Like("customer.Name", custName, MatchMode.Start));
}
if (!string.IsNullOrEmpty(projectName))
{
    criteria.Add(Expression.Like("Name", projectName, MatchMode.Start));
}

criteria.SetProjection(Projection.List()
    .Add(Projections.Property("Id"), "ProjectId")
    .Add(Projections.Property("Name"), "ProjectName")
    .Add(Projections.Property("customer.Id"), "CustomerId")
    .Add(Projections.Property("customer.Name"), "CustomerName"));

projectList = criteria
    .SetTransforer(Transformers.AliasToBean<SearchResult>())
    .List<Project>();