views:

90

answers:

1

I'm working with 2 tables, Employees and a table called EmployeeGroups as follows. I've left out most of the unrelevant columns below. I'm struggling to map the foreign key relationship. Its a one-to-one relationship where not every EmployeeId in the Employees table will exist in the EmployeeGroups table. I'm wondering how to setup the EmployeeId relationship in my mapping files below?

Employees

CREATE TABLE [dbo].[Employees](
[EmployeeId] [int] IDENTITY(1,1) NOT NULL,
[ForeName] [nvarchar](20) COLLATE Latin1_General_CI_AI NOT NULL,
[SurName] [nvarchar](20) COLLATE Latin1_General_CI_AI NOT NULL,

CONSTRAINT [PK_Employees_1] PRIMARY KEY CLUSTERED 
(
    [EmployeeId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

EmployeeGroups

CREATE TABLE [dbo].[EmployeeGroups](
[GroupId] [int] NULL,
[EmployeeId] [int] NOT NULL,
[Active] [int] NULL,
 CONSTRAINT [PK_EmployeeGroups] PRIMARY KEY CLUSTERED 
(
    [EmployeeId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

USE [DemoDEV]
GO
ALTER TABLE [dbo].[EmployeeGroups]  WITH CHECK ADD  CONSTRAINT     
[FK_EmployeeGroups_Employees] FOREIGN KEY([EmployeeId])
REFERENCES [dbo].[Employees] ([EmployeeId])

My Employees mapping file so far

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="AdaptiveMaintenance.Core.Domain.Employee, AdaptiveMaintenance.Core" table="Employees" lazy="false">
    <id name="ID" column="EmployeeId">
        <generator class="assigned" />
    </id>
    <property name="Forename" column="ForeName" not-null="true"/>
    <property name="Surname" column="SurName" not-null="true"/>

</class>
</hibernate-mapping>

My EmployeeGroups mapping file so far looks like this

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="AdaptiveMaintenance.Core.Domain.EmployeeGroup, AdaptiveMaintenance.Core" table="EmployeeGroups" lazy="false">
      <id name="ID" column="EmployeeId">
      <generator class="foreign" />
    </id>

    <property name="Active" column="Active" />
</class>

A: 

You should not only consider how to map the db model, you should always think about the class model: how should it look like?

This creates a reference from the EmployeeGroup to Employee.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="AdaptiveMaintenance.Core.Domain.EmployeeGroup, AdaptiveMaintenance.Core" table="EmployeeGroups" lazy="false">
    <id name="ID" column="GroupId">
      <generator class="assigned" />
    </id>

    <property name="Active" column="Active" />
    <many-to-one name="Employee" class="Employee" column="EmployeeId"/>
</class>

When you have a one-to-one relation an try to couple primary key, why is there a GroupId? I made it an independent primary key.

BTW, if you are creating a new database model it is easier to design the classes first, map them afterwards and create the schema using SchemaExport.

Stefan Steinegger