Nhibernate:映射一对多关系时出现问题

本文关键字:问题 关系 映射 一对多 Nhibernate | 更新日期: 2023-09-27 18:21:24

我对nhibernate非常非常陌生,我确信这里有很多明显的事情我忽略了,但我已经寻找答案几天了,根本找不到适合我特定问题的解决方案。我真的非常感谢你的帮助。

我将首先描述我的问题,然后发布代码:

在我的对象模型中,我有一个IList为"Organizer"的"Event"。我想使用nhibernate,这样当我执行"session.Save(anEvent)"时,数据会写入数据库中的三个表:Events、Organizers和Organizers_Events(联接表)。当我使用以下代码时,我会得到以下错误:

Invalid column name 'EventID'

我的代码:首先,.hbm文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="BLL"
                   namespace="BusinessLogic">
  <class name="Event" table="Events">
    <id name="EventID">
      <column name="ID"/>
      <generator class="native" />
    </id>
    <property name="Name">
      <column name="Name"/>
    </property>
    <bag name="Organizers" table="Organizers_Events" inverse="false" cascade="all-delete-orphan" lazy="true" access="nosetter.camelcase-underscore" >
      <key column="EventID"/>
      <one-to-many class="Organizer" />
    </bag>
  </class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="BLL"
                   namespace="BusinessLogic">
  <class name="Organizer" table="Organizers">
    <id name="Id">
      <column name="ID"/>
      <generator class="native" />
    </id>
    <property name="FullName">
      <column name="FullName"/>
    </property>
    <many-to-one name="TheEvent" column="EventID" />
  </class>
</hibernate-mapping>

接下来,对象:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Event
/// </summary>
/// 
namespace BusinessLogic
{
    public class Event
    {
        private int eventID;
        public virtual int EventID
        {
            get { return eventID; }
            set { eventID = value; }
        }
        private IList<Organizer> _organizers = null;
        public virtual IList<Organizer> Organizers
        {
            get { return _organizers; }
            set { _organizers = value; }
        }
        public Event()
        {
            //
            // TODO: Add constructor logic here
            //
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BusinessLogic
{
    public class Organizer:Invitee
    {
        private Event theEvent = new Event();
        public virtual Event TheEvent
        {
            get { return theEvent; }
            set { theEvent = value; }
        }
    }
}

最后,数据库模式:

CREATE TABLE [dbo].[Events](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](max) NOT NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[Organizers](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [FullName] [nvarchar](550) NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[Organizers_Events](
    [OrganizerID] [int] NOT NULL,
    [EventID] [int] NOT NULL
) ON [PRIMARY]

为了简化问题,我删掉了不相关的数据。我不认为其他任何事情对我的问题有任何影响。出于我们的目的,我只希望Event对象将其EventID和Name写入Events表,IList中的每个组织者对象将其ID和FullName写入Organizers表,EventID和organizer的ID写入Organizers_Events表。

我做错了什么?如果有很多,请告诉我。或者,如果网上已经有我错过的答案,请直接告诉我。

Nhibernate:映射一对多关系时出现问题

在一对多关联中,不需要联接表。只有在多对多关联中才需要联接表。正确映射

<bag name="Organizers" inverse="false" cascade="all-delete-orphan" lazy="true" access="nosetter.camelcase-underscore" >
    <key column="EventID"/>
    <one-to-many class="Organizer" />
</bag>

一对多关联的正确数据库模式

CREATE TABLE [dbo].[Events]
(
  [ID] [int] IDENTITY(1,1) NOT NULL,
  [Name] [nvarchar](max) NOT NULL
)
 ON [PRIMARY] 
CREATE TABLE [dbo].[Organizers]
( 
 [ID] [int] IDENTITY(1,1) NOT NULL, 
 [FullName] [nvarchar](550) NULL,
 [EventID] [int] NOT NULL // add FK constraint here
) 
ON [PRIMARY]   

我想你忘了在包声明中放inverse="false":

<bag name="Organizers" inverse="false" table="Organizers_Events" cascade="all-delete-orphan" lazy="true" access="nosetter.camelcase-underscore" >