Problem with aggregate function, using CreateQuery from Hibernate

1
  • In a Packaging, a fk Packaging refers to the 'parent', the larger packaging. EX: let's say that inside a package B fit 10 packages A. A packing record A will have in your package the packing B in which it will be inserted.

  • There is a query that looks for 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: Column Packing.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 aggregate function, I can only put in the select something that is either in the aggregate function itself or in the group by. The e.fkPacking is in the group by, so I do not know why it goes wrong.

UPDATE:

I noticed that the problem is when it goes to SQL, because what would be an e.fkPackaging in hibernate turns a lot of things when it passes pro select in SQL (ex: packing.id, packing.data_changing, etc) and it claims because these items that are in the select should also be in the aggregate function or group by. But the object that represents all this is. How do I solve this? I need to return the entire object!

If you only do: SELECT e.fkEmbalagem.id [...] GOUP BY e.fkEmbalagem.id It works, but then I'm just getting ID. I wanted the whole object.

    
asked by anonymous 30.07.2015 / 21:42

0 answers