0
Hello, I need to return a list of child objects of the same kind as the father. however, using nhibernate, it only returns one child object, while in the bank I have 3 children. Below are excerpts of the code:
Data Stage
public Etapa ObterPorId(int id, bool completo = false)
{
if (!completo) return base.ObterPorId(id);
using (var session = HibernateUtil.GetSessionFactory().OpenSession())
{
return session.Query<Etapa>()
//.Fetch(e => e.EtapaFilhas) // já tentei com este tb..
.FetchMany(e => e.EtapaFilhas).FirstOrDefault(e => e.Id == id);
}
}
Etapamap:
public class EtapaMap:ClassMap<Etapa>
{
public EtapaMap()
{
Table("TEMA_ETAPA");
Not.LazyLoad();
Id(i => i.Id, "id").GeneratedBy.Identity();
HasMany(e => e.EtapaFilhas)
.Cascade.DeleteOrphan().KeyColumn("id_TEMA_ETAPA_pai");
References(e => e.EtapaPai).Column("id_TEMA_ETAPA_pai");
References(e => e.Tema).Column("id_TEMA_FORMULARIO");
}
}
Step (entity)
public class Etapa
{
public virtual int Id { get; set; }
public virtual Etapa EtapaPai { get; set; }
public virtual Tema Tema { get; set; }
public virtual ISet<Etapa> EtapaFilhas { get; set; }
}
Test method:
[Test]
public void DeveRetornarQuantidadeDeFilhas()
{
var etapaPaiId = 13;
var sut = new EtapaDados();
var result = sut.ObterPorId(etapaPaiId, true).EtapaFilhas;
Assert.That(result.Count, Is.EqualTo(3));
}
Using Linqpad or EF, it returns 3 children. I think I may be missing in the mapping or data call method, but I don’t know how to fix.
Alternatively, I have created a method that returns only children. However I wanted to avoid this approach, since I should access your children through the property ISet<Etapa> EtapaFilhas
:
public IEnumerable<Etapa> ObterFilhas(int idEtapaPai)
{
using (var session =
HibernateUtil.GetSessionFactory().OpenSession())
return session.Query<Etapa>()
.Where(e => e.EtapaPai.Id == idEtapaPai)
.ToList();
}