Ler uma tabela com relacionamento N:1
23/10/2015
0
Boa noite pessoal.
Estou com um problema para ler uma tabela com um relacionamento N:1.
Eu define as classes, o mapeamento e o acesso assim:
Ao executar a leitura da tabela Followup da o erro "Followup is not mapped [select count(f) from Followup f where f.ContatoId = :contatoid]".
Como devo mapear esse relacionamento e qual a sintaxe do hql?
Antecipadamente, agradeço.
Diógenes
Estou com um problema para ler uma tabela com um relacionamento N:1.
Eu define as classes, o mapeamento e o acesso assim:
public class Contato { public virtual int ContatoId { get; set; } public virtual string Nome { get; set; } public virtual string ResidCEP { get; set; } public virtual string ResidLogrTipo { get; set; } public virtual string ResidLogrNome { get; set; } public virtual int? ResidNumero { get; set; } public virtual string ResidSetor { get; set; } public virtual string ResidQuadra { get; set; } public virtual string ResidLote { get; set; } public virtual string ResidBloco { get; set; } public virtual string ResidAndar { get; set; } public virtual string ResidUnidade { get; set; } public virtual string ResidComplemento { get; set; } public virtual string ResidBairro { get; set; } public virtual string ResidDistrito { get; set; } public virtual string ResidMunicipio { get; set; } public virtual string ResidUF { get; set; } } public class Followup { public virtual int FollowupId { get; set; } public virtual Contato ContatoId { get; set; } public virtual DateTime DtFollowup { get; set; } public virtual int UserId { get; set; } public virtual string Tipo { get; set; } public virtual string Texto { get; set; } } public class FollowupMapping : ClassMap<Followup> { public FollowupMapping() { Id(f => f.FollowupId).GeneratedBy.Identity(); References(f => f.ContatoId, "ContatoId").Not.Nullable(); Map(f => f.DtFollowup).Not.Nullable(); Map(f => f.UserId).Not.Nullable(); Map(f => f.Tipo).CustomSqlType("char(1)").Not.Nullable(); Map(f => f.Texto).CustomSqlType("nvarchar(255)").Not.Nullable(); } } public class FollowupDAO { public long ContaFollowupporContatoId(Contato contatoid) { using (ISession session = NHibernateHelper.AbreSessionCorreios()) { String hql = "select count(f) from Followup f where f.ContatoId = :contatoid"; IQuery query = session.CreateQuery(hql); query.SetParameter("contatoid", contatoid); var qtdReg = query.UniqueResult<long>(); return qtdReg; } } }
Ao executar a leitura da tabela Followup da o erro "Followup is not mapped [select count(f) from Followup f where f.ContatoId = :contatoid]".
Como devo mapear esse relacionamento e qual a sintaxe do hql?
Antecipadamente, agradeço.
Diógenes
Diógenes Freitas
Curtir tópico
+ 0
Responder
Posts
24/10/2015
Fabiano Carvalho
Sua consulta está errada!
Não existe colocar nome da tabela ou referencia da tabela dentro de uma função de agregação.
Faça isso.
ou
ou
Entende? ;)
Não existe colocar nome da tabela ou referencia da tabela dentro de uma função de agregação.
Faça isso.
select count(1) from Followup f where f.ContatoId = :contatoid
ou
select count(*) from Followup f where f.ContatoId = :contatoid
ou
select count(f.ContatoId) from Followup f where f.ContatoId = :contatoid
Entende? ;)
Responder
26/10/2015
Diógenes Freitas
Obrigado Fabiano mas eu já tentei essas query e não funcionaram.
Repare que na classe Followup, o atributo ContatoId é do tipo Contato, que é uma classe.
A consulta
funcionou, mas o hql "select count(*) from Followup f left join f.ContatoId c where c.ContatoId = :contatoid" não funciona,. Ja tentei com (*), (f.ContatoId), (F.FollowupId).
Repare que na classe Followup, o atributo ContatoId é do tipo Contato, que é uma classe.
A consulta
public IList<Followup> ListaporContatoId(int contatoid, int firstresult, int maxresult) { using (ISession session = NHibernateHelper.AbreSessionContato()) { string hql = "select f from Followup f left join f.ContatoId c where c.ContatoId = :contatoid"; IQuery query = session.CreateQuery(hql); query.SetParameter("contatoid", contatoid); query.SetFirstResult(firstresult); query.SetMaxResults(maxresult); return query.List<Followup>(); } }
funcionou, mas o hql "select count(*) from Followup f left join f.ContatoId c where c.ContatoId = :contatoid" não funciona,. Ja tentei com (*), (f.ContatoId), (F.FollowupId).
Responder
Clique aqui para fazer login e interagir na Comunidade :)