Search in sources :

Example 1 with CriteriaBuilder

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)");
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Film(org.jooq.example.jpa.entity.Film) Language(org.jooq.example.jpa.entity.Language) Actor(org.jooq.example.jpa.entity.Actor) Record(org.jooq.Record) Tuple(jakarta.persistence.Tuple)

Example 2 with CriteriaBuilder

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));
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Film(org.jooq.example.jpa.entity.Film) Record(org.jooq.Record)

Example 3 with CriteriaBuilder

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)");
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Film(org.jooq.example.jpa.entity.Film) Language(org.jooq.example.jpa.entity.Language) Record(org.jooq.Record) Tuple(jakarta.persistence.Tuple)

Aggregations

CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)3 Record (org.jooq.Record)3 Film (org.jooq.example.jpa.entity.Film)3 Tuple (jakarta.persistence.Tuple)2 Language (org.jooq.example.jpa.entity.Language)2 Actor (org.jooq.example.jpa.entity.Actor)1