diff --git a/querydsl-core/src/main/java/com/mysema/query/types/expr/DslExpression.java b/querydsl-core/src/main/java/com/mysema/query/types/expr/DslExpression.java index 8541475f8..0090123c3 100644 --- a/querydsl-core/src/main/java/com/mysema/query/types/expr/DslExpression.java +++ b/querydsl-core/src/main/java/com/mysema/query/types/expr/DslExpression.java @@ -13,10 +13,11 @@ */ package com.mysema.query.types.expr; -import com.mysema.query.types.Expression; -import com.mysema.query.types.Ops; -import com.mysema.query.types.Path; -import com.mysema.query.types.PathImpl; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.lang.reflect.Field; + +import com.mysema.query.types.*; /** * DslExpression is the base class for DSL expressions, but {@link SimpleExpression} is the base class @@ -31,7 +32,7 @@ public abstract class DslExpression implements Expression { protected final Expression mixin; - protected final int hashCode; + protected transient final int hashCode; public DslExpression(Expression mixin) { this.mixin = mixin; @@ -77,4 +78,16 @@ public abstract class DslExpression implements Expression { return mixin.toString(); } + private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { + try { + ois.defaultReadObject(); + Field field = DslExpression.class.getDeclaredField("hashCode"); + field.setAccessible(true); + field.set(this, mixin.hashCode()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + } diff --git a/querydsl-jpa/src/test/java/com/mysema/query/AbstractJPATest.java b/querydsl-jpa/src/test/java/com/mysema/query/AbstractJPATest.java index 972bd3c06..45b68754c 100644 --- a/querydsl-jpa/src/test/java/com/mysema/query/AbstractJPATest.java +++ b/querydsl-jpa/src/test/java/com/mysema/query/AbstractJPATest.java @@ -283,49 +283,6 @@ public abstract class AbstractJPATest { assertEquals(1, query().from(cat).where(cat.kittens.any().name.eq("Ruth123")).count()); } - @Test - public void Any_Serialized() throws Exception { - Predicate where = cat.kittens.any().name.eq("Ruth123"); - - // serialize predicate - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream out = new ObjectOutputStream(baos); - out.writeObject(where); - out.close(); - - // deserialize predicate - ByteArrayInputStream bain = new ByteArrayInputStream(baos.toByteArray()); - ObjectInputStream in = new ObjectInputStream(bain); - Predicate where2 = (Predicate) in.readObject(); - in.close(); - - assertEquals(1, query().from(cat).where(where).count()); - assertEquals(1, query().from(cat).where(where2).count()); - } - - @Test - public void Any_Serialized2() throws Exception { - Predicate where = cat.kittens.any().name.eq("Ruth123"); - - File file = new File("target", "predicate.ser"); - if (!file.exists()) { - // serialize predicate on first run - FileOutputStream fileOutputStream = new FileOutputStream(file); - ObjectOutputStream out = new ObjectOutputStream(fileOutputStream); - out.writeObject(where); - out.close(); - assertEquals(1, query().from(cat).where(where).count()); - } else { - // deserialize predicate on second run - FileInputStream fileInputStream = new FileInputStream(file); - ObjectInputStream in = new ObjectInputStream(fileInputStream); - Predicate where2 = (Predicate) in.readObject(); - in.close(); - assertEquals(1, query().from(cat).where(where2).count()); - } - } - - @SuppressWarnings("unchecked") @Test public void ArrayProjection() { diff --git a/querydsl-jpa/src/test/java/com/mysema/query/jpa/SerializationBase.java b/querydsl-jpa/src/test/java/com/mysema/query/jpa/SerializationBase.java index e1e957606..aaf97856a 100644 --- a/querydsl-jpa/src/test/java/com/mysema/query/jpa/SerializationBase.java +++ b/querydsl-jpa/src/test/java/com/mysema/query/jpa/SerializationBase.java @@ -13,25 +13,17 @@ */ package com.mysema.query.jpa; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; - import javax.persistence.EntityManager; - -import org.junit.Test; -import org.junit.runner.RunWith; +import java.io.*; import com.mysema.query.QueryMetadata; import com.mysema.query.jpa.domain.QCat; import com.mysema.query.jpa.impl.JPAQuery; +import com.mysema.query.types.Predicate; import com.mysema.testutil.JPATestRunner; +import org.junit.Test; +import org.junit.runner.RunWith; +import static org.junit.Assert.*; @RunWith(JPATestRunner.class) public class SerializationBase { @@ -43,7 +35,7 @@ public class SerializationBase { @Test public void test() throws IOException, ClassNotFoundException{ // create query - JPAQuery query = new JPAQuery(entityManager); + JPAQuery query = query(); query.from(cat).where(cat.name.eq("Kate")).list(cat); // get metadata @@ -75,6 +67,52 @@ public class SerializationBase { query2.list(cat); } + @Test + public void Any_Serialized() throws Exception { + Predicate where = cat.kittens.any().name.eq("Ruth123"); + + // serialize predicate + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream out = new ObjectOutputStream(baos); + out.writeObject(where); + out.close(); + + // deserialize predicate + ByteArrayInputStream bain = new ByteArrayInputStream(baos.toByteArray()); + ObjectInputStream in = new ObjectInputStream(bain); + Predicate where2 = (Predicate) in.readObject(); + in.close(); + + assertEquals(1, query().from(cat).where(where).count()); + assertEquals(1, query().from(cat).where(where2).count()); + } + + @Test + public void Any_Serialized2() throws Exception { + Predicate where = cat.kittens.any().name.eq("Ruth123"); + + File file = new File("target", "predicate.ser"); + if (!file.exists()) { + // serialize predicate on first run + FileOutputStream fileOutputStream = new FileOutputStream(file); + ObjectOutputStream out = new ObjectOutputStream(fileOutputStream); + out.writeObject(where); + out.close(); + assertEquals(1, query().from(cat).where(where).count()); + } else { + // deserialize predicate on second run + FileInputStream fileInputStream = new FileInputStream(file); + ObjectInputStream in = new ObjectInputStream(fileInputStream); + Predicate where2 = (Predicate) in.readObject(); + in.close(); + assertEquals(1, query().from(cat).where(where2).count()); + } + } + + private JPAQuery query() { + return new JPAQuery(entityManager); + } + public void setEntityManager(EntityManager entityManager) { this.entityManager = entityManager; }