简体   繁体   中英

Nhibernate: Trouble mapping a one-to-many relationship

I'm very, very new to nhibernate, and I'm certain there are a lot of obvious things I'm overlooking here, but I've been searching for the answer for a few days now and simply cannot find a solution which fits my specific problem. I would really, really appreciate your help.

I'll describe my problem first, and then post the code:

In my object model, I have an "Event" with an IList of "Organizer". I want to use nhibernate so that when I do "session.Save(anEvent)", data is written to three tables in the database: Events, Organizers, and Organizers_Events (the join table). When I use the below code, I get the following error:

Invalid column name 'EventID'

My code: First, the .hbm files:

<?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>

Next, the objects:

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; }
        }
    }
}

And finally, the database schema:

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]

I've cut out the irrelevant data to simplify the issue. I don't think any of the other stuff has any affect on my problem here. For our purposes here, I just want the Event object to write it's EventID and Name to the Events table, each organizer object in the IList to write its ID and FullName to the Organizers table, and the EventID and the Organizer's ID to be written to the Organizers_Events table.

What am I doing wrong? If it's a lot, please tell me. Or if there's an answer already online that I've missed please direct me to it.

In one-to-many association you don't need join table. Join tables are only required in many-to-many assciations. Correct mapping

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

Correct db schema for one-to-many association

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" >

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