Question

I am new in NHibernate.i am facing problem with nhibernate when i hit query on main table it gives me main table records but as well as it is also giving me related linked object tables records.So because of this more sql query hit on database. which decrease performance of my query.i want only main table records and not fired any other query from database.

I want TAPVendor table records only

Below is my C# code from where i am fired query from database

var lstTAPVendor = session.Query< TAPVendor >()

i have also attached the .hbm file of tapvendor

<?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>
Était-ce utile?

La solution

There are some solutions, from easy to hard - 1. Remove the <set> mappings, or create another entity without them. 2. Set all <set lazy="true">...</set> the fetch will occur only on accessing these properties. 3. Create a DTO (data transfer object) entity, a lite version, and use to NHibernate.Projections library to fetch properties selectively. 4. Set <set fetch="join">...</set> to fetch all data in one joined query, this might duplicate your records, so be careful.

And in general, NH separating the queries is not necessarily bad, JOIN on big tables can cause much bigger performance hit. Your issues might not be a mapping issue, it might be DB or code issue.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top