如何使用Nhibernate将过程转换为创建条件?

时间:2013-05-21 18:18:32

标签: c#-4.0 nhibernate sql-server-2008-r2

我有一个存储过程,我必须将其转换为Nhibernate创建条件。我已完成与.hbm文件中的过程相关的所有映射。但是当我调试代码时,没有检索到任何数据。但是当我在SQL Server中为该过程提供相同的值时,它会检索数据。

我有以下存储过程

CREATE PROCEDURE ak_ReportData_GLActivityD_BeginningBalance  

  @CompanyID          uniqueidentifier,  
  @PropertyID         uniqueidentifier = NULL,  
  @StartAccount       float,  
  @EndAccount         float,  
  @StartDate          datetime  

AS  

SET NOCOUNT ON  

DECLARE @dtZeroDate         datetime  
DECLARE @dtFiscalYearStart  datetime  


-- Set constants  
SELECT @dtZeroDate = CONVERT(DATETIME, '1-Jan-1900')  

-- If companyID is not supplied get it from PropertyID  
IF @CompanyID IS NULL   
  SELECT @CompanyID = fCompanyID   
  FROM tSCProperty   
  WHERE fPropertyID = @PropertyID  

--Get start date of the fiscal year containing the passed @StartDate  
SELECT @dtFiscalYearStart = fBeginDate   
FROM tSCFiscalPeriod   
WHERE fFiscalYear = (SELECT fFiscalYear   
                     FROM tSCFiscalPeriod   
                     WHERE (@StartDate BETWEEN fBeginDate AND fEndDate)   
                       AND fCompanyID = @CompanyID)   
  AND fPeriod = 1   
  AND fCompanyID = @CompanyID  

-- Calculate beginning balances  
SELECT la.fAccount,   
       SUM(ps.fDebitAmount - ps.fCreditAmount) AS BeginBalance  
FROM        tSCLedgerAccount la   
 INNER JOIN tGLPostSummary   ps ON (la.fAccount = ps.fAccount AND la.fCompanyID = ps.fCompanyID)  
WHERE la.fCompanyID = @CompanyID  
  AND (la.fPropertyID = @PropertyID OR @PropertyID IS NULL)  
  AND la.fAccount BETWEEN @StartAccount AND @EndAccount  
  AND ps.fPostDate >= (CASE la.fType   
                         WHEN 'Liability' THEN @dtZeroDate  
                         WHEN 'Equity'    THEN @dtZeroDate   
                         WHEN 'Asset'     THEN @dtZeroDate   
                                          ELSE @dtFiscalYearStart  
                       END)   
  AND ps.fPostDate < @StartDate  
GROUP  BY la.fAccount 

以下是.hbm文件

第一个是

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="BlackOpsP2.Core" namespace="BlackOpsP2.Core.Domain.ARModule" xmlns="urn:nhibernate-mapping-2.2">
  <class name="tGLPostSummary" table="tGLPostSummary" lazy="true" >
    <id name="fCompanyID">
      <generator class="guid" />
    </id>
    <property name="fAccount">
    </property>
    <property name="fPostDate">
    </property>
    <property name="fCreditAmount">
    </property>
    <property name="fDebitAmount">
    </property>
    <property name="fDateModified">
    </property>

    <many-to-one name="LedgerAccount" class="tSCLedgerAccount" insert="false" update="false">
      <column name="fCompanyID" />
      <column name="fAccount" />
    </many-to-one>
</class>
</hibernate-mapping>

第二个是

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping assembly="BlackOpsP2.Core" namespace="BlackOpsP2.Core.Domain.ARModule" xmlns="urn:nhibernate-mapping-2.2">
  <class name="tSCLedgerAccount" table="tSCLedgerAccount" lazy="true" >
    <composite-id>
      <key-property name="fCompanyID"></key-property>
      <key-property name="fAccount"></key-property>
    </composite-id>
    <version name="fTimestamp" generated="always" unsaved-value="null" type="BinaryBlob">
      <column name="fTimestamp" not-null="false" sql-type="timestamp"/>
    </version>
    <property name="fInactive">  </property>
    <property name="fPropertyID">  </property>
    <property name="fProjectID"></property>
    <property name="fCapital">   </property>
    <property name="fName">    </property>
    <property name="fType"> </property>
    <property name="fDescription"> </property>
    <property name="fDepLife">   </property>

    <bag name="PostSummary" inverse="true">
      <key>
        <column name="fCompanyID"/>
        <column name="fAccount"/>
      </key>
      <one-to-many class="tGLPostSummary"/>
    </bag>

  </class>

</hibernate-mapping>

在C#中我写了这个创建条件查询。

 var BeginingBalance= Session.CreateCriteria<tSCLedgerAccount>("Ledger")
                                        .CreateCriteria("Ledger.PostSummary", "Summary", NHibernate.SqlCommand.JoinType.InnerJoin)
                                        .Add(Expression.And(Expression.Eq("Ledger.fCompanyID", GlobalMember.PrimaryFilter.CompanyId), Expression.Eq("Ledger.fPropertyID", GlobalMember.PrimaryFilter.PropertyId)))
                                        .Add(Expression.Eq("Summary.fDebitAmount", StartAccount))
                                        .Add(Expression.Eq("Summary.fCreditAmount", EndAccount))
                                        .Add(Expression.Eq("Summary.fPostDate", Convert.ToDateTime("2012-01-01")))
                                        .SetProjection(Projections.ProjectionList()
                                        .Add(Projections.GroupProperty("Summary.fAccount"), "fAccount")
                                        .Add(Projections.Property("Summary.fAccount"), "fAccount"))
                                        //.Add(Projections.Property("Summary.fDebitAmount"), "fDebitAmount")
                                        //.Add(Projections.Property("Summary.fCreditAmount"), "fCreditAmount"))
                                        .SetResultTransformer(Transformers.AliasToBean(typeof(tSCLedgerAccount)))
                                        .List<tSCLedgerAccount>();


        return BeginingBalance;

我使用C#作为语言和NHibernate。

0 个答案:

没有答案