Open kuyeol opened 1 month ago
6.3.13. GroupBy and Having The groupBy method of the CriteriaQuery interface is used to define a partitioning of the query results into groups. The having method of the CriteriaQuery interface can be used to filter over the groups. The arguments to the groupBy method are Expression instances. When the groupBy method is used, each selection item that is not the result of applying an aggregate method must correspond to a path expression that is used for defining the grouping. Requirements on the types that correspond to the elements of the grouping and having constructs and their relationship to the select items are as specified in Section 4.8. Example:
CriteriaQuery<Tuple> q = cb.createTupleQuery(); Root<Customer> customer = q.from(Customer.class);
q.groupBy(customer.get(Customer_.status)); q.having(cb.in(customer.get(Customer_.status)).value(1).value(2)); q.select(cb.tuple(
customer.get(Customer_.status), cb.avg(customer.get(Customer_.filledOrderCount)), cb.count(customer)));
This query is equivalent to the following Jakarta Persistence query language query:
SELECT c.status, AVG(c.filledOrderCount), COUNT(c) FROM Customer c
GROUP BY c.status HAVING c.status IN (1, 2)
6.3.13. GroupBy and Having
The groupBy method of the CriteriaQuery interface is used to define a partitioning of the query results into groups. The having method of the CriteriaQuery interface can be used to filter over the groups.
The arguments to the groupBy method are Expression instances.
When the groupBy method is used, each selection item that is not the result of applying an aggregate method must correspond to a path expression that is used for defining the grouping. Requirements on the types that correspond to the elements of the grouping and having constructs and their relationship to the select items are as specified in Section 4.8.
Example:
CriteriaQuery<Tuple> q = cb.createTupleQuery(); Root<Customer> customer = q.from(Customer.class);
q.groupBy(customer.get(Customer_.status)); q.having(cb.in(customer.get(Customer_.status)).value(1).value(2)); q.select(cb.tuple(
customer.get(Customer_.status), cb.avg(customer.get(Customer_.filledOrderCount)), cb.count(customer)));
This query is equivalent to the following Jakarta Persistence query language query:
SELECT c.status, AVG(c.filledOrderCount), COUNT(c) FROM Customer c
GROUP BY c.status HAVING c.status IN (1, 2)
`
Pro Jpa2 : ebook firstCheck! :1:
public Stream getGroupMembersStream(RealmModel realm, GroupModel group, String search, Boolean exact, Integer first, Integer max)
{
TypedQuery query;
if (StringUtil.isBlank(search)) {
query = em.createNamedQuery("groupMembership", UserEntity.class);
} else if (Boolean.TRUE.equals(exact)) {
query = em.createNamedQuery("groupMembershipByUser", UserEntity.class);
query.setParameter("search", search);
} else {
query = em.createNamedQuery("groupMembershipByUserContained", UserEntity.class);
query.setParameter("search", search.toLowerCase());
}
query.setParameter("groupId", group.getId());
//!?!?
try (Stream<Object[]> persons = session.createQuery( "select p.name, p.nickName " + "from Person p " + "where p.name like :name", Object[].class) .setParameter("name", "J%") .getResultStream()) { persons.map(row -> new PersonNames((String) row[0], (String) row[1])) .forEach(this::process); }
try(Stream persons = session.createQuery( "select p " + "from Person p " + "where p.name like :name", Person.class) .setParameter("name", "J%") .getResultStream())
{> callRegistry = persons.flatMap(person -> person.getPhones().stream()) .flatMap(phone -> phone.getCalls().stream()) .collect(Collectors.groupingBy(Call::getPhone));
Map<Phone, List
process(callRegistry); }
https://docs.jboss.org/hibernate/stable/orm/userguide/html_single/Hibernate_User_Guide.html#:~:text=public%20class%20PersonWrapper,createQuery(criteria).getResultList()%3B