2010-05-26 83 views
2

我有簡單的情況下(如圖像link text上)和簡單的SQL查詢NHibernate的加入和投影性能

SELECT M.Name, 
      A.Name, 
      B.Name 
FROM Master M  LEFT JOIN DetailA A 
ON M.DescA = A.Id LEFT JOIN DetailB B 
ON M.DescB = B.Id 

如何才達到NHibernate的使用CriteriaAPI同樣的效果?

我有這樣的事情:

public class Employee : ClassBase, IContactData 
{ 
    public virtual string FirstName { get; set; } 
    public virtual string MiddleName { get; set; } 
    public virtual string LastName { get; set; } 
    public virtual string NickName { get; set; } 
    public virtual string Login { get; set; } 
    public virtual string Password { get; set; } 
    public virtual string ContactPerson { get; set; } 
    public virtual string PESEL { get; set; } 
    public virtual string IdentificationNo { get; set; } 
    public virtual string NIP { get; set; } 
    public virtual string Description { get; set; } 
    public virtual string Profession { get; set; } 
    public virtual byte[] Photo { get; set; } 
    public virtual DateTime? BirthDate { get; set; } 
    public virtual Boolean SpecialUser { get; set; } 
    public virtual Boolean Sex { get; set; } 
    public virtual Item Gender { get; set; } 
    public virtual Item Position { get; set; } 
    public virtual Item Status { get; set; } 
    public virtual Item Nation { get; set; } 
    public virtual Item Education { get; set; } 
    public virtual Item JobType { get; set; } 
    public virtual ISet<Address> Addresses { get; set; } 
} 

public class Item : ClassBase 
{ 

    public virtual int ItemCode { get; set; } 
    public virtual int DictCode{ get; set;} 
    public virtual string Description{ get; set;} 

    public override string ToString() 
    { 
     return Description; 
    } 
} 

public class Address : ClassBase 
{ 
    public virtual string Description { get; set; } 
    public virtual string District { get; set; } 
    public virtual string Community { get; set; } 
    public virtual string City { get; set; } 
    public virtual string Street { get; set; } 
    public virtual string PostalCode { get; set; } 
    public virtual string HouseNo { get; set; } 
    public virtual string FlatNo { get; set; } 
    public virtual Boolean Official { get; set; } 
    public virtual Item Country { get; set; } 
    public virtual Item Region { get; set; } 
} 

映射:

<property name="FirstName"  column="FIRST_NAME"   type="string" length="50" not-null="true"/> 
<property name="MiddleName"  column="MIDDLE_NAME"  type="string" length="50" not-null="false"/> 
<property name="LastName"   column="LAST_NAME"   type="string" length="50" not-null="true"/> 
<property name="NickName"   column="NICKNAME"   type="string" length="50" not-null="false"/> 
<property name="Login"   column="LOGIN"    type="string" length="30" not-null="false"/> 
<property name="Password"   column="PASSWORD"   type="string" length="100" not-null="false"/> 
<property name="ContactPerson" column="CONTACT_PERSON"  type="string" length="250" not-null="false"/> 
<property name="PESEL"   column="PESEL"    type="string" length="11" not-null="false"/> 
<property name="IdentificationNo" column="IDENTIFICATION_NO" type="string" length="12" not-null="false"/> 
<property name="NIP"    column="NIP"    type="string" length="11" not-null="false"/> 
<property name="Description"  column="DESCRIPTION"  type="string" length="1000" not-null="false"/> 
<property name="Profession"  column="PROFESSION"   type="string" length="150" not-null="false"/> 
<property name="BirthDate"  column="BIRTH_DATE"   type="DateTime"    not-null="false"/> 
<property name="Photo"   column="PHOTO"    type="BinaryBlob"    not-null="false"/> 
<property name="Sex"    column="SEX"    type="Boolean"     not-null="false"/> 
<property name="SpecialUser"  column="SPECIAL_USER"  type="Boolean"     not-null="false"/> 

<many-to-one name="Gender"  column="DIC_GENDER"   not-null="false" class="DomainModel.ERP.Item,DomainModel" /> 
<many-to-one name="Position"  column="DIC_POSITION"  not-null="false" class="DomainModel.ERP.Item,DomainModel" /> 
<many-to-one name="Status"  column="DIC_STATUS"   not-null="false" class="DomainModel.ERP.Item,DomainModel" /> 
<many-to-one name="Nation"  column="DIC_NATION"   not-null="false" class="DomainModel.ERP.Item,DomainModel" /> 
<many-to-one name="Education"  column="DIC_EDUCATION"  not-null="false" class="DomainModel.ERP.Item,DomainModel" /> 
<many-to-one name="JobType"  column="DIC_JOB_TYPE"  not-null="false" class="DomainModel.ERP.Item,DomainModel" /> 

<set name="Addresses" table="TBL_ADDRESS" generic="true"> 
    <key column="FK_EMPLOYEE" /> 
    <one-to-many class="DomainModel.ERP.HRM.Address,DomainModel"/> 
</set> 

<property name="ItemCode"   column="ITEM_CODE"   type="integer" not-null="true"/> 
<property name="DictCode"   column="TABLE_CODE"   type="integer" not-null="true"/> 
<property name="Description"  column="NAME"    type="string" length="200" not-null="true"/> 

<version name="Version"  column="VERSION"  type="integer" unsaved-value="0"/> 
<property name="Description" column="DESCRIPTION" type="string" length="1000" not-null="false"/> 
<property name="District"  column="DISTRICT"  type="string" length="15" not-null="false"/> 
<property name="Community" column="COMMUNITY"  type="string" length="150" not-null="false"/> 
<property name="City"   column="CITY"   type="string" length="150" not-null="true"/> 
<property name="Street"  column="STREET"  type="string" length="150" not-null="true"/> 
<property name="PostalCode" column="POSTAL_CODE" type="string" length="10" not-null="false"/> 
<property name="HouseNo"  column="HOUSENO"  type="string" length="20" not-null="true"/> 
<property name="FlatNo"  column="FLATNO"  type="string" length="20" not-null="false"/> 
<property name="Official"  column="IS_OFFICIAL" type="Boolean"     not-null="true"/> 

<many-to-one name="Country"   column="DIC_COUNTRY"  not-null="false" class="DomainModel.ERP.Item,DomainModel" /> 
<many-to-one name="Region"   column="DIC_REGION"  not-null="false" class="DomainModel.ERP.Item,DomainModel" /> 

我需要:

  1. Retrive數據到我的網格控制
    - 姓,
    - 姓氏,
    - NIP,
    - 位置。說明,
    - Education.Description,
    - JobType.Description,
    - Country.Description - 從地址,其中Address.Official是真實的,
    - 街 - 從地址,其中Address.Official是真的,
    - 的HouseNo - 從地址,其中Address.Official是真實的,
    - FlatNo - 從地址,其中Address.O真實的。
  • Retrive數據到另一個網格控制
    • - 姓,
      - 名字,
      - NIP,
      - Position.Description ,
      - Education.Description,
      - JobType.Description。

    和最重要的想法。我只想使用一個SELECT語句來完成此操作。 現在爲第1點我使用NamedSQLQuery和一切正在工作的爐排,但對於最簡單的情況我想使用CriteriaAPI。

    也用於點1和2 I具有DTO類和我變換查詢的結果到這個類。

    我能做到這一點通過GetAllEmployees(),但許多屬性是延遲加載,做我有應用程序和數據庫之間的巨大TRAFIC。我可以將這些屬性更改爲Eager Loading,但在我看來,有太多的數據需要修改,我不需要。要顯示與來自多個實體的數據網格,只需創建一個SQL視圖和地圖與NH -

    +1

    您將需要包括你的映射一些細節給予任何形式的回答 – 2010-05-26 23:15:24

    +0

    的你真的* *想要得到唯一的名字?通常當使用ORM時,你會得到整個對象。 – 2010-05-27 04:51:57

    回答

    5

    我對這種情況的意見。

    你會從長遠來看,更快樂。

    +0

    可以輕鬆處理分頁場景嗎? – kite 2012-08-14 20:07:01

    +0

    當然。這與常規表格上的分頁相同。 – sirrocco 2012-08-16 07:01:25

    7

    我發現我的解決方案:)

    var master = DetachedCriteria.For<Master>() 
         .CreateAlias("DetailA", "detA", JoinType.LeftOuterJoin) 
         .CreateAlias("DetailB", "detB", JoinType.LeftOuterJoin) 
         .SetProjection 
         (
          Projections.ProjectionList() 
          .Add(Projections.Property("Id"), "Id") 
          .Add(Projections.Property("Name"), "MasterName") 
          .Add(Projections.Property("detA.Name"), "DetailAName") 
          .Add(Projections.Property("detB.Name"), "DetailBName") 
         ) 
         .SetResultTransformer(Transformers.AliasToBean(typeof (MasterDTO))); 
    
    class MasterDTO 
    { 
        public virtual int Id {get;set;} 
        public virtual string MasterName {get;set;} 
        public virtual string DetailAName {get;set;} 
        public virtual string DetailBName {get;set;} 
    
        public MasterDTO() 
        {} 
    
        public MasterDTO(int id, string mastername, string detailaname, string detailbname) 
        { 
         Id = id; 
         MasterName = mastername; 
         DetailAName = detailaname; 
         DetailBName = detailbname; 
        } 
    }