DATAJPA-848 - Fixed AbstractPersistable's equals(…) for proxies.

AbstractPersistable.equals(…) now consides the target object's user class before comparing types.
This commit is contained in:
Oliver Gierke
2016-01-15 14:03:23 +01:00
parent ef08359f21
commit 418fbec2f7
3 changed files with 26 additions and 5 deletions

View File

@@ -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<PK extends Serializable> implements Pe
return true;
}
if (!getClass().equals(obj.getClass())) {
if (!getClass().equals(ClassUtils.getUserClass(obj))) {
return false;
}

View File

@@ -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));
}
}

View File

@@ -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<CustomAbstractPersistable, UUID> {}
public interface CustomAbstractPersistableRepository extends JpaRepository<CustomAbstractPersistable, UUID> {}