Criteria API and JPQL API with GROUP BY and GROUP_CONCAT with DISTINCT / ORDER BY / SEPERATOR Support?

One of the solutions is to create a custom GROUP_CONCAT HQL function that is translated to SQL. Idea is to create function: group_concat(name, true, ‘ # ‘, name, ‘DESC’) 1: name of the column for aggregation 2: true\false use DISTINCT or not 3: the separator for concatenation 4: column name for ORDER BY 5: sorting … Read more

JPA Select latest instance for each item

In SQL the solution is very simple – join the table with a subquery, which gives you the most recent meeting for each attendee: select * from Meeting ALL join ( select max(meetingDate) as newest, attendee from Meeting group by attendee ) LATEST on ALL.meetingDate = LATEST.newest AND ALL.attendee = LATEST.attendee This works, and works … Read more

JPA: JOIN in JPQL

Join on one-to-many relation in JPQL looks as follows: select b.fname, b.lname from Users b JOIN b.groups c where c.groupName = :groupName When several properties are specified in select clause, result is returned as Object[]: Object[] temp = (Object[]) em.createNamedQuery(“…”) .setParameter(“groupName”, groupName) .getSingleResult(); String fname = (String) temp[0]; String lname = (String) temp[1]; By the … Read more

JPQL Like Case Insensitive

You can use the concat operator: @Query(“select u from User u where lower(u.name) like lower(concat(‘%’, ?1,’%’))”) public List<User> findByNameFree(String name); or with a named parameter: @Query(“select u from User u where lower(u.name) like lower(concat(‘%’, :nameToFind,’%’))”) public List<User> findByNameFree(@Param(“nameToFind”) String name); (Tested with Spring Boot 1.4.3)