use of org.jooq.example.jpa.entity.Language 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 org.jooq.example.jpa.entity.Language 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)");
}
use of org.jooq.example.jpa.entity.Language 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();
}