This is an example of an object:
public class Account{ @Id Long id Double remaining; @ManyToOne AccountType type } public class AccountType{ @Id Long id; String name; }
Now I create a criteria query using Join as follwing:
CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder(); CriteriaQuery criteriaQuery = criteriaBuilder.createquery(); Root<Account> accountRoot = criteriaQuery.from(Account.class); Join<Account, AccountType> typeJoin = accountRoot.join(Account_.type); criteriaQuery.multiSelect( typeJoin, criteriaBuilder.sum(accountRoot.get(Account_.remaining)) ); criteriaQuery.groupBy(typeJoin); Query query = getEntityManager().createQuery(criteriaQuery); query.getResultList();
The above code generates an Sql command, as shown below:
select accType.id, accType.name, sum(acc.remaining) from account acc join accType on acc.accounttype_id = accType.id group by accType.id
The above code works in PosgreSQL, but cannot work in Oracle, because in it select accType.name, which does not appear in the group by clause.
update :
I think my question is incomprehensible to you. My question is not about the behavior of PostgreSQL or Oracle in group by . My question is: I use typeJoin in group by (this means that I expect hibernate to use the entire AccountType field in group by ), but why does sleep mode just use the authentication field on group by ? if I use only the identifier field in group by , then I can use the following statement:
criteriaQuery.groupBy(typeJoin.get(AccountType_.id)) ;
source share