1

TripSent 和 ReturnCode

我想在这些表中进行左连接,例如

“在 TripSent.ReturnCode = ReturnCode.Id 上左加入 TripSent”

这是我的 TripSent 映射类

public TripSentMap()
{
    ImportType<RemittanceDetailTransHeader>();
    Schema(Dao.DataAccess.Schema);
    Table("EI_TRIP_SENT");
    LazyLoad();
    Id(x => x.Id)
      .Column("ID")
      .CustomType("Int64")
      .Access.Property()
      .CustomSqlType("NUMBER")
      .Not.Nullable()
      .Precision(12)
      .GeneratedBy.Sequence("EI_TRIP_SENT_ID_SEQ");
    //EDIT : Removed redundant Mapping Info
    References(x => x.ReturnCode)
      .Class<ReturnCode>()
      .Access.Property()
      .Cascade.None()
      .LazyLoad()
      .Columns("EI_RETURN_CODE_ID"); 
}

这是我的 HQL

String hql = @"SELECT ts.PartialTripNumber, ts.Sequence, ts.TagIssuer, ts.TagNumber, ts.DateTime, ts.PlazaNumber, ts.LaneNumber, ts.RealCategory, ts.DetectedCategory,
    ts.TabulatedCategory, ts.Amount, ts.PassMode, ts.LicensePlate, ts.ImageReason, ts.ReturnCode, ts.TripSentStatus
    FROM TripSent ts
    WHERE ts.Remittance.Id = :remId";

var transactionDetailsHql = _session.CreateQuery(hql)
                .SetParameter("remId", remId)
                .List();

这会生成这样的查询

select tripsent0_.PARTIAL_TRIP_NUMBER    as col_0_0_,
       tripsent0_.SEQUENCE               as col_1_0_,
       tripsent0_.EI_TAG_ISSUER_ID       as col_2_0_,
       tripsent0_.TAG_NUMBER             as col_3_0_,
       tripsent0_.DATE_TIME              as col_4_0_,
       tripsent0_.PLAZA_NUMBER           as col_5_0_,
       tripsent0_.LANE_NUMBER            as col_6_0_,
       tripsent0_.REAL_CATEGORY          as col_7_0_,
       tripsent0_.DETECTED_CATEGORY      as col_8_0_,
       tripsent0_.TABULATED_CATEGORY     as col_9_0_,
       tripsent0_.AMOUNT                 as col_10_0_,
       tripsent0_.EI_PASS_MODE_ID        as col_11_0_,
       tripsent0_.LICENSE_PLATE          as col_12_0_,
       tripsent0_.EI_IMAGE_REASON_ID     as col_13_0_,
       tripsent0_.EI_RETURN_CODE_ID      as col_14_0_,
       tripsent0_.EI_TRIP_SENT_STATUS_ID as col_15_0_,
       tagissuer1_.ID                    as ID36_0_,
       passmode2_.ID                     as ID14_1_,
       imagereaso3_.ID                   as ID12_2_,
       returncode4_.ID                   as ID28_3_,
       tripsentst5_.ID                   as ID48_4_,
       tagissuer1_.DESCRIPTION           as DESCRIPT2_36_0_,
       tagissuer1_.EI_TAG_CHECK_TYPE_ID  as EI3_36_0_,
       tagissuer1_.EI_TAG_OSA_ID         as EI4_36_0_,
       passmode2_.DESCRIPTION            as DESCRIPT2_14_1_,
       imagereaso3_.DESCRIPTION          as DESCRIPT2_12_2_,
       returncode4_.DESCRIPTION          as DESCRIPT2_28_3_,
       tripsentst5_.DESCRIPTION          as DESCRIPT2_48_4_
from   CENTRO.EI_TRIP_SENT tripsent0_
       inner join CENTRO.EI_TAG_ISSUER tagissuer1_
         on tripsent0_.EI_TAG_ISSUER_ID = tagissuer1_.ID
       inner join CENTRO.EI_PASS_MODE passmode2_
         on tripsent0_.EI_PASS_MODE_ID = passmode2_.ID
       inner join CENTRO.EI_IMAGE_REASON imagereaso3_
         on tripsent0_.EI_IMAGE_REASON_ID = imagereaso3_.ID
       inner join CENTRO.EI_RETURN_CODE returncode4_
         on tripsent0_.EI_RETURN_CODE_ID = returncode4_.ID
       inner join CENTRO.EI_TRIP_SENT_STATUS tripsentst5_
         on tripsent0_.EI_TRIP_SENT_STATUS_ID = tripsentst5_.ID
where  tripsent0_.EI_REMITTANCE_ID = 12 /* :p0 */

但我想要一个 LEFT OUTER JOIN 而不是

inner join CENTRO.EI_RETURN_CODE returncode4_ on tripsent0_.EI_RETURN_CODE_ID = returncode4_.ID

我找不到如何做到这一点我也尝试过映射

References(x => x.ReturnCode)
      .Class<ReturnCode>()
      .Access.Property()
      .Cascade.None()
      .NotFound.Ignore().Nullabe()
      .Not.LazyLoad()
      .Columns("EI_RETURN_CODE_ID"); 

没有成功 D:

4

2 回答 2

0

您应该从映射中删除冗余信息以使其更易于阅读。

此外,像 NHibernate 这样的对象/关系映射器旨在从关系数据库返回对象。如果您尝试指定诸如 LEFT JOIN 之类的内容,那么您似乎不是在尝试拉取对象,而是在尝试一些自定义视图。

您仍然可以这样做,但是您需要一个数据传输对象 (DTO),它基本上是一个对象,表示查询中的每一行将返回的内容。您仍然可以利用 NHibernate 的数据库连接并用于CreateSqlQuery运行原始 SQL,然后根据需要将结果转换为对象。

于 2012-11-27T18:40:46.513 回答
0

在这种情况下,我需要编写一个纯 sql 查询。但它是一个孤立的。

于 2017-01-04T21:26:03.397 回答