代码之家  ›  专栏  ›  技术社区  ›  Ras

Nhibernate一对一关系返回始终为null

  •  0
  • Ras  · 技术社区  · 8 年前

    我有两张桌子,

    CREATE TABLE [dbo].[WidgetProperty](
        [WidgetPropertyID] [int] IDENTITY(1,1) NOT NULL,
        [WidgetID] [int] NOT NULL,
        [PropertyID] [int] NOT NULL,
        [PropertyIndex] [int] NOT NULL,
    PRIMARY KEY CLUSTERED 
    (
        [WidgetPropertyID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    ALTER TABLE [dbo].[WidgetProperty] ADD  DEFAULT ((1)) FOR [PropertyIndex]
    

    HBM公司

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
      <class name="Nt.Engine.domain.WidgetProperty,Nt.Engine" table="WidgetProperty" lazy="true">
        <id name="WidgetPropertyID" column="WidgetPropertyID">
          <generator class="native" />
        </id>
        <property name="WidgetID" column="WidgetID" type="Int32" not-null="true" />
        <property name="PropertyID" column="PropertyID" type="Int32" not-null="true" />
        <property name="PropertyIndex" column="PropertyIndex" type="Int32" not-null="true" />
        <one-to-one name="PropertyDetail" cascade="all" foreign-key="none" class="Nt.Engine.domain.PropertyDetail,Nt.Engine"  />  
      </class>
    </hibernate-mapping>
    

    型号

    public class WidgetProperty
    {
        public virtual Int32 WidgetPropertyID { get; set; }
        public virtual Int32 WidgetID { get; set; }
        public virtual Int32 PropertyID { get; set; }
        public virtual Int32 PropertyIndex { get; set; }
        public virtual PropertyDetail PropertyDetail { get; set; }
    }
    

    第二个包含每个属性的数据/详细信息:

    CREATE TABLE [dbo].[PropertyDetail](
        [PropertyID] [int] IDENTITY(1,1) NOT NULL,
        [Name] [nvarchar](50) NOT NULL,
        [Length] [nvarchar](50) NOT NULL,
        [Type] [smallint] NOT NULL,
        [DefaultHtml] [nvarchar](600) NULL,
     CONSTRAINT [PK_PropertyDetail] PRIMARY KEY CLUSTERED 
    (
        [PropertyID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    ALTER TABLE [dbo].[PropertyDetail] ADD  CONSTRAINT [DF__tblProper__Prope__276EDEB3]  DEFAULT ((1)) FOR [Type]
    

    HBM公司

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
      <class name="Nt.Engine.domain.PropertyDetail,Nt.Engine" table="PropertyDetail" lazy="true">
        <id name="PropertyID" column="PropertyID">
          <generator class="native" />
        </id>
        <property name="Name" column="Name" type="string" length="50" not-null="true" />
        <property name="Length" column="Length" type="string" length="50" not-null="true" />
        <property name="Type" column="Type" type="byte" not-null="true" />
        <property name="DefaultHtml" column="DefaultHtml" type="string" length="600" not-null="false" />
      </class>
    </hibernate-mapping>
    

    型号

    public class PropertyDetail
    {
        public virtual Int32 PropertyID { get; set; }
        public virtual String Name { get; set; }
        public virtual String Length { get; set; }        
        public virtual byte Type { get; set; }
        public virtual String DefaultHtml { get; set; }
    }
    

    基本上在第一个表中,每个表有几行 WidgetID PropertyID PropertyDetail 表一对一,因此每个WidgetProperty。PropertyID中只有一行 属性详细信息 桌子 在manager中,如果我调用对象 widgetID (es:4) WidgetProperty.PropertyDetail 始终为空。有什么提示吗?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Steve Py    8 年前

    一对一引用用于连接共享同一PK的记录,或者二级表具有引用父行的FK的记录。(使用property ref选项)在本例中,您希望父表上的一列指向子行。这被配置为多对一关系。

    <many-to-one name="PropertyDetail" cascade="all" column="PropertyId" class="Nt.Engine.domain.PropertyDetail,Nt.Engine"  /> 
    

    如果需要一对一,则需要将PropertyDetail表上的PropertyId替换为WidgetPropertyId。本质上,如果PropertyId值等于WidgetPropertyId,那么您实际上会用当前代码返回一行,这肯定不是您所期望的。一、 e.如果您的WidgetProperty/w ID为1234,PropertyId为5, 如果 有一个ID为1234的PropertyDetail,它将返回到与小部件1234关联的位置,因为一对一的性质是寻找匹配的PK。

    编辑:对上述内容进行了更正。如果需要一对一,可以反转FK关联,在您的情况下,将WidgetPropertyId添加到PropertyDetail,并从WidgetProperty中删除PropertyId。从那里,您可以像以前一样,通过删除外键并将其替换为property ref来设置从小部件到PropertyDetail的一对一,告诉NH通过其FK列而不是PK链接PropertyDetail:

    <one-to-one name="PropertyDetail" cascade="all" property-ref="WidgetPropertyId" class="Nt.Engine.domain.PropertyDetail,Nt.Engine"  />