Skip to content

Commit

Permalink
The transform method throws a ClassCastException when working with jo…
Browse files Browse the repository at this point in the history
…inFetch since 4.4.0 (#534)

* ported fix for #3264 querydsl/querydsl#3264
from https://github.com/aasyspl/querydsl

* test for regression querydsl/querydsl#3264

* Allow test to throw exceptions when needed

* Move logic for tuple creation to TupleUtils

---------

Co-authored-by: sapolinarski <[email protected]>
Co-authored-by: Marvin Froeder <[email protected]>
  • Loading branch information
3 people authored Aug 5, 2024
1 parent 01aadb8 commit 461bc56
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.querydsl.core.types.FactoryExpression;
import com.querydsl.core.types.FactoryExpressionUtils;
import com.querydsl.core.types.Projections;
import com.querydsl.core.util.TupleUtils;
import java.util.Collection;
import java.util.Objects;
import java.util.function.Supplier;
Expand Down Expand Up @@ -60,8 +61,10 @@ public RES transform(FetchableQuery<?, ?> query) {
GroupImpl group = null;
K groupId = null;
while (iter.hasNext()) {
Tuple tuple = TupleUtils.toTuple(iter.next(), expressions);
@SuppressWarnings("unchecked") // This type is mandated by the key type
K[] row = (K[]) iter.next().toArray();
K[] row = (K[]) tuple.toArray();
// end of workaround
if (group == null) {
group = new GroupImpl(groupExpressions, maps);
groupId = row[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.querydsl.core.types.FactoryExpression;
import com.querydsl.core.types.FactoryExpressionUtils;
import com.querydsl.core.types.Projections;
import com.querydsl.core.util.TupleUtils;
import java.util.Map;
import java.util.function.Supplier;

Expand Down Expand Up @@ -57,8 +58,11 @@ public RES transform(FetchableQuery<?, ?> query) {
CloseableIterator<Tuple> iter = query.select(expr).iterate();
try {
while (iter.hasNext()) {
Tuple tuple = TupleUtils.toTuple(iter.next(), expressions);

@SuppressWarnings("unchecked") // This type is mandated by the key type
K[] row = (K[]) iter.next().toArray();
K[] row = (K[]) tuple.toArray();
// end of workaround
K groupId = row[0];
GroupImpl group = (GroupImpl) groups.get(groupId);
if (group == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.querydsl.core.types.FactoryExpression;
import com.querydsl.core.types.FactoryExpressionUtils;
import com.querydsl.core.types.Projections;
import com.querydsl.core.util.TupleUtils;
import java.util.NoSuchElementException;
import java.util.Objects;

Expand Down Expand Up @@ -73,8 +74,12 @@ public V next() {
}

while (iter.hasNext()) {
// workaround from https://github.com/querydsl/querydsl/issues/3264
Tuple tuple = TupleUtils.toTuple(iter.next(), expressions);

@SuppressWarnings("unchecked") // This type is mandated by the key type
K[] row = (K[]) iter.next().toArray();
K[] row = (K[]) tuple.toArray();
// end of workaround
if (group == null) {
group = new GroupImpl(groupExpressions, maps);
groupId = row[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.querydsl.core.types.FactoryExpression;
import com.querydsl.core.types.FactoryExpressionUtils;
import com.querydsl.core.types.Projections;
import com.querydsl.core.util.TupleUtils;
import java.util.LinkedHashMap;
import java.util.Map;

Expand Down Expand Up @@ -51,8 +52,11 @@ public Map<K, V> transform(FetchableQuery<?, ?> query) {
}
try (CloseableIterator<Tuple> iter = query.select(expr).iterate()) {
while (iter.hasNext()) {
Tuple tuple = TupleUtils.toTuple(iter.next(), expressions);

@SuppressWarnings("unchecked") // This type is mandated by the key type
K[] row = (K[]) iter.next().toArray();
K[] row = (K[]) tuple.toArray();
// end of workaround
K groupId = row[0];
GroupImpl group = (GroupImpl) groups.get(groupId);
if (group == null) {
Expand Down
22 changes: 22 additions & 0 deletions querydsl-core/src/main/java/com/querydsl/core/util/TupleUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.querydsl.core.util;

import com.querydsl.core.Tuple;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.Projections;

/** TupleUtils provides tuple related utility functionality */
public final class TupleUtils {

public static Tuple toTuple(Object next, Expression<?>[] expressions) {
// workaround from https://github.com/querydsl/querydsl/issues/3264
Tuple tuple;
if (next instanceof Tuple) {
tuple = (Tuple) next;
} else if (next instanceof Object[]) {
tuple = Projections.tuple(expressions).newInstance((Object[]) next);
} else {
throw new IllegalArgumentException(String.format("Could not translate %s into tuple", next));
}
return tuple;
}
}
17 changes: 17 additions & 0 deletions querydsl-jpa/src/test/java/com/querydsl/jpa/HibernateBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
package com.querydsl.jpa;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import com.mysema.commons.lang.CloseableIterator;
import com.querydsl.core.DefaultQueryMetadata;
import com.querydsl.core.Target;
import com.querydsl.core.Tuple;
import com.querydsl.core.group.GroupBy;
import com.querydsl.core.testutil.ExcludeIn;
import com.querydsl.core.types.EntityPath;
import com.querydsl.core.types.Expression;
import com.querydsl.jpa.domain.Cat;
Expand Down Expand Up @@ -158,4 +162,17 @@ public void createQuery3() {
assertTrue(row instanceof String);
}
}

@Test
@ExcludeIn(Target.DERBY)
public void createQuery4() {
assertDoesNotThrow(
() ->
query()
.from(cat)
.leftJoin(cat.kittens)
.fetchJoin()
.distinct()
.transform(GroupBy.groupBy(cat.id).as(cat)));
}
}

0 comments on commit 461bc56

Please sign in to comment.