Skip to content

Commit 88d344d

Browse files
committed
DATAJPA-848 - Fixed AbstractPersistable's equals(…) for proxies.
AbstractPersistable.equals(…) now consides the target object's user class before comparing types.
1 parent c18b5fe commit 88d344d

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

src/main/java/org/springframework/data/jpa/domain/AbstractPersistable.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import javax.persistence.Transient;
2424

2525
import org.springframework.data.domain.Persistable;
26+
import org.springframework.util.ClassUtils;
2627

2728
/**
2829
* Abstract base class for entities. Allows parameterization of id type, chooses auto-generation and implements
@@ -93,7 +94,7 @@ public boolean equals(Object obj) {
9394
return true;
9495
}
9596

96-
if (!getClass().equals(obj.getClass())) {
97+
if (!getClass().equals(ClassUtils.getUserClass(obj))) {
9798
return false;
9899
}
99100

src/test/java/org/springframework/data/jpa/repository/AbstractPersistableIntegrationTests.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,8 @@
1818
import static org.hamcrest.Matchers.*;
1919
import static org.junit.Assert.*;
2020

21+
import javax.persistence.EntityManager;
22+
2123
import org.junit.Test;
2224
import org.junit.runner.RunWith;
2325
import org.springframework.beans.factory.annotation.Autowired;
@@ -32,13 +34,15 @@
3234
* Integration tests for {@link AbstractPersistable}.
3335
*
3436
* @author Thomas Darimont
37+
* @author Oliver Gierke
3538
*/
3639
@Transactional
3740
@RunWith(SpringJUnit4ClassRunner.class)
3841
@ContextConfiguration(locations = { "classpath:config/namespace-autoconfig-context.xml" })
3942
public class AbstractPersistableIntegrationTests {
4043

4144
@Autowired CustomAbstractPersistableRepository repository;
45+
@Autowired EntityManager em;
4246

4347
/**
4448
* @see DATAJPA-622
@@ -52,4 +56,19 @@ public void shouldBeAbleToSaveAndLoadCustomPersistableWithUuidId() {
5256

5357
assertThat(found, is(saved));
5458
}
59+
60+
/**
61+
* @see DATAJPA-848
62+
*/
63+
@Test
64+
public void equalsWorksForProxiedEntities() {
65+
66+
CustomAbstractPersistable entity = repository.saveAndFlush(new CustomAbstractPersistable());
67+
68+
em.clear();
69+
70+
CustomAbstractPersistable proxy = repository.getOne(entity.getId());
71+
72+
assertThat(proxy, is(proxy));
73+
}
5574
}

src/test/java/org/springframework/data/jpa/repository/sample/CustomAbstractPersistableRepository.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,9 +18,10 @@
1818
import java.util.UUID;
1919

2020
import org.springframework.data.jpa.domain.sample.CustomAbstractPersistable;
21-
import org.springframework.data.repository.CrudRepository;
21+
import org.springframework.data.jpa.repository.JpaRepository;
2222

2323
/**
2424
* @author Thomas Darimont
25+
* @author Oliver Gierke
2526
*/
26-
public interface CustomAbstractPersistableRepository extends CrudRepository<CustomAbstractPersistable, UUID> {}
27+
public interface CustomAbstractPersistableRepository extends JpaRepository<CustomAbstractPersistable, UUID> {}

0 commit comments

Comments
 (0)