0
I’m studying NHibernate
and is giving me an error in group by
,
but at first it is generating me wrong sql
, do not know why, follow the code below SQL and its structure.
string hql = "select p.Categoria, count(p) " +
"from Produto p " +
"group by p.Categoria";
IQuery query = session.CreateQuery(hql);
var resultados = query.List<Object[]>();
And the class structure is like this:
public class Produto
{
public virtual int Id { get; set; }
public virtual string Nome { get; set; }
public virtual decimal Preco { get; set; }
public virtual Categoria Categoria { get; set; }
}
public class Categoria
{
public virtual int Id { get; set; }
public virtual string Nome { get; set; }
public virtual IList<Produto> Produtos { get; set; }
}
The sql generated by Nhibernate:
select
produto0_.CategoriaId as col_0_0_,
count(produto0_.Id) as col_1_0_,
categoria1_.Id as id1_2_,
categoria1_.Nome as nome2_2_
from
Produto produto0_
inner join
Categoria categoria1_
on produto0_.CategoriaId=categoria1_.Id
group by
produto0_.CategoriaId
But it is returning me the error below:
'could not execute query [ select product0_.Categoriaid as col_0_0_, Count(product0_.Id) as col_1_0_, categoria1_.Id as id1_2_, categoria1_.Name as nome2_2_ from Product product0_ Inner Join Category categoria1_ on product0_.Categoriaid=categoria1_.Id group by product0_.Categoriaid ] [SQL: select product0_.Categoriaid as col_0_0_, Count(product0_.Id) as col_1_0_, categoria1_.Id as id1_2_, categoria1_.Nome as nome2_2_ from Product product0_ Inner Join Categoria categoria1_ on product0_.Categoriaid=categoria1_.Id group by product0_.Categoriaid]'
In my view, the SQL
of group by
should also inform the columns of the category, so it is giving error, but I could not find out why it is not generating.
already tried to explain the fields?
– novic
Explaining the fields in select and in the group by ai of right, but the idea is not to need to clarify since if there was an increase of fields, continue working automatically.
– Matheus Rizzi
Ai you are programming wrong Matheus because you are creating a poor performance in your SQL which is in most systems the big bottleneck ... This is my view through experience, why select fields that will not be used? Something else if the algorithm is bad Hibernate? boy are so many strands... that in my experience I like to have mastery of the code
– novic
You’re right about this. But still it should work because in theory it should return me an object of type category with all the information of the category. but thanks for the return.
– Matheus Rizzi
Sure I can agree but, I didn’t see in the documentation that this is so, if you found something?
– novic