From e7505b40a5a9f2e3df20187fb67e03abf45d5b3a Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 7 Oct 2013 16:50:52 +0200 Subject: [PATCH] DATAJPA-83 - Added getOne(ID id) to JpaRepository. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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(…)). --- .../data/jpa/repository/JpaRepository.java | 11 +++++++++++ .../jpa/repository/support/SimpleJpaRepository.java | 11 +++++++++++ .../data/jpa/repository/UserRepositoryTests.java | 12 ++++++++++++ 3 files changed, 34 insertions(+) diff --git a/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java index 9bd5666a6..55cf6c3c7 100644 --- a/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java @@ -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 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); } diff --git a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index 50f430775..281f3e5d9 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -212,6 +212,17 @@ public class SimpleJpaRepository 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) diff --git a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java index 9b296ea49..d4e41bdd8 100644 --- a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java @@ -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 executeSpecWithSort(Sort sort) { flushTestUsers();