use of jakarta.persistence.criteria.CriteriaBuilder in project jOOQ by jOOQ.
the class CriteriaQueryOrJOOQ method numberOfActorsPerLanguage.
private static void numberOfActorsPerLanguage(EntityManager em, DSLContext ctx) {
// Using criteria query
// --------------------
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Tuple> q = qb.createTupleQuery();
Root<Film> filmRoot = q.from(Film.class);
Join<Film, Language> filmJoin = filmRoot.join(Film_.language);
Path<String> languagePath = filmJoin.get(Language_.name);
SetJoin<Film, Actor> actorJoin = filmRoot.join(Film_.actors);
Path<Integer> actorPath = actorJoin.get(Actor_.actorId);
Expression<Long> count = qb.countDistinct(actorPath);
q = q.multiselect(languagePath, count);
q = q.groupBy(languagePath);
TypedQuery<Tuple> typed = em.createQuery(q);
for (Tuple tuple : typed.getResultList()) System.out.println(tuple.get(languagePath) + " (" + tuple.get(count) + " actors)");
for (Record rec : ctx.select(LANGUAGE.NAME, countDistinct(FILM_ACTOR.ACTORS_ACTORID).as("c")).from(LANGUAGE).join(FILM).on(FILM.LANGUAGE_LANGUAGEID.eq(LANGUAGE.LANGUAGEID)).join(FILM_ACTOR).on(FILM.FILMID.eq(FILM_ACTOR.FILMS_FILMID)).groupBy(LANGUAGE.NAME)) System.out.println(rec.get(LANGUAGE.NAME) + " (" + rec.get("c") + " actors)");
}
use of jakarta.persistence.criteria.CriteriaBuilder in project jOOQ by jOOQ.
the class CriteriaQueryOrJOOQ method filmLengthAndLanguages.
private static void filmLengthAndLanguages(EntityManager em, DSLContext ctx) {
// Using criteria query
// --------------------
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Film> q = qb.createQuery(Film.class);
Root<Film> root = q.from(Film.class);
q = q.select(root);
TypedQuery<Film> typed = em.createQuery(q);
for (Film film : typed.getResultList()) System.out.println(film.title.value + " (" + film.length + " minutes) in " + film.language.name);
for (Record rec : ctx.select(FILM.TITLE, FILM.LENGTH, LANGUAGE.NAME).from(FILM).join(LANGUAGE).on(FILM.LANGUAGE_LANGUAGEID.eq(LANGUAGE.LANGUAGEID))) System.out.println(rec.get(FILM.TITLE) + " (" + rec.get(FILM.LENGTH) + " minutes) in " + rec.get(LANGUAGE.NAME));
}
use of jakarta.persistence.criteria.CriteriaBuilder in project jOOQ by jOOQ.
the class CriteriaQueryOrJOOQ method numberOfFilmsPerLanguage.
private static void numberOfFilmsPerLanguage(EntityManager em, DSLContext ctx) {
// Using criteria query
// --------------------
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Tuple> q = qb.createTupleQuery();
Root<Film> filmRoot = q.from(Film.class);
Join<Film, Language> filmJoin = filmRoot.join(Film_.language);
Path<String> languagePath = filmJoin.get(Language_.name);
Path<Integer> filmPath = filmRoot.get(Film_.filmId);
Expression<Long> count = qb.count(filmPath);
q = q.multiselect(languagePath, count);
q = q.groupBy(languagePath);
TypedQuery<Tuple> typed = em.createQuery(q);
for (Tuple tuple : typed.getResultList()) System.out.println(tuple.get(languagePath) + " (" + tuple.get(count) + " films)");
for (Record rec : ctx.select(LANGUAGE.NAME, count()).from(LANGUAGE).join(FILM).on(FILM.LANGUAGE_LANGUAGEID.eq(LANGUAGE.LANGUAGEID)).groupBy(LANGUAGE.NAME)) System.out.println(rec.get(LANGUAGE.NAME) + " (" + rec.get(count()) + " films)");
}