将my-sql查询转换为nhibernate查询

时间:2011-08-04 06:09:57

标签: nhibernate queryover

有人可以帮助我或让我开始将此查询转换为nhibernate查询转换查询吗?

SELECT
    Campaign.Id,
    Location.Name,
    Location.PrimaryPostcode,
    Inventory.Section,
    Campaign.Sov,
    IFNULL(Contracts.Spend, 0) AS Spend,
    IFNULL(Contracts.Impressions, 0) AS Impressions,
    IFNULL(Days, 0) Days,
    RenewalDate,
    LastContract.OptIn
FROM
    Contract AS Campaign
    JOIN Inventory ON Campaign.InventoryId = Inventory.Id
    JOIN Location ON Inventory.LocationId = Location.Id
    LEFT OUTER JOIN (
        SELECT
            CampaignId,
            SUM(RackRate) AS Spend,
            SUM(Impressions) AS Impressions,
            1 + DATEDIFF(Now(), MIN(StartDate)) AS Days,
            DATE_SUB(MAX(EndDate), INTERVAL 11 Day) AS RenewalDate,
            MAX(StartDate) AS CurrentOrNextStartDate
        FROM
            Contract
            LEFT OUTER JOIN Creative On Creative.ContractId = Contract.Id
            LEFT OUTER JOIN CreativeLog On Creative.Id = CreativeLog.CreativeId            
        GROUP BY
            CampaignId
    ) AS Contracts ON Campaign.Id = Contracts.CampaignId
    LEFT OUTER JOIN (
        SELECT
            CampaignId,
            Id,
            Optin,
            StartDate
        FROM
            Contract
    ) AS LastContract ON Campaign.Id = LastContract.CampaignId AND Contracts.CurrentOrNextStartDate = LastContract.StartDate
WHERE
    Campaign.AgencyId = '04ba6b28-a7a0-4448-b21f-9f2b00a4621b'
ORDER BY
    RenewalDate,
    Location.Name,
    Inventory.Section

我还没有过去:

_session.QueryOver<Contract>()
    .Left.JoinQueryOver<Contract>(campaign => campaign.Contracts).List()

因为当它应该是CampaignId == Id时,nhibernate在两个表别名Id == Id之间生成一个on子句。

FROM Contract this_ left outer join Contract contract1_ on this_.Id=contract1_.Id 

合约的“合约”属性是一种自我引用关系。它不是1:1。

2 个答案:

答案 0 :(得分:0)

您需要显示或解释您的映射是如何完成的。如果您要将Contract加入到自身,那么您需要告诉它要加入什么,否则它会在主键上执行。如果您使用最新的nhibernate,我相信您可以执行.With并告诉它加入不同的列。如果它还没有你可以使用.UnderlyingCriteria

答案 1 :(得分:0)

我已经得出结论,这无法在查询中完成。至少不是以最佳方式。无法连接投影然后从中进行选择几乎是一个显示阻止。