Making a reference to a class with a composite ID

2,457 views
Skip to first unread message

Matt

unread,
Mar 27, 2009, 2:34:40 PM3/27/09
to Fluent NHibernate
So we are working on a Legacy Database that has some interesting table
setups.

What we have is a class that contains a state and county. Which we
have mapped like so:

UseCompositeID().WithKeyProperty(x => x.ID, "XXXXXX").WithKeyProperty
(x => x.ID, "YYYYYY");

So now the issue comes when we go to map to that Reference.

The table we are mapping from has different column names to begin
with, and the bridge table has FK on both the keys, where the table we
are referencing from does not.

I've tried many of the Reference ideas such as "WithColumnNames" but
that throws an error and does not build. If anyone could provide a
little insight on what we may do to be able to fix this issue, it
would be much appreciated.

James Gregory

unread,
Mar 27, 2009, 6:32:25 PM3/27/09
to fluent-n...@googlegroups.com
Could you show an example of your schema?

nobodybutca

unread,
Mar 28, 2009, 7:29:11 AM3/28/09
to Fluent NHibernate
Mine is here:

Error is :
NHibernate.MappingException: composite-id class must override Equals
(): Competency.Domain.AccessibleDocumentsDto
NHibernate.MappingException: Could not compile the mapping document:
(XmlDocument)

public class AccessibleDocumentsDto
{
public virtual int RoleId { get; set; }
public virtual int DocId { get; set; }
public virtual bool WithAccess { get; set; }
}

public class AccessibleDocumentsDtoMap :
ClassMap<AccessibleDocumentsDto>
{
public AccessibleDocumentsDtoMap()
{
WithTable("dbo.RoleDocument");
UseCompositeId()
.WithKeyProperty(x => x.RoleId, "RoleID")
.WithKeyProperty(x => x.DocId, "DocID");
Map(x => x.WithAccess)
.FormulaIs("case when AllowFullAccess = 1 or AllowEdit
= 1 or AllowRead = 1 or AllowView = 1 then 1 else 0 end");
}
}


XMB File:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Entities.RoleDocument, Entities"
table="dbo.RoleDocument">
<composite-id name="Id" class="Entities">
<key-property name="DocID" column="DocID" type="System.Int32"/>
<key-property name="RoleID" column="RoleID" type="System.Int32"/>
</composite-id>
<property name="AllowEdit" column="AllowEdit" type="System.Boolean"
not-null="true"/>
<property name="AllowFullAccess" column="AllowFullAccess"
type="System.Boolean" not-null="true"/>
<property name="AllowRead" column="AllowRead" type="System.Boolean"
not-null="true"/>
<property name="AllowView" column="AllowView" type="System.Boolean"
not-null="true"/>
</class>
</hibernate-mapping>


SCHEMA:
CREATE TABLE [dbo].[RoleDocument](
[RoleID] [int] NOT NULL,
[DocID] [int] NOT NULL,
[AllowFullAccess] [bit] NOT NULL CONSTRAINT
[DF_RoleDocument_AllowFullAccess] DEFAULT (0),
[AllowEdit] [bit] NOT NULL CONSTRAINT [DF_RoleDocument_AllowEdit]
DEFAULT (0),
[AllowRead] [bit] NOT NULL CONSTRAINT [DF_RoleDocument_AllowRead]
DEFAULT (0),
[AllowView] [bit] NOT NULL CONSTRAINT [DF_RoleDocument_AllowView]
DEFAULT (0),
CONSTRAINT [PK_RoleDocument] PRIMARY KEY CLUSTERED
(
[RoleID] ASC,
[DocID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, FILLFACTOR = 90) ON
[PRIMARY]
) ON [PRIMARY]


But if I use this Map:

public class AccessibleDocumentsDtoMap :
ClassMap<AccessibleDocumentsDto>
{
public AccessibleDocumentsDtoMap()
{
WithTable("dbo.RoleDocument");
Id(x => x.RoleId)
.TheColumnNameIs("RoleID")
.GeneratedBy.Assigned();
Map(x => x.DocId)
.ColumnName("DocID")
.Not.Nullable();
Map(x => x.WithAccess)
/.FormulaIs("case when AllowFullAccess = 1 or
AllowEdit = 1 or AllowRead = 1 or AllowView = 1 then 1 else 0 end");
}
}

All returned results are all duplicates, If we say it returns 100
records, all that 100 are identical. =(


I was suppose to ask you on twitter, if there are writing about
"UseCompositeId()" =)

James Gregory

unread,
Mar 28, 2009, 8:11:31 AM3/28/09
to fluent-n...@googlegroups.com
nobodybutca: Have you overridden Equals in your entity?

nobodybutca

unread,
Mar 28, 2009, 9:19:55 AM3/28/09
to Fluent NHibernate
James Gregory,

Nope? that's all of my codes above.


On Mar 28, 8:11 pm, James Gregory <jagregory....@gmail.com> wrote:
> nobodybutca: Have you overridden Equals in your entity?
>

James Gregory

unread,
Mar 28, 2009, 9:23:27 AM3/28/09
to fluent-n...@googlegroups.com
Well your exception says: NHibernate.MappingException: composite-id class must override Equals
()
: Competency.Domain.AccessibleDocumentsDto

nobodybutca

unread,
Mar 30, 2009, 12:45:54 AM3/30/09
to Fluent NHibernate
Hi guys,

I finally solved the issue by overriding Equals & GetHasCode:

public class AccessibleDocumentsDto
{
public virtual int RoleId { get; set; }
public virtual int DocId { get; set; }
public virtual bool WithAccess { get; set; }
public override int GetHashCode()
{
int hashCode = 0;
hashCode = hashCode ^ RoleId.GetHashCode() ^
DocId.GetHashCode();
return hashCode;
}

public override bool Equals(object obj)
{
var toCompare = obj as AccessibleDocumentsDto;
if (toCompare == null)
{
return false;
}
return (this.GetHashCode() != toCompare.GetHashCode());
}

}



Now working properly.

Thanks James.





On Mar 28, 9:23 pm, James Gregory <jagregory....@gmail.com> wrote:
> Well your exception says: NHibernate.MappingException: composite-id class
> must override Equals
> (): Competency.Domain.AccessibleDocumentsDto
>
Reply all
Reply to author
Forward
0 new messages