Skip to content

Commit 2a029e2

Browse files
kamilkrzywanskikkrzywanski
andauthored
issue #1405 Fix serialization of subqueries (#1406)
Co-authored-by: kkrzywanski <[email protected]>
1 parent 928e4d1 commit 2a029e2

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

querydsl-libraries/querydsl-core/src/main/java/com/querydsl/core/support/FetchableQueryBase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
*/
3333
public abstract class FetchableQueryBase<T, Q extends FetchableQueryBase<T, Q>> extends QueryBase<Q>
3434
implements Fetchable<T> {
35+
protected FetchableQueryBase() {
36+
super();
37+
}
3538

3639
public FetchableQueryBase(QueryMixin<Q> queryMixin) {
3740
super(queryMixin);

querydsl-libraries/querydsl-core/src/main/java/com/querydsl/core/support/QueryBase.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public abstract class QueryBase<Q extends QueryBase<Q>> {
3434

3535
protected final QueryMixin<Q> queryMixin;
3636

37+
protected QueryBase() {
38+
this.queryMixin = null;
39+
}
40+
3741
public QueryBase(QueryMixin<Q> queryMixin) {
3842
this.queryMixin = queryMixin;
3943
}

querydsl-libraries/querydsl-jpa/src/main/java/com/querydsl/jpa/JPAQueryBase.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@
1515

1616
import com.querydsl.core.QueryMetadata;
1717
import com.querydsl.core.support.FetchableSubQueryBase;
18+
import com.querydsl.core.support.QueryBase;
1819
import com.querydsl.core.types.CollectionExpression;
1920
import com.querydsl.core.types.EntityPath;
2021
import com.querydsl.core.types.MapExpression;
2122
import com.querydsl.core.types.Path;
2223
import com.querydsl.core.types.Predicate;
2324
import com.querydsl.core.types.dsl.Expressions;
25+
import java.io.IOException;
26+
import java.io.ObjectInputStream;
27+
import java.io.ObjectOutputStream;
28+
import java.io.Serial;
29+
import java.lang.reflect.Field;
2430

2531
/**
2632
* {@code JPAQueryBase} is a base Query class for JPA queries
@@ -32,9 +38,9 @@
3238
public abstract class JPAQueryBase<T, Q extends JPAQueryBase<T, Q>>
3339
extends FetchableSubQueryBase<T, Q> implements JPQLQuery<T> {
3440

35-
protected final JPAQueryMixin<Q> queryMixin;
41+
protected final transient JPAQueryMixin<Q> queryMixin;
3642

37-
private final JPQLTemplates templates;
43+
private final transient JPQLTemplates templates;
3844

3945
@SuppressWarnings("unchecked")
4046
public JPAQueryBase(QueryMetadata md, JPQLTemplates templates) {
@@ -224,4 +230,32 @@ public String toString() {
224230

225231
@Override
226232
public abstract Q clone();
233+
234+
@Serial
235+
private void writeObject(ObjectOutputStream oos) throws IOException {
236+
oos.writeObject(getMetadata());
237+
}
238+
239+
@Serial
240+
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
241+
QueryMetadata metadata = (QueryMetadata) ois.readObject();
242+
JPAQueryMixin<Q> queryMixinValue = new JPAQueryMixin<>(metadata);
243+
@SuppressWarnings("unchecked")
244+
Q self = (Q) this;
245+
queryMixinValue.setSelf(self);
246+
try {
247+
setFinalFieldValue(JPAQueryBase.class, "templates", JPQLTemplates.DEFAULT);
248+
setFinalFieldValue(JPAQueryBase.class, "queryMixin", queryMixinValue);
249+
setFinalFieldValue(QueryBase.class, "queryMixin", queryMixinValue);
250+
} catch (NoSuchFieldException | IllegalAccessException e) {
251+
throw new RuntimeException(e);
252+
}
253+
}
254+
255+
private void setFinalFieldValue(Class<?> type, String fieldName, Object value)
256+
throws NoSuchFieldException, IllegalAccessException {
257+
Field field = type.getDeclaredField(fieldName);
258+
field.setAccessible(true);
259+
field.set(this, value);
260+
}
227261
}

querydsl-libraries/querydsl-jpa/src/test/java/com/querydsl/jpa/SubQueryTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
import static com.querydsl.jpa.JPAExpressions.selectFrom;
2020
import static com.querydsl.jpa.JPAExpressions.selectOne;
2121
import static org.assertj.core.api.Assertions.assertThat;
22+
import static org.junit.Assert.assertEquals;
2223

2324
import com.querydsl.core.domain.QCat;
25+
import com.querydsl.core.testutil.Serialization;
26+
import com.querydsl.jpa.domain.Cat;
2427
import com.querydsl.jpa.domain.QEmployee;
2528
import com.querydsl.jpa.domain.QUser;
2629
import org.junit.Test;
@@ -193,4 +196,10 @@ public void indexOf() {
193196
// query.toString().replace("\n", " "));
194197
// }
195198

199+
@Test
200+
public void subQueryShouldBeSerialized() {
201+
JPQLSubQuery<Cat> subQuery = select(cat).from(cat).where(cat.alive.isTrue());
202+
JPQLSubQuery<Cat> deserializationResult = Serialization.serialize(subQuery);
203+
assertEquals(subQuery, deserializationResult);
204+
}
196205
}

0 commit comments

Comments
 (0)