Problem with aggregation function, using Hibernate Createquery

Asked

Viewed 108 times

1

  • In a Package, a fkEmbaling refers to the 'father', the larger package. EX: Let’s say that 10 A packs fit inside a B package. A packaging record A will have in its fkEmballing the packaging B in which it will be inserted.

  • There is a query that searches how many type B packages exist with less than x type A packages associated with them.

.createQuery("SELECT e.fkEmbalagem FROM Embalagem e " + "WHERE e.fkEmbalagem IS NOT NULL " + "AND e.fkTipoEmbalagem.id = :tipo " + "GROUP BY e.fkEmbalagem " + "HAVING COUNT(e.id) < : x")

The query claims to: Column Packaging.ID is invalid in the select list because it is not contained in either an Aggregate Function or the GROUP BY clause.

As far as I know, when the query has aggregation function, I can only put in the select something that is either in the aggregation function itself or in the group by. O e.fkEmbaling is in group by, so I don’t know why it goes wrong.

UPDATE:

I realized that the problem is when you switch to SQL, because what would be an e.fkEmbaling in Hibernate becomes a lot of things when you pass pro select in SQL (e.g., packaging.id, packaging.data_alteration, etc.) and he complains that these items that are in select should also be in the aggregation function or group by. But the object that represents it all is. How do I resolve this? I need to return the entire object!

If you just : SELECT e.fkEmbalagem.id [...] GOUP BY e.fkEmbalagem.id works, but then I’m only picking up ID. I wanted the whole object.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.