DATAJPA-527 - Improved handling of CrudRepository.exists(ID) entities with complex composite id.
We now delegate the exists(…) check to findOne(…) in SimpleJpaRepository for entities that have a complex composite id via @IdClass. Previously we tried to generate a string based count query in that case, which didn't work since the parameter types for the IdClass attributes didn't match the values returned by entityInformation.getCompositeIdAttributeValue(…). Polished JavaDoc in JpaEntityInformation. Original pull request: #95.
This commit is contained in:
committed by
Oliver Gierke
parent
fe7222d4ac
commit
09d32cb3f5
@@ -215,4 +215,52 @@ public class RepositoryWithCompositeKeyTests {
|
||||
assertThat(result.get(0), is(emp3));
|
||||
assertThat(result.get(1), is(emp1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-527
|
||||
*/
|
||||
@Test
|
||||
public void testExistsWithIdClass() {
|
||||
|
||||
IdClassExampleDepartment dep = new IdClassExampleDepartment();
|
||||
dep.setName("TestDepartment");
|
||||
dep.setDepartmentId(-1);
|
||||
|
||||
IdClassExampleEmployee emp = new IdClassExampleEmployee();
|
||||
emp.setDepartment(dep);
|
||||
|
||||
employeeRepositoryWithIdClass.save(emp);
|
||||
|
||||
IdClassExampleEmployeePK key = new IdClassExampleEmployeePK();
|
||||
key.setDepartment(dep.getDepartmentId());
|
||||
key.setEmpId(emp.getEmpId());
|
||||
|
||||
assertThat(employeeRepositoryWithIdClass.exists(key), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-527
|
||||
*/
|
||||
@Test
|
||||
public void testExistsWithEmbeddedId() {
|
||||
|
||||
EmbeddedIdExampleDepartment dep1 = new EmbeddedIdExampleDepartment();
|
||||
dep1.setDepartmentId(1L);
|
||||
dep1.setName("Dep1");
|
||||
|
||||
EmbeddedIdExampleEmployeePK key = new EmbeddedIdExampleEmployeePK();
|
||||
key.setDepartmentId(1L);
|
||||
key.setEmployeeId(1L);
|
||||
|
||||
EmbeddedIdExampleEmployee emp = new EmbeddedIdExampleEmployee();
|
||||
emp.setDepartment(dep1);
|
||||
emp.setEmployeePk(key);
|
||||
|
||||
emp = employeeRepositoryWithEmbeddedId.save(emp);
|
||||
|
||||
key.setDepartmentId(emp.getDepartment().getDepartmentId());
|
||||
key.setEmployeeId(emp.getEmployeePk().getEmployeeId());
|
||||
|
||||
assertThat(employeeRepositoryWithEmbeddedId.exists(key), is(true));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008-2013 the original author or authors.
|
||||
* Copyright 2008-2014 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.
|
||||
@@ -40,14 +40,14 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
* Integration test for {@link JpaRepository}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration({ "classpath:infrastructure.xml" })
|
||||
@Transactional
|
||||
public class JpaRepositoryTests {
|
||||
|
||||
@PersistenceContext
|
||||
EntityManager em;
|
||||
@PersistenceContext EntityManager em;
|
||||
|
||||
JpaRepository<SampleEntity, SampleEntityPK> repository;
|
||||
CrudRepository<SampleWithIdClass, SampleWithIdClassPK> idClassRepository;
|
||||
@@ -104,6 +104,23 @@ public class JpaRepositoryTests {
|
||||
assertThat(idClassRepository.exists(new SampleWithIdClassPK(1L, 2L)), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-527
|
||||
*/
|
||||
@Test
|
||||
public void executesExistsForEntityWithIdClass() {
|
||||
|
||||
SampleWithIdClass entity = new SampleWithIdClass(1L, 1L);
|
||||
idClassRepository.save(entity);
|
||||
|
||||
assertThat(entity.getFirst(), is(notNullValue()));
|
||||
assertThat(entity.getSecond(), is(notNullValue()));
|
||||
|
||||
SampleWithIdClassPK id = new SampleWithIdClassPK(entity.getFirst(), entity.getSecond());
|
||||
|
||||
assertThat(idClassRepository.exists(id), is(true));
|
||||
}
|
||||
|
||||
private static interface SampleEntityRepository extends JpaRepository<SampleEntity, SampleEntityPK> {
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user