1
I need help in a query you should search for:
PK_PARENT, NOME_PAI, {NOME_FILHO1, NOME_FILHO2, ..., NOME_FILHO*}
For research I have already tried to do research in some ways like:
var listaPai = (from parent in session.Query<Parent>() select parent).ToList();
var dto = listaPai.Select(parent => new DTOListaPai
{
Codigo = parent.Codigo,
NomePai = parent.Nome,
// aqui eu faço uma consulta e já monto uma string com o formato
// desejado
NomesFilho = string.Join(",", session.Query<Child>()
.Where<Child>(
child => child.Parent.Codigo == parent.Codigo)
.ToList())
ERROR: When query validates
Where<Child>(child => child.Parent.Codigo == parent.Codigo)
the methodGetHashCode()
of the Child class is invoked and the moment it attempts to do the conversionConvert.ToInt32(this.Parent.Codigo)
bursts an instance of null object exception. I think I’m missing at this point, or maybe some annotation missing in the mappings.
Details about the implementation in the database are commented on in the code below.
Example classes:
public class Parent {
//pk_parent == Codigo
public virtual long Codigo { get; set; }
public virtual string NomePai { get; set; }
public virtual List<Child> Childs { get; set; }
public bool Equals(object obj1)
{
var obj2 = obj1 as Parent;
if (ReferenceEquals(null, obj2)) return false;
if (ReferenceEquals(this, obj2)) return true;
return this.Codigo == obj2.Codigo;
}
public int GetHashCode()
{
return Convert.ToInt32(this.Codigo);
}
}
public class Child {
// pk_child == NomeFilho && Parent.Codigo
public virtual string NomeFilho { get; set; }
// fk_parent == Parent.Codigo
public virtual Parent Parent { get; set; }
public override bool Equals(object obj1)
{
var obj2 = obj1 as Child;
if (ReferenceEquals(null, obj2)) return false;
if (ReferenceEquals(this, obj2)) return true;
return this.NomeFilho == obj2.NomeFilho &&
this.Parent.Codigo == obj2.Parent.Codigo;
}
public override int GetHashCode()
{
return Convert.ToInt32(this.Parent.Codigo) + NomeFilho.GetHashCode();
}
}
- pk_child is composed of fk_parent + filename
Mappings:
public class ParentMap : ClassMap<Parent>
{
Table("tb_parent");
Id(parent => parent.Codigo)
.Not.Nullable()
.Column("pk_parent")
.GeneratedBy.Sequence("tb_parent_pk_parent_seq");
Map(parent => parent.Nome).Column("nome_pai")
.Not.Nullable();
HasMany(parent => parent.Childs)
.KeyColumn("fk_parent")
.Cascade.All()
.AsBag();
}
public class ChildMap : ClassMap<Child>
{
Table("tb_child");
CompositeId()
.KeyProperty(child => child.Parent.Codigo, "fk_parent")
.KeyProperty(child=> child.Nome, "nome_filho");
References(child => child.Parent, "fk_parent");
}