/** * Create a new aggregating map expression using a backing LinkedHashMap * * @param key * @param value * @return */ public static <K, V, T> AbstractGroupExpression<Pair<K, V>, Map<T, V>> map(GroupExpression<K, T> key, Expression<V> value) { return map(key, new GOne<V>(value)); }
/** * Create a new aggregating map expression using a backing TreeMap * * @param key * @param value * @return */ public static <K extends Comparable<? super K>, V, T extends Comparable<? super T>> AbstractGroupExpression<Pair<K, V>, SortedMap<T, V>> sortedMap(GroupExpression<K, T> key, Expression<V> value) { return sortedMap(key, new GOne<V>(value)); }
@Test @ExcludeIn(DERBY) public void Transform_GroupBy() { QCat kitten = new QCat("kitten"); Map<Integer, Cat> result = query().from(cat).innerJoin(cat.kittens, kitten) .transform(GroupBy.groupBy(cat.id) .as(Projections.constructor(Cat.class, cat.name, cat.id, GroupBy.list(Projections.constructor(Cat.class, kitten.name, kitten.id))))); for (Cat entry : result.values()) { assertEquals(1, entry.getKittens().size()); } }
@Test @ExcludeIn(DERBY) public void Transform_GroupBy2() { QCat kitten = new QCat("kitten"); Map<List<?>, Group> result = query().from(cat).innerJoin(cat.kittens, kitten) .transform(GroupBy.groupBy(cat.id, kitten.id) .as(cat, kitten)); assertFalse(result.isEmpty()); for (Tuple row : query().from(cat).innerJoin(cat.kittens, kitten) .list(cat, kitten)) { assertNotNull(result.get(Arrays.asList(row.get(cat).getId(), row.get(kitten).getId()))); } }
@Test @ExcludeIn({MYSQL, DERBY}) @NoBatooJPA public void GroupBy() { QAuthor author = QAuthor.author; QBook book = QBook.book; for (int i = 0; i < 10; i++) { Author a = new Author(); a.setName(String.valueOf(i)); save(a); for (int j = 0; j < 2; j++) { Book b = new Book(); b.setTitle(String.valueOf(i)+" "+String.valueOf(j)); b.setAuthor(a); save(b); } } Map<Long, List<Pair<Long, String>>> map = query() .from(author) .join(author.books, book) .transform(GroupBy .groupBy(author.id) .as(GroupBy.list(QPair.create(book.id, book.title)))); for (Entry<Long, List<Pair<Long, String>>> entry : map.entrySet()) { System.out.println("author = " + entry.getKey()); for (Pair<Long,String> pair : entry.getValue()) { System.out.println(" book = " + pair.getFirst() + "," + pair.getSecond()); } } }
@Test @ExcludeIn(DERBY) public void Transform_GroupBy_Alias() { QCat kitten = new QCat("kitten"); SimplePath<Cat> k = new SimplePath<Cat>(Cat.class, "k"); Map<Integer, Group> result = query().from(cat).innerJoin(cat.kittens, kitten) .transform(GroupBy.groupBy(cat.id) .as(cat.name, cat.id, GroupBy.list(Projections.constructor(Cat.class, kitten.name, kitten.id).as(k)))); for (Group entry : result.values()) { assertNotNull(entry.getOne(cat.id)); assertNotNull(entry.getOne(cat.name)); assertFalse(entry.getList(k).isEmpty()); } }
/** * Create a new aggregating map expression using a backing TreeMap * * @param key * @param value * @return */ public static <K extends Comparable<? super K>, V, U> AbstractGroupExpression<Pair<K, V>, SortedMap<K, U>> sortedMap(Expression<K> key, GroupExpression<V, U> value) { return sortedMap(new GOne<K>(key), value); }
/** * Create a new aggregating map expression using a backing LinkedHashMap * * @param key * @param value * @return */ public static <K, V, U> AbstractGroupExpression<Pair<K, V>, Map<K, U>> map(Expression<K> key, GroupExpression<V, U> value) { return map(new GOne<K>(key), value); }
/** * Create a new aggregating map expression using a backing TreeMap using the given comparator * * @param key * @param value * @param comparator * @return */ public static <K, V, U> AbstractGroupExpression<Pair<K, V>, SortedMap<K, U>> sortedMap(Expression<K> key, GroupExpression<V, U> value, Comparator<? super U> comparator) { return sortedMap(new GOne<K>(key), value, comparator); }
/** * Create a new aggregating map expression using a backing TreeMap using the given comparator * * @param key * @param value * @param comparator * @return */ public static <K, V, T> AbstractGroupExpression<Pair<K, V>, SortedMap<T, V>> sortedMap(GroupExpression<K, T> key, Expression<V> value, Comparator<? super K> comparator) { return sortedMap(key, new GOne<V>(value), comparator); }