DATAJPA-464 - Add support for returning subtypes of Repository Entity in JpaRepository.saveAndFlush.

Adopted generic method declaration from save method to saveAndFlush.

Original pull request: #58.
This commit is contained in:
Thomas Darimont
2014-02-18 10:57:17 +01:00
committed by Oliver Gierke
parent 5f89cfd430
commit f5718e608c
3 changed files with 23 additions and 5 deletions

View File

@@ -67,7 +67,7 @@ public interface JpaRepository<T, ID extends Serializable> extends PagingAndSort
* @param entity
* @return the saved entity
*/
T saveAndFlush(T entity);
<S extends T> S saveAndFlush(S entity);
/**
* Deletes the given entities in a batch which means it will create a single {@link Query}. Assume that we will clear

View File

@@ -378,9 +378,9 @@ public class SimpleJpaRepository<T, ID extends Serializable> implements JpaRepos
* @see org.springframework.data.jpa.repository.JpaRepository#saveAndFlush(java.lang.Object)
*/
@Transactional
public T saveAndFlush(T entity) {
public <S extends T> S saveAndFlush(S entity) {
T result = save(entity);
S result = save(entity);
flush();
return result;

View File

@@ -55,6 +55,7 @@ import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.sample.Address;
import org.springframework.data.jpa.domain.sample.Role;
import org.springframework.data.jpa.domain.sample.SpecialUser;
import org.springframework.data.jpa.domain.sample.User;
import org.springframework.data.jpa.repository.sample.UserRepository;
import org.springframework.test.context.ContextConfiguration;
@@ -1213,7 +1214,7 @@ public class UserRepositoryTests {
}
/**
* @DATAJPA-461
* @see DATAJPA-461
*/
@Test
public void customFindByQueryWithPositionalVarargsParameters() {
@@ -1227,7 +1228,7 @@ public class UserRepositoryTests {
}
/**
* @DATAJPA-461
* @see DATAJPA-461
*/
@Test
public void customFindByQueryWithNamedVarargsParameters() {
@@ -1240,6 +1241,23 @@ public class UserRepositoryTests {
assertThat(result, hasItems(firstUser, secondUser));
}
/**
* @see DATAJPA-464
*/
@Test
public void saveAndFlushShouldSupportReturningSubTypesOfRepositoryEntity() {
repository.deleteAll();
SpecialUser user = new SpecialUser();
user.setFirstname("Thomas");
user.setEmailAddress("thomas@example.org");
SpecialUser savedUser = repository.saveAndFlush(user);
assertThat(user.getFirstname(), is(savedUser.getFirstname()));
assertThat(user.getEmailAddress(), is(savedUser.getEmailAddress()));
}
private Page<User> executeSpecWithSort(Sort sort) {
flushTestUsers();