How to map a CompositeID as the primary key

3,360 views
Skip to first unread message

Russell Petersen

unread,
May 13, 2011, 2:29:57 AM5/13/11
to fluent-n...@googlegroups.com
The mapping of a the table is 

public class AATResultMap : ClassMap<AATResult>
{
public AATResultMap()
{
Table("AATResults");
CompositeId()
.KeyReference(x => x.SpecBarCode)
.KeyReference(x => x.Seq);
Map(x => x.TestDt);
Map(x => x.CreatedDt);
}
}

The combined fields form the unique key. How can I work with this type of file in Fluent and NHibernate? Can I combine some auto mapping and some manual mapping? Could I map this one table manually and all the other tables with the auto mapper? Or, am I just lost in using NHibernate on this type of table because it doesn't have a simple single field primary key?

Thanks

James Gregory

unread,
May 13, 2011, 4:14:30 AM5/13/11
to fluent-n...@googlegroups.com
I'm confused. What's the problem?

Russell Petersen

unread,
May 13, 2011, 10:17:01 AM5/13/11
to Fluent NHibernate
On May 13, 2:14 am, James Gregory <jagregory....@gmail.com> wrote:
> I'm confused. What's the problem?

Sorry for causing the confusion. When I re-read what I wrote I can see
I didn't state the problem very clearly.

I've setup the primary key on the table as the CompositeId in the
example code. When the program is getting started it executes

Session = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(c =>
c.FromConnectionStringWithKey("ConnStr")))
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<Env>())
.BuildSessionFactory();

and this error message is reported:

FluentNHibernate.Cfg.FluentConfigurationException: An invalid or
incomplete configuration was used while creating a SessionFactory.
Check PotentialReasons collection, and InnerException for more detail.

---> NHibernate.MappingException: An association from the table
AATResults refers to an unmapped class: System.String
at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)

Everything I've been able research on the problem indicates that
Fluent requires a single field as the primary key on the table. So, I
would like to use the automapping of Fluent on all the tables that
have a single field primary key and then use classic NHibernate xml
file mapping on the tables that have a two field composite primary
key.

Can Fluent auto mapping and NHibernate xml mapping be combined when
configuring the session, if so, where can I read more about it?

Thanks,
Russ

TigerShark

unread,
May 27, 2011, 11:41:32 AM5/27/11
to Fluent NHibernate
Shouldn't you use KeyProperty instead of KeyReference? I believe
KeyReference is the same as References(..) and KeyProperty is the same
as Map(..).
Since, I think, as I haven't seen your class definition, the property
SpecBarCode and/or Seq is a string, it's not referencing another type,
which is why you get the exception.

Dennis Alba

unread,
May 27, 2011, 12:21:19 PM5/27/11
to fluent-n...@googlegroups.com
this is the way to make a compositeId key:

persistent entitie
public partial class AR
{
public virtual string IdA { get; set; }
public virtual string IdA2 { get; set; }
public virtual string A1 { get; set; }
public virtual string A2 { get; set; }
public virtual char A3 { get; set; }
public override int GetHashCode()
{
return this.IdA.GetHashCode() + this.IdA2.GetHashCode();
}

public override bool Equals(object obj)
{
return base.Equals(obj);
}

maping class

public class MapeoA: ClassMap<AR>
{
public MapeoA()
{
CompositeId().KeyProperty(x => x.IdA, "ID_A")
.KeyProperty(x => x.IdA2, "ID_A2");


Map(x => x.A1).Column("A1").Length(11);
Map(x => x.A2).Column("A2").Length(8);
Map(x => x.A3).Column("A3").Length(1);
}
}

if one of the fields that are part of the composite key is a reference
to another class:

public partial class BR
{
public virtual string IdB { get; set; }
public virtual string B1 { get; set; }
public virtual string B2 { get; set; }
}

public partial class AR
{
public virtual string IdA { get; set; }

public virtual BR br { get; set; } <---------------------------------

public virtual string A1 { get; set; }

public virtual string A2 { get; set; }

public virtual char A3 { get; set; }

public override int GetHashCode()
{
return this.IdA.GetHashCode() + this.br.GetHashCode();
}

public override bool Equals(object obj)
{
return base.Equals(obj);
}


public class MapeoA: ClassMap<AR>
{
public MapeoA()
{
CompositeId().KeyProperty(x => x.IdA, "ID_A")
.KeyReferences(x => x.br, "ID_A2"); <-------------------


Map(x => x.A1).Column("A1").Length(11);
Map(x => x.A2).Column("A2").Length(8);
Map(x => x.A3).Column("A3").Length(1);
}
}


2011/5/27, TigerShark <kenneth...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups
> "Fluent NHibernate" group.
> To post to this group, send email to fluent-n...@googlegroups.com.
> To unsubscribe from this group, send email to
> fluent-nhibern...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/fluent-nhibernate?hl=en.
>
>

Reply all
Reply to author
Forward
0 new messages