9
Always when I try to save an object with a data collection mapped as HasMany
I have to put a circular reference so that Nhibernate can save this object in cascade.
For example I have the following parent class mapping.
public class ClasseMap : ClassMap<Classe>
{
public ClasseMap()
{
Table("tClasse");
HasMany<Metodo>(t => t.Metodos)
.KeyColumn("IdClasse")
.Inverse()
.Cascade
.SaveUpdate();
}
}
And in the daughter class
public class MetodoMap : ClassMap<Metodo>
{
public MetodoMap()
{
Table("tMetodo");
References(t => t.Classe)
.Column("IdClasse")
.ForeignKey("FK_Metodo_IdClasse");
}
}
When I will save the data of my object Classe
I’ll tell you what:
public class RepositorioClasse
{
protected readonly ISession SessaoAtual;
public RepositorioClasse(ISession sessaoAtual)
{
SessaoAtual = sessaoAtual;
}
public SalvaEntidades() {
var classe = new Classe();
var metodo = new Metodo(){
Nome= "ToString",
TipoRetorno="String",
Classe= classe
};
classe.Nome = "String";
classe.Metodos.Add(metodo);
SessaoAtual.SaveUpdate(classe);
}
}
If I only put up a collection of Metodos
in Classe
and remove the reference to Classe
in Metodo
Nhibernate does not save and returns an exception saying that I must have a reference of Classe
in Metodo
.
Hello Mark. Could you complement your question by placing the original message of the exception which occurs?
– Jônatas Hudler