From 418fbec2f71795215a7961b65cf462f288d14598 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 15 Jan 2016 14:03:23 +0100 Subject: [PATCH] =?UTF-8?q?DATAJPA-848=20-=20Fixed=20AbstractPersistable's?= =?UTF-8?q?=20equals(=E2=80=A6)=20for=20proxies.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AbstractPersistable.equals(…) now consides the target object's user class before comparing types. --- .../data/jpa/domain/AbstractPersistable.java | 3 ++- .../AbstractPersistableIntegrationTests.java | 21 ++++++++++++++++++- .../CustomAbstractPersistableRepository.java | 7 ++++--- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/domain/AbstractPersistable.java b/src/main/java/org/springframework/data/jpa/domain/AbstractPersistable.java index 8d82f84d7..fd6afc8c6 100644 --- a/src/main/java/org/springframework/data/jpa/domain/AbstractPersistable.java +++ b/src/main/java/org/springframework/data/jpa/domain/AbstractPersistable.java @@ -23,6 +23,7 @@ import javax.persistence.MappedSuperclass; import javax.persistence.Transient; import org.springframework.data.domain.Persistable; +import org.springframework.util.ClassUtils; /** * Abstract base class for entities. Allows parameterization of id type, chooses auto-generation and implements @@ -93,7 +94,7 @@ public abstract class AbstractPersistable implements Pe return true; } - if (!getClass().equals(obj.getClass())) { + if (!getClass().equals(ClassUtils.getUserClass(obj))) { return false; } diff --git a/src/test/java/org/springframework/data/jpa/repository/AbstractPersistableIntegrationTests.java b/src/test/java/org/springframework/data/jpa/repository/AbstractPersistableIntegrationTests.java index 3c451bfbd..79880b914 100644 --- a/src/test/java/org/springframework/data/jpa/repository/AbstractPersistableIntegrationTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/AbstractPersistableIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,8 @@ package org.springframework.data.jpa.repository; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +import javax.persistence.EntityManager; + import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -32,6 +34,7 @@ import org.springframework.transaction.annotation.Transactional; * Integration tests for {@link AbstractPersistable}. * * @author Thomas Darimont + * @author Oliver Gierke */ @Transactional @RunWith(SpringJUnit4ClassRunner.class) @@ -39,6 +42,7 @@ import org.springframework.transaction.annotation.Transactional; public class AbstractPersistableIntegrationTests { @Autowired CustomAbstractPersistableRepository repository; + @Autowired EntityManager em; /** * @see DATAJPA-622 @@ -52,4 +56,19 @@ public class AbstractPersistableIntegrationTests { assertThat(found, is(saved)); } + + /** + * @see DATAJPA-848 + */ + @Test + public void equalsWorksForProxiedEntities() { + + CustomAbstractPersistable entity = repository.saveAndFlush(new CustomAbstractPersistable()); + + em.clear(); + + CustomAbstractPersistable proxy = repository.getOne(entity.getId()); + + assertThat(proxy, is(proxy)); + } } diff --git a/src/test/java/org/springframework/data/jpa/repository/sample/CustomAbstractPersistableRepository.java b/src/test/java/org/springframework/data/jpa/repository/sample/CustomAbstractPersistableRepository.java index 819a2b0d0..fd2c875c7 100644 --- a/src/test/java/org/springframework/data/jpa/repository/sample/CustomAbstractPersistableRepository.java +++ b/src/test/java/org/springframework/data/jpa/repository/sample/CustomAbstractPersistableRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,9 +18,10 @@ package org.springframework.data.jpa.repository.sample; import java.util.UUID; import org.springframework.data.jpa.domain.sample.CustomAbstractPersistable; -import org.springframework.data.repository.CrudRepository; +import org.springframework.data.jpa.repository.JpaRepository; /** * @author Thomas Darimont + * @author Oliver Gierke */ -public interface CustomAbstractPersistableRepository extends CrudRepository {} +public interface CustomAbstractPersistableRepository extends JpaRepository {}