简体   繁体   中英

NHibernate nested class mapping issue

I have below classes. How can I write mapping document for MainBranch.Id column. I have no branch table in database, just want to use branchId for MAINBRANCHCODE. Any Idea?

public class Bundle
        {
            public virtual Decimal Id { get; set; }       
            public virtual BundleEntranceInformation Information { get; set; } 
        }
    public class BundleEntranceInformation
        {
            public virtual Branch MainBranch { get; set; }      
        }
    public class Branch
        {
            public virtual short Id { get; set; }       
        }

My mapping document:

<class name="PromissoryNotes.Server.Data.Bundle, PromissoryNotes.Server.Data" table="BUNDLE" lazy="true">
    <id name="Id" column="ID" type="Decimal">
      <generator class="increment" />
    </id>   
    <property name="Information.MainBranch.Id" column="MAINBRANCHCODE" type="short"></property>

  </class>

Use a component mapping

<class name="BundleEntranceInformation">
  <component name="MainBranch">
    <property name="Id" column="MAINBRANCHCODE"/>
  </component>
</class>
public class MainClass
{
  public virtual long MainKey {get; set;}
  public virtual SubClass SubInstance {get; set;}

  public class SubClass
  {
    public virtual long SubKey {get;set;}
  }
}

can be mapped as:

<class name="MainClass" table="Main">
  <id name="MainKey" column="MainId" type="Int64">
    <generator class="identity" />
  </id>
  <many-to-one name="SubInstance" class="MainClass+SubClass" Column="SubId"/> 
</class>

<class name="MainClass+SubClass" table="Sub">
  <id name="SubKey" column="SubId" type="Int64">
    <generator class="identity" />
  </id>
</class>

So the plus sign is key (I believe that java-hibernate uses the dollar sign $ for this)

Here is the answer :)

<component name="Information">
   <component name="MainBranch">
     <property name="Id" column="MAINBRANCHCODE"/>
   </component>
</component  >

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM