From 52cdb3c766b574ecd9477d46ce52da96afd1e7ec Mon Sep 17 00:00:00 2001 From: Radim Vansa Date: Tue, 1 Mar 2016 15:53:42 +0100 Subject: [PATCH] HHH-10329 ClassCastException on runtime with Infinispan 8.0.1.Final * upgraded Infinispan version to 7.2.5.Final (contains fix for ISPN-5676) * removed workaround introduced in HHH-10023 * made TypeEquivalence more robust when dealing with objects that do not belong to the type --- .../cache/infinispan/TypeEquivalance.java | 23 ++++++++++++++++--- .../cache/infinispan/util/Caches.java | 20 ---------------- .../infinispan/functional/EqualityTest.java | 10 ++++++++ libraries.gradle | 2 +- 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/TypeEquivalance.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/TypeEquivalance.java index b87c92ae3d..d6a44c69c3 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/TypeEquivalance.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/TypeEquivalance.java @@ -21,12 +21,23 @@ public class TypeEquivalance implements Equivalence { @Override public int hashCode(Object o) { - return type.getHashCode(o); + if (type.getReturnedClass().isInstance(o)) { + return type.getHashCode(o); + } + else { + return o != null ? o.hashCode() : 0; + } } @Override public boolean equals(Object x, Object y) { - return type.isEqual(x, y); + Class typeClass = type.getReturnedClass(); + if (typeClass.isInstance(x) && typeClass.isInstance(y)) { + return type.isEqual(x, y); + } + else { + return (x == y) || (x != null && x.equals(y)); + } } @Override @@ -41,6 +52,12 @@ public class TypeEquivalance implements Equivalence { @Override public int compare(Object x, Object y) { - return type.compare(x, y); + Class typeClass = type.getReturnedClass(); + if (typeClass.isInstance(x) && typeClass.isInstance(y)) { + return type.compare(x, y); + } + else { + return 0; + } } } diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/util/Caches.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/util/Caches.java index 55b5c3e85c..d2ec2ce398 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/util/Caches.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/util/Caches.java @@ -306,10 +306,6 @@ public class Caches { } public static CollectableCloseableIterable keys(AdvancedCache cache, KeyValueFilter filter) { - if (cache.getCacheConfiguration().transaction().transactionMode().isTransactional()) { - // Dummy read to enlist the LocalTransaction as workaround for ISPN-5676 - cache.containsKey(false); - } // HHH-10023: we can't use keySet() final CloseableIterable> entryIterable = cache .filterEntries( filter ) @@ -322,20 +318,12 @@ public class Caches { } public static CollectableCloseableIterable values(AdvancedCache cache, KeyValueFilter filter) { - if (cache.getCacheConfiguration().transaction().transactionMode().isTransactional()) { - // Dummy read to enlist the LocalTransaction as workaround for ISPN-5676 - cache.containsKey(false); - } // HHH-10023: we can't use values() final CloseableIterable> entryIterable = cache.filterEntries(filter); return new CollectableCloseableIterableImpl(entryIterable, Selector.VALUE); } public static CollectableCloseableIterable values(AdvancedCache cache, KeyValueFilter filter, Converter converter) { - if (cache.getCacheConfiguration().transaction().transactionMode().isTransactional()) { - // Dummy read to enlist the LocalTransaction as workaround for ISPN-5676 - cache.containsKey(false); - } // HHH-10023: we can't use values() final CloseableIterable> entryIterable = cache.filterEntries(filter).converter(converter); return new CollectableCloseableIterableImpl(entryIterable, Selector.VALUE); @@ -347,20 +335,12 @@ public class Caches { } public static MapCollectableCloseableIterable entrySet(AdvancedCache cache, KeyValueFilter filter) { - if (cache.getCacheConfiguration().transaction().transactionMode().isTransactional()) { - // Dummy read to enlist the LocalTransaction as workaround for ISPN-5676 - cache.containsKey(false); - } // HHH-10023: we can't use values() final CloseableIterable> entryIterable = cache.filterEntries(filter); return new MapCollectableCloseableIterableImpl(entryIterable); } public static MapCollectableCloseableIterable entrySet(AdvancedCache cache, KeyValueFilter filter, Converter converter) { - if (cache.getCacheConfiguration().transaction().transactionMode().isTransactional()) { - // Dummy read to enlist the LocalTransaction as workaround for ISPN-5676 - cache.containsKey(false); - } // HHH-10023: we can't use values() final CloseableIterable> entryIterable = cache.filterEntries(filter).converter(converter); return new MapCollectableCloseableIterableImpl(entryIterable); diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/EqualityTest.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/EqualityTest.java index be255e8fcc..d52bbfae45 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/EqualityTest.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/EqualityTest.java @@ -3,6 +3,7 @@ package org.hibernate.test.cache.infinispan.functional; import org.hibernate.stat.Statistics; import org.hibernate.test.cache.infinispan.functional.entities.Name; import org.hibernate.test.cache.infinispan.functional.entities.Person; +import org.hibernate.testing.TestForIssue; import org.junit.Test; import java.util.List; @@ -63,4 +64,13 @@ public class EqualityTest extends SingleNodeTest { assertEquals(expected.getName().getLastName(), person.getName().getLastName()); assertEquals(expected.getAge(), person.getAge()); } + + + @TestForIssue(jiraKey = "HHH-10329") + @Test + public void testInvalidationWithEmbeddedId() throws Exception { + Person person = new Person("John", "Brown", 33); + withTxSession(s -> s.persist(person)); + withTx(() -> { cleanupCache(); return 0; }); + } } diff --git a/libraries.gradle b/libraries.gradle index 5afeac99e4..0fc9e1c6dc 100644 --- a/libraries.gradle +++ b/libraries.gradle @@ -13,7 +13,7 @@ ext { // h2Version = '1.2.145' h2Version = '1.3.176' bytemanVersion = '2.1.2' - infinispanVersion = '7.2.1.Final' + infinispanVersion = '7.2.5.Final' jnpVersion = '5.0.6.CR1' elVersion = '2.2.4'