NHibernate检索多个表记录

时间:2013-10-31 14:26:32

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

我是新的NHibernate.i面临nhibernate的问题,当我在主表上查询它给我主表记录,但它也给了我相关的链接对象表记录。所以因为这个更多的SQL查询点击数据库。 这会降低我的查询性能。我只想要主表记录,而不是从数据库中解除任何其他查询。

我只想要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 个答案:

答案 0 :(得分:1)

有一些解决方案,从易到难 - 1.删​​除<set>映射,或创建没有它们的其他实体。 2.设置所有<set lazy="true">...</set>仅在访问这些属性时才会进行提取。 3.创建DTO(数据传输对象)实体,精简版,并使用NHibernate.Projections库来有选择地获取属性。 4.设置<set fetch="join">...</set>以获取一个已加入查询中的所有数据,这可能会复制您的记录,因此请小心。

总的来说,分离查询的NH并不一定是坏事,大表上的JOIN会导致更大的性能损失。您的问题可能不是映射问题,可能是数据库或代码问题。