NHibernate nested class mapping issue

2019-08-11 08:30发布

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>

3条回答
一纸荒年 Trace。
2楼-- · 2019-08-11 09:04
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)

查看更多
来,给爷笑一个
3楼-- · 2019-08-11 09:12

Use a component mapping

<class name="BundleEntranceInformation">
  <component name="MainBranch">
    <property name="Id" column="MAINBRANCHCODE"/>
  </component>
</class>
查看更多
SAY GOODBYE
4楼-- · 2019-08-11 09:15

Here is the answer :)

<component name="Information">
   <component name="MainBranch">
     <property name="Id" column="MAINBRANCHCODE"/>
   </component>
</component  >
查看更多
登录 后发表回答