2013-10-31 69 views
1

我是新的NHibernate.i面臨nhibernate問題時,我打在主表查詢它給了我主表記錄,但它也給我相關的鏈接的對象表記錄。所以因爲這個更多的sql查詢命中數據庫。 這會降低我的查詢性能。我只想要主表記錄,而不是從數據庫中激發任何其他查詢。NHibernate檢索多個表記錄

我想TAPVendor表只記錄

下面是我在哪裏發射查詢我的C#代碼數據庫

VAR lstTAPVendor = session.Query < TAPVendor>()

我有還附上了tapvendor的.hbm文件

<?xml version="1.0" encoding="utf-8"?> 
<hibernate-mapping namespace="M3.Entities" assembly="M3.Entities" xmlns="urn:nhibernate-mapping-2.2"> 
    <class name="TAPVendor" table="tAPVendor" schema="dbo"> 
    <id name="FVendorID" type="Guid"> 
     <generator class="assigned" /> 
    </id> 
    <version name="FTimestamp" generated="always" unsaved-value="null" type="BinaryBlob"> 
     <column name="FTimestamp" not-null="true" sql-type="timestamp"/> 
    </version> 
    <property name="FActive" type="Boolean" not-null="true" /> 

    <property name="FTermsType" type="Int32" precision="10" not-null="true" /> 
    <property name="FACHPayment" type="Boolean" not-null="true" /> 
    <property name="FCompanyName" type="String" length="50" not-null="true" /> 
    <property name="FAccrueUseTax" type="Boolean" not-null="true" /> 
    <property name="FSinglePaymentVendor" type="Boolean" not-null="true" /> 
    <property name="FACHEnabled" type="Boolean" not-null="true" /> 
    <property name="FCheckPerInvoice" type="Boolean" not-null="true" /> 
    <property name="FBankAccountType" type="Int32" precision="10" not-null="true" /> 
    <property name="FBankAccountName" type="String" length="50" /> 
    <property name="FBankAccountNumber" type="String" length="50" /> 
    <property name="FBankABANumber" type="String" length="50" /> 
    <property name="FLegalName" type="String" length="50" /> 
    <property name="FDateAdded" type="DateTime" /> 
    <property name="FAddedBy" type="String" length="50" /> 
    <property name="FDateModified" type="DateTime" /> 
    <property name="FModifiedBy" type="String" length="50" /> 
    <property name="FAccountNo" type="Double" precision="53" /> 
    <property name="FDescription" type="String" length="100" /> 
    <property name="FTerms" type="Int32" precision="10" /> 
    <property name="F1099Box" type="Int16" precision="5" /> 
    <property name="F1099Type" type="String" length="15" /> 
    <property name="FTaxID" type="String" length="10" /> 
    <property name="FSSN" type="String" length="11" /> 
    <property name="FVendorNo" type="String" length="10" /> 
    <property name="FAccountNo2" type="Double" precision="53" not-null="false" /> 
    <property name="FIsUseAccountNo2" type="Boolean" not-null="true" /> 
    <many-to-one name="TAddres" class="TAddres" column="fAddressID" /> 
    <many-to-one name="TSCCompany" class="TSCCompany" column="fCompanyID" /> 
    <many-to-one name="TContact" class="TContact" column="fContactID" /> 
    <many-to-one name="TSCEnterprise" class="TSCEnterprise" column="fEnterpriseID" /> 
    <many-to-one name="TSCProperty" class="TSCProperty" column="fPropertyID" /> 
    <set name="TAPInvoices" table="tAPInvoice" inverse="true"> 
     <key column="fVendorID" /> 
     <one-to-many class="TAPInvoice" /> 
    </set> 
    <set name="TBAChecks" table="tBACheck" inverse="true"> 
     <key column="fVendorID" /> 
     <one-to-many class="TBACheck" /> 
    </set> 
    <set name="TAPRecurringInvoices" table="tAPRecurringInvoice" inverse="true"> 
     <key column="fVendorID" /> 
     <one-to-many class="TAPRecurringInvoice" /> 
    </set> 
    <set name="TGLPostMasters" table="tGLPostMaster" inverse="true"> 
     <key column="fVendorID" /> 
     <one-to-many class="TGLPostMaster" /> 
    </set> 
    </class> 
</hibernate-mapping> 

回答

1

有一些解決方案離子,從容易到難 - 1.刪除<set>映射,或創建沒有它們的另一個實體。 2.設置全部<set lazy="true">...</set>只有在訪問這些屬性時纔會發生提取。 3.創建DTO(數據傳輸對象)實體,lite版本,並使用NHibernate.Projections庫來選擇性地提取屬性。 4.設置<set fetch="join">...</set>以獲取一個連接查詢中的所有數據,這可能會複製您的記錄,所以要小心。

而且通常情況下,NH將查詢分開並不一定是不好的,加入大表可能會導致更大的性能下降。您的問題可能不是映射問題,可能是數據庫或代碼問題。