Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce getReferenceById. #2398

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.7.0-SNAPSHOT</version>
<version>2.7.0-GH-2232-SNAPSHOT</version>

<name>Spring Data JPA</name>
<description>Spring Data module for JPA repositories.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* @author Mark Paluch
* @author Sander Krabbenborg
* @author Jesse Wouters
* @author Greg Turnquist
*/
@NoRepositoryBean
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
Expand Down Expand Up @@ -96,7 +97,9 @@ public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>,
* @deprecated Use {@link #deleteAllInBatch(Iterable)} instead.
*/
@Deprecated
default void deleteInBatch(Iterable<T> entities){deleteAllInBatch(entities);}
default void deleteInBatch(Iterable<T> entities) {
deleteAllInBatch(entities);
}

/**
* Deletes the given entities in a batch which means it will create a single query. This kind of operation leaves JPAs
Expand All @@ -108,7 +111,6 @@ public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>,
*/
void deleteAllInBatch(Iterable<T> entities);


/**
* Deletes the entities identified by the given ids using a single query. This kind of operation leaves JPAs first
* level cache and the database out of sync. Consider flushing the {@link EntityManager} before calling this method.
Expand All @@ -132,7 +134,7 @@ public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>,
* @param id must not be {@literal null}.
* @return a reference to the entity with the given identifier.
* @see EntityManager#getReference(Class, Object) for details on when an exception is thrown.
* @deprecated use {@link JpaRepository#getById(ID)} instead.
* @deprecated use {@link JpaRepository#getReferenceById(ID)} instead.
*/
@Deprecated
T getOne(ID id);
Expand All @@ -146,10 +148,25 @@ public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>,
* @param id must not be {@literal null}.
* @return a reference to the entity with the given identifier.
* @see EntityManager#getReference(Class, Object) for details on when an exception is thrown.
* @deprecated use {@link JpaRepository#getReferenceById(ID)} instead.
* @since 2.5
*/
@Deprecated
T getById(ID id);

/**
* Returns a reference to the entity with the given identifier. Depending on how the JPA persistence provider is
* implemented this is very likely to always return an instance and throw an
* {@link javax.persistence.EntityNotFoundException} on first access. Some of them will reject invalid identifiers
* immediately.
*
* @param id must not be {@literal null}.
* @return a reference to the entity with the given identifier.
* @see EntityManager#getReference(Class, Object) for details on when an exception is thrown.
* @since 2.7
*/
T getReferenceById(ID id);

/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,26 +331,34 @@ protected QueryHints getQueryHints() {
@Deprecated
@Override
public T getOne(ID id) {

Assert.notNull(id, ID_MUST_NOT_BE_NULL);
return em.getReference(getDomainClass(), id);
return getReferenceById(id);
}

/*
* (non-Javadoc)
* @see org.springframework.data.jpa.repository.JpaRepository#getById(java.io.Serializable)
*/
@Deprecated
@Override
public T getById(ID id) {
return getReferenceById(id);
}

/*
* (non-Javadoc)
* @see org.springframework.data.jpa.repository.JpaRepository#getReferenceById(java.io.Serializable)
*/
@Override
public T getReferenceById(ID id) {

Assert.notNull(id, ID_MUST_NOT_BE_NULL);
return em.getReference(getDomainClass(), id);
}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#existsById(java.io.Serializable)
*/
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#existsById(java.io.Serializable)
*/
@Override
public boolean existsById(ID id) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.AbstractPersistable;
import org.springframework.data.jpa.domain.sample.CustomAbstractPersistable;
Expand All @@ -37,6 +36,7 @@
* @author Oliver Gierke
* @author Jens Schauder
* @author Jesse Wouters
* @author Greg Turnquist
*/
@Transactional
@ExtendWith(SpringExtension.class)
Expand Down Expand Up @@ -79,4 +79,16 @@ void equalsWorksForProxiedEntitiesUsingGetById() {

assertThat(proxy).isEqualTo(proxy);
}

@Test // gh-1697
void equalsWorksForProxiedEntitiesUsingGetReferenceById() {

CustomAbstractPersistable entity = repository.saveAndFlush(new CustomAbstractPersistable());

em.clear();

CustomAbstractPersistable proxy = repository.getReferenceById(entity.getId());

assertThat(proxy).isEqualTo(proxy);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,16 @@ void looksUpEntityReferenceUsingGetById() {
assertThat(result).isEqualTo(firstUser);
}

@Test // DATAJPA-415
@Test // gh-1697
void looksUpEntityReferenceUsingGetReferenceById() {

flushTestUsers();

User result = repository.getReferenceById(firstUser.getId());
assertThat(result).isEqualTo(firstUser);
}

@Test // DATAJPA-415
void invokesQueryWithVarargsParametersCorrectly() {

flushTestUsers();
Expand Down