1
Guys I’m having difficulty in mapping composite primary key using nhibernate Fluent. I have the following class:
public class NotaItem
{
public NotaItem(){}
public virtual int Id { get; set; } //chave primaria
public virtual Int16 NumeroSequencialItem { get; set; } //chave primaria
public virtual Int16 NumeroOrdem { get; set; }
}
Mapping:
public class NotaItemMap : ClassMap<NotaItem>
{
public NotaItemMap()
{
Table("NOTITEM");
//Chave primária
CompositeId().KeyReference(x => x.Id, "NOTIT_ID")
.KeyProperty(x => x.NumeroSequencialItem, "NOTIT_NR_SEQUENCIAL_ITEM");
Map(x => x.NumeroOrdem)
.Column("NOTIT_NR_ORDEM")
.Not.Nullable();
}
}
I would like to know how best to implement this, because it is not working in this way. Note: Primary key fields is not auto increment.
Log of exception: Execution :
Fluentnhibernate.cfg.Fluentconfigurationexception: An invalid or incomplete Configuration was used while Creating a Sessionfactory. Check Potentialreasons Collection, and Innerexception for more Detail.
---> Fluentnhibernate.cfg.Fluentconfigurationexception: An invalid or incomplete Configuration was used while Creating a Sessionfactory. Check Potentialreasons Collection, and Innerexception for more Detail.
---> Nhibernate.Mappingexception: Could not Compile the Mapping Ocument: (Xmldocument) ---> Nhibernate.Mappingexception: Posit-id class must override Equals(): Trainingnhibernate.Core.Entity.Notaitem in Nhibernate.cfg.Xmlhbmbinding.ClassCompositeIdBinder.Checkequalsandgethashcodeoverride() in Nhibernate.cfg.Xmlhbmbinding.ClassCompositeIdBinder.Bindcompositeid(Hbmcompositeid idSchema, Persistentclass rootClass) in Nhibernate.cfg.Xmlhbmbinding.RootClassBinder.Bind(Hbmclass classSchema, Idictionary
2 inheritedMetas) em NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddEntitiesMappings(HbmMapping mappingSchema, IDictionary
2 inheritedMetas) in Nhibernate.cfg.Xmlhbmbinding.MappingRootBinder.Bind(Hbmmapping mappingSchema) in Nhibernate.cfg.Configuration.Adddeserializedmapping(Hbmmapping mappingDocument, String documentFileName) --- End of tracking stack of internal exceptions --- in Nhibernate.cfg.Configuration.Logandthrow(Exception Exception) in Nhibernate.cfg.Configuration.Adddeserializedmapping(Hbmmapping mappingDocument, String documentFileName) Nhibernate.cfg.Configuration.Processmappingsqueue() in Nhibernate.cfg.Configuration.Addinputstream(Stream xmlInputStream, String name) in Nhibernate.cfg.Configuration.Adddocument(Xmldocument doc, String name) in Nhibernate.cfg.Configuration.Adddocument(Xmldocument doc) in Fluentnhibernate.PersistenceModel.Configure(Configuration cfg) in Fluentnhibernate.cfg.Mappingconfiguration.Apply(Configuration cfg)
in Fluentnhibernate.cfg.Fluentconfiguration.Buildconfiguration()
--- End of stack tracking of internal exceptions --- in Fluentnhibernate.cfg.Fluentconfiguration.Buildconfiguration() in Fluentnhibernate.cfg.Fluentconfiguration.Buildsessionfactory() ---- End of stack tracking of internal exceptions --- in Fluentnhibernate.cfg.Fluentconfiguration.Buildsessionfactory() in TreinamentoNHibernate.Data.SessionFactory.SessionFactoryUtil.. cctor() in E: Visualcsharp Projects Trainingnhibernate Trainingnhibernate.Data Sessionfactory Sessionfactoryutil.Cs:line 46
What problem are you with the current implementation?
– PauloHDSousa
What is going wrong that you expected a different result? Your composite key mapping, as far as I could see, is correct!
– Fernando Leal
Can edit the question with error logging at login?
– Fernando Leal
The exception is raised when fetching database data.
– Marcos Vinicius
I ran a test implementing with a simple key: Id(t => t.Id). Column("NOTIT_ID"); I changed the whole line of "Compositeid()" by the one above and it worked. No exception!
– Marcos Vinicius