【问题标题】:NHibernate - Join 2 tables and searchNHibernate - 加入 2 个表并搜索
【发布时间】:2013-02-04 20:19:44
【问题描述】:

我正在尝试为 NHibernate 创建 SQL 查询:

SQL 看起来像这样:

Select p.*
From PPoint p
Inner join PPFile f on p.ImportID = f.ImportID
where p.Name like '%a%'
Or p.Code like '%a%'
Or f.FileName like '%a%'

找不到一个像样的例子,即使听起来很容易做到。

目前我有:

        var pointList = session
            .CreateCriteria(typeof(PPoint))
           .Add(Restrictions.Or(Restrictions.Or(Restrictions.Or(Restrictions.Or
                Restrictions.Like("Name", "%" + search + "%"),
                Restrictions.Like("Code", "%" + search + "%")),
                Restrictions.Like("Test", "%" + search + "%")),
                Restrictions.Like("Test2", "%" + search + "%")),
                Restrictions.Like("FileName", "%" + search + "%")))
            .List<PPoint>();

NHibernate 2.2 版

【问题讨论】:

    标签: c# sql nhibernate


    【解决方案1】:

    找到解决方案:

    var pointList = session
                    .CreateCriteria(typeof(PPoint), "p")
                    .CreateAlias("ImportFile", "f", NHibernate.SqlCommand.JoinType.InnerJoin)
                    .Add(Restrictions.Disjunction()
                        .Add(Restrictions.Like("p.Name", search, MatchMode.Anywhere))
                        .Add(Restrictions.Like("p.Code", search, MatchMode.Anywhere))
                        .Add(Restrictions.Like("p.Test1", search, MatchMode.Anywhere))
                        .Add(Restrictions.Like("p.Test2", search, MatchMode.Anywhere))
                        .Add(Restrictions.Like("f.FileName", search, MatchMode.Anywhere)))
                    .List<PPoint>();
    

    主要是在映射文件中描述关系:

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                       assembly="AAA.Data"
                       namespace="AAA.Data.Domain">
    
      <class name="PPoint" lazy="true">
        <id name="PPointID" />
        <!--<property name="ImportID" />-->
        <many-to-one name="ImportFile" column="ImportID" not-null="true" cascade="all-delete-orphan" />
    
        <property name="Code" />
        <property name="Test1" />
    
        ...
      </class>
    

    【讨论】:

    • 很酷,我们现在已经有了使用 QueryOver 而不是使用 QueryOver 解决相同问题的示例。它让我们深入了解 QueryOver api 是如何演变的。
    【解决方案2】:

    您使用的是 nHibernate 3.0 或更高版本吗?这种查询非常适合QueryOver API

    string searchString = string.Format("%{0}%", search);
    PPoint ppointAlias = null;
    PPFile ppFileAlias = null;
    var pointList = session.QueryOver<PPoint>(() => ppointAlias)
                           .JoinQueryOver<PPFile>(ppPoint => ppPoint.ImportFile, () => ppFileAlias)
                           .Where(
                                     Restrictions.On(() => ppointAlias.Name).IsLike(searchString))
                                                  ||
                                     Restrictions.On(() => ppointAlias.Code).IsLike(searchString))
                                                  ||
                                     Restrictions.On(() => ppFileAlias.FileName).IsLike(searchString))
                                  )
                            .List();
    

    【讨论】:

    • 我使用 NHibernate 2.2 版本。
    猜你喜欢
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-09
    • 1970-01-01
    相关资源
    最近更新 更多