DATAJPA-83 - Added getOne(ID id) to JpaRepository.
Added a getOne(ID id) method to both JpaRepository as well as SimpleJpaRepository to be able to obtain references to entities (as implemented by EntityManager.getReference(…)).
This commit is contained in:
@@ -18,6 +18,8 @@ package org.springframework.data.jpa.repository;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.repository.NoRepositoryBean;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
@@ -79,4 +81,13 @@ public interface JpaRepository<T, ID extends Serializable> extends PagingAndSort
|
||||
* Deletes all entites in a batch call.
|
||||
*/
|
||||
void deleteAllInBatch();
|
||||
|
||||
/**
|
||||
* Returns a reference to the entity with the given identifier.
|
||||
*
|
||||
* @param id must not be {@literal null}.
|
||||
* @return a reference to the entity with the given identifier.
|
||||
* @see EntityManager#getReference(Class, Object)
|
||||
*/
|
||||
T getOne(ID id);
|
||||
}
|
||||
|
||||
@@ -212,6 +212,17 @@ public class SimpleJpaRepository<T, ID extends Serializable> implements JpaRepos
|
||||
return type == null ? em.find(domainType, id) : em.find(domainType, id, type);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jpa.repository.JpaRepository#getOne(java.io.Serializable)
|
||||
*/
|
||||
@Override
|
||||
public T getOne(ID id) {
|
||||
|
||||
Assert.notNull(id, "The given id must not be null!");
|
||||
return em.getReference(getDomainClass(), id);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#exists(java.io.Serializable)
|
||||
|
||||
@@ -1093,6 +1093,18 @@ public class UserRepositoryTests {
|
||||
assertThat(lastname, hasItem("Dave"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-83
|
||||
*/
|
||||
@Test
|
||||
public void looksUpEntityReference() {
|
||||
|
||||
flushTestUsers();
|
||||
|
||||
User result = repository.getOne(firstUser.getId());
|
||||
assertThat(result, is(firstUser));
|
||||
}
|
||||
|
||||
private Page<User> executeSpecWithSort(Sort sort) {
|
||||
|
||||
flushTestUsers();
|
||||
|
||||
Reference in New Issue
Block a user