Search in sources :

Example 1 with Film

use of org.jooq.example.jpa.entity.Film 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 Film

use of org.jooq.example.jpa.entity.Film 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 Film

use of org.jooq.example.jpa.entity.Film 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)

Example 4 with Film

use of org.jooq.example.jpa.entity.Film in project jOOQ by jOOQ.

the class Setup method data.

static void data(EntityManager em) {
    // Set up database
    // ---------------
    Language english = new Language("English");
    Language german = new Language("German");
    Actor umaThurman = new Actor("Uma", "Thurman");
    Actor davidCarradine = new Actor("David", "Carradine");
    Actor darylHannah = new Actor("Daryl", "Hannah");
    Actor michaelAngarano = new Actor("Michael", "Angarano");
    Actor reeceThompson = new Actor("Reece", "Thompson");
    Film killBill = new Film(Title.of("Kill Bill"), english, 111, Year.of(2015));
    Film meerjungfrauen = new Film(Title.of("Meerjungfrauen ticken anders"), german, 89, Year.of(2017));
    killBill.actors.addAll(Arrays.asList(umaThurman, davidCarradine, darylHannah));
    meerjungfrauen.actors.addAll(Arrays.asList(umaThurman, michaelAngarano, reeceThompson));
    em.persist(english);
    em.persist(german);
    em.persist(umaThurman);
    em.persist(davidCarradine);
    em.persist(darylHannah);
    em.persist(michaelAngarano);
    em.persist(reeceThompson);
    em.persist(killBill);
    em.persist(meerjungfrauen);
    em.flush();
}
Also used : Language(org.jooq.example.jpa.entity.Language) Film(org.jooq.example.jpa.entity.Film) Actor(org.jooq.example.jpa.entity.Actor)

Aggregations

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