Exception:
System.InvalidCastException: Can not convert an object of type 'NHibernate.Type.ManyToOneType' to type 'NHibernate.Type.ComponentType'.
The classes involved are:
public class ClassePrincipal
{
public virtual long Codigo { get; set; }
public virtual string Atributo1 { get; set; }
public virtual EEnumeradorAbstrato EnumeradorAbstrato { get; set; }
public virtual SubClasse SubClasse { get; set; }
}
public class SubClasse
{
public virtual string Atributo1 { get; set; }
public virtual double Atributo2 { get; set; }
/// <summary>
/// Bi-direcionamento
/// </summary>
public virtual ClassePrincipal ClassePrincipal { get; set; }
}
The mappings:
public class ClassePrincipalMap : ClassMap<ClassePrincipal>
{
public ClassePrincipalMap()
{
Table("tb_classe_principal");
Id(ClassePrincipal => ClassePrincipal.Codigo)
.Not.Nullable()
.Column("pk_classe_principal")
.GeneratedBy.Sequence("tb_classe_principal_pk_classe_principal_seq");
Map(a => a.Atributo1).Column("atributo1");
Component(ClassePrincipal => ClassePrincipal.EnumeradorAbstrato,
componentPart =>
componentPart.Map(eEnumeradorAbstrato => eEnumeradorAbstrato.Identificador,
"enumerador_abstrato"));
HasOne(ClassePrincipal => ClassePrincipal.SubClasse)
.Cascade.All()
.Constrained()
.ForeignKey("fk_classe_principal");
}
}
public class SubClasseMap : ClassMap<SubClasse>
{
public SubClasseMap()
{
Table("tb_SubClasse");
Id(SubClasse => SubClasse.ClassePrincipal.Codigo)
.Column("fk_classe_principal")
.GeneratedBy.Foreign("ClassePrincipal.Codigo");
Map(SubClasse => SubClasse.Atributo1).Column("atributo1");
Map(SubClasse => SubClasse.Atributo2).Column("atributo2");
References(SubClasse => SubClasse.ClassePrincipal, "fk_classe_principal").Cascade.None();
}
}
Specifications:
- The error occurred after I added the annotation ".GeneratedBy.Foreign (" MasterClass.Code ");
-
The same occurs when I invoke the session.Save () method right after the "select nextval ('sequence');"
Thank you.