There are several problems that can come up when size() is used as a select expression.
Using the following code:
{code:java} @Entity(name ="Company") public class Company {
@Id private int id; private String name;
@ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER) private List<Customer> customers = new ArrayList<>(); }
@Entity(name = "Customer") public static class Customer {
@Id private int id; private String name; }
public static class CompanyDto {
public int id; public String name; public int sizeCustomer;
public CompanyDto(int id, String name, int sizeCustomer){ this.id = id; this.name = name; this.sizeCustomer = sizeCustomer; } } {code}
1) There is no result when a collection is Results from the following query do not include an {{Company}} with an empty {{customers}} collection:
select new org . hibernate.test.hql.size.ManyToManySizeTest$CompanyDto(c.id, c.name, size( c.customers ) ) from Company c group by c.id, c.name order by c.id For example:
---- Original description:
I'm using the following HQL and it return a correct result :
{code:java} select distinct new net.rhuanrocha.dto.CompanyDto(c.id,c.name,0) from Company c left join c.customers cu where c.name =:name group by c.id, c.name
{code}
But when I use the size function in my HQL the query results is wrong (In my case is returning an empty result). Look this HQL:
{code:java} select distinct new net.rhuanrocha.dto.CompanyDto(c.id,c.name,size(c.customers)) from Company c left join c.customers cu where c.name =:name group by c.id, c.name {code} |
|