I have my code working, but I'm getting 2 extra columns in the table/ddl, to represent a Many to Many relationship, ~~but~~ with attributes (scalars) on the relationship.
I am using 1.2.0.712 (FluentNHibernate.dll) 3.1.0.4000 (NHibernate.dll)
public partial class Employee
{
public Employee()
{
CommonConstructor();
}
private void CommonConstructor()
{
this.MyEmployeeToJobTitleMatchLinks = new List<EmployeeToJobTitleMatchLink>();
}
public virtual Guid? EmployeeUUID { get; set; }
public virtual byte[] TheVersionProperty { get; set; }
public virtual string SSN { get; set; }
public virtual string LastName { get; set; }
public virtual string FirstName { get; set; }
public virtual DateTime CreateDate { get; set; }
public virtual DateTime HireDate { get; set; }
public virtual ICollection<EmployeeToJobTitleMatchLink> MyEmployeeToJobTitleMatchLinks { get; set; }
public virtual void AddJobTitleLink(EmployeeToJobTitleMatchLink link)
{
link.TheEmployee = this;
if (!this.MyEmployeeToJobTitleMatchLinks.Contains(link))
{
this.MyEmployeeToJobTitleMatchLinks.Add(link);
}
if (!link.TheJobTitle.MyJobTitleToEmployeeMatchLinks.Contains(link))
{
link.TheJobTitle.MyJobTitleToEmployeeMatchLinks.Add(link);
}
}
public virtual void RemoveJobTitleLink(EmployeeToJobTitleMatchLink link)
{
link.TheEmployee = this;
if (this.MyEmployeeToJobTitleMatchLinks.Contains(link))
{
this.MyEmployeeToJobTitleMatchLinks.Remove(link);
}
if (link.TheJobTitle.MyJobTitleToEmployeeMatchLinks.Contains(link))
{
link.TheJobTitle.MyJobTitleToEmployeeMatchLinks.Remove(link);
}
}
}
public partial class JobTitle
{
public JobTitle()
{
CommonConstructor();
}
private void CommonConstructor()
{
this.MyJobTitleToEmployeeMatchLinks = new List<EmployeeToJobTitleMatchLink>();
}
public virtual Guid? JobTitleUUID { get; set; }
public virtual byte[] TheVersionProperty { get; set; }
public virtual string JobTitleName { get; set; }
public virtual DateTime CreateDate { get; set; }
public virtual ICollection<EmployeeToJobTitleMatchLink> MyJobTitleToEmployeeMatchLinks { get; set; }
public virtual void AddEmployeeLink(EmployeeToJobTitleMatchLink link)
{
link.TheJobTitle = this;
if (!this.MyJobTitleToEmployeeMatchLinks.Contains(link))
{
this.MyJobTitleToEmployeeMatchLinks.Add(link);
}
if (!link.TheEmployee.MyEmployeeToJobTitleMatchLinks.Contains(link))
{
link.TheEmployee.MyEmployeeToJobTitleMatchLinks.Add(link);
}
}
public virtual void RemoveEmployeeLink(EmployeeToJobTitleMatchLink link)
{
link.TheJobTitle = this;
if (this.MyJobTitleToEmployeeMatchLinks.Contains(link))
{
this.MyJobTitleToEmployeeMatchLinks.Remove(link);
}
if (link.TheEmployee.MyEmployeeToJobTitleMatchLinks.Contains(link))
{
link.TheEmployee.MyEmployeeToJobTitleMatchLinks.Remove(link);
}
}
}
public partial class EmployeeToJobTitleMatchLink
{
public EmployeeToJobTitleMatchLink()
{
//this.Id = Guid.NewGuid(); /* this works in conjuction with <generator class="assigned"></generator> */
}
public virtual Guid? LinkSurrogateUUID { get; set; }
/* These are "scalar properties of the ~~relationship~~ */
public virtual int PriorityRank { get; set; }
public virtual DateTime JobStartedOnDate { get; set; }
public virtual Employee TheEmployee { get; set; }
public virtual JobTitle TheJobTitle { get; set; }
}
public class EmployeeMap : ClassMap<Employee>
{
public EmployeeMap()
{
Id(x => x.EmployeeUUID).GeneratedBy.GuidComb();
OptimisticLock.Version();
Version(x => x.TheVersionProperty)
.Column("MyVersionColumn")
.Not.Nullable()
.CustomSqlType("timestamp")
.Generated.Always();
Map(x => x.SSN);
Map(x => x.LastName);
Map(x => x.FirstName);
Map(x => x.CreateDate);
Map(x => x.HireDate);
HasMany(x => x.MyEmployeeToJobTitleMatchLinks)
.Inverse()
.Cascade.All();
}
}
public class JobTitleMap : ClassMap<JobTitle>
{
public JobTitleMap()
{
Id(x => x.JobTitleUUID).GeneratedBy.GuidComb();
OptimisticLock.Version();
Version(x => x.TheVersionProperty)
.Column("MyVersionColumn")
.Not.Nullable()
.CustomSqlType("timestamp")
.Generated.Always();
Map(x => x.JobTitleName);
Map(x => x.CreateDate);
HasMany(x => x.MyJobTitleToEmployeeMatchLinks)
.Inverse()
.Cascade.All();
}
}
public class EmployeeToJobTitleMatchLinkMap : ClassMap<EmployeeToJobTitleMatchLink>
{
public EmployeeToJobTitleMatchLinkMap()
{
Id(x => x.LinkSurrogateUUID).GeneratedBy.GuidComb();
Map(x => x.PriorityRank);
Map(x => x.JobStartedOnDate);
References(x => x.TheEmployee).Column("TheEmployeeUUID").Not.Nullable();/*Bad naming convention with "The", but left here so it can be seen easily in the DDL*/
References(x => x.TheJobTitle).Column("TheJobTitleUUID").Not.Nullable();/*Bad naming convention with "The", but left here so it can be seen easily in the DDL*/
}
}
This works fine, but I'm getting 2 extra (nullable) columns in the ddl. They are marked with asteriks(*) below.
Select * From [dbo].[EmployeeToJobTitleMatchLink]
LinkSurrogateUUID
PriorityRank
JobStartedOnDate
TheEmployeeUUID
TheJobTitleUUID
*Employee_id
*JobTitle_id
I understand this is "by convention". (The names with the "_id" on them). But I don't need these columns. And I need to be able to have customized names. (TheEmployeeUUID and TheJobTitleUUID in this mock example.)
My end-game is to have:
Select * From [dbo].[EmployeeToJobTitleMatchLink]
LinkSurrogateUUID (UniqueIdentifier, SurrogateKey)
PriorityRank (scalar, int)
JobStartedOnDate (scalar,datetime)
TheEmployeeUUID (UniqueIdentifier, FK back to dbo.Employee.EmployeeUUID )
TheJobTitleUUID (UniqueIdentifier, FK back to dbo.JobTitle.JobTitleUUID )
The attribute(s) on the ~relationship are very important to keep. (PriorityRank and JobStartedOnDate in this mock up example.)
Thanks. I'm ~so close.
EDIT:
public class EmployeeMap : ClassMap<Employee>
{
public EmployeeMap()
{
Id(x => x.EmployeeUUID).GeneratedBy.GuidComb();
OptimisticLock.Version();
Version(x => x.TheVersionProperty)
.Column("MyVersionColumn")
.Not.Nullable()
.CustomSqlType("timestamp")
.Generated.Always();
Map(x => x.SSN);
Map(x => x.LastName);
Map(x => x.FirstName);
Map(x => x.CreateDate);
Map(x => x.HireDate);
HasMany(x => x.MyEmployeeToJobTitleMatchLinks)
.Inverse()
.Cascade.All()
.KeyColumn("TheEmployeeUUID")
;
}
}
public class JobTitleMap : ClassMap<JobTitle>
{
public JobTitleMap()
{
Id(x => x.JobTitleUUID).GeneratedBy.GuidComb();
OptimisticLock.Version();
Version(x => x.TheVersionProperty)
.Column("MyVersionColumn")
.Not.Nullable()
.CustomSqlType("timestamp")
.Generated.Always();
Map(x => x.JobTitleName);
Map(x => x.CreateDate);
HasMany(x => x.MyJobTitleToEmployeeMatchLinks)
.Inverse()
.Cascade.All()
.KeyColumn("TheJobTitleUUID")
;
}
}
public class EmployeeToJobTitleMatchLinkMap : ClassMap<EmployeeToJobTitleMatchLink>
{
public EmployeeToJobTitleMatchLinkMap()
{
Id(x => x.LinkSurrogateUUID).GeneratedBy.GuidComb();
Map(x => x.PriorityRank);
Map(x => x.JobStartedOnDate);
References(x => x.TheEmployee).Column("TheEmployeeUUID").Not.Nullable();/*Bad naming convention with "The", but left here so it can be seen easily in the DDL*/
References(x => x.TheJobTitle).Column("TheJobTitleUUID").Not.Nullable();/*Bad naming convention with "The", but left here so it can be seen easily in the DDL*/
}
}
Thanks Nathan!
PS One new term I learned while googling/binging myself was
"objectified relationship"
It was in the comments area of this page: LINK1
In case that page dies sometime in the future, here is that commented pasted in:
It’s called an ‘objectified relationship’ (ref: http://www.orm.net) and in NIAM/ORM it’s typically defined as a relationship which is on itself an entity with attributes. An objectified relationship is always forming at least one m:n relationship. (From http://weblogs.asp.net/fbouma/ )
I think you need to add a KeyColumn("key-name") as part of the HasMany mapping to both your JobTitleMap and EmplyeeMap. This is because fluent-nhibernate is using conventions to create the FK in the EmployeeToJobTitleMatchLink table. Using KeyColumn as part of the HasMay mapping should override the convention.
Something like the following :-
public class JobTitleMap : ClassMap<JobTitle>
{
public JobTitleMap()
{
Id(x => x.JobTitleUUID).GeneratedBy.GuidComb();
OptimisticLock.Version();
Version(x => x.TheVersionProperty)
.Column("MyVersionColumn")
.Not.Nullable()
.CustomSqlType("timestamp")
.Generated.Always();
Map(x => x.JobTitleName);
Map(x => x.CreateDate);
HasMany(x => x.MyJobTitleToEmployeeMatchLinks)
.Inverse()
.Cascade.All()
**.KeyColumn("TheJobTitleUUID")**
}
}