Skip to content

Commit

Permalink
Extract a ReadOnlyRepository between Repository and CrudRepository.
Browse files Browse the repository at this point in the history
Gather read-only, i.e. "safe" operations into a separate interface, giving users a clear ability to define such repositories themselves.

Closes #2610, #2064.
  • Loading branch information
gregturn committed Apr 20, 2022
1 parent 6041952 commit c82af80
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 49 deletions.
Expand Up @@ -25,7 +25,7 @@
* @author Jens Schauder
*/
@NoRepositoryBean
public interface CrudRepository<T, ID> extends Repository<T, ID> {
public interface CrudRepository<T, ID> extends ReadOnlyRepository<T, ID> {

/**
* Saves a given entity. Use the returned instance for further operations as the save operation might have changed the
Expand All @@ -48,52 +48,6 @@ public interface CrudRepository<T, ID> extends Repository<T, ID> {
*/
<S extends T> Iterable<S> saveAll(Iterable<S> entities);

/**
* Retrieves an entity by its id.
*
* @param id must not be {@literal null}.
* @return the entity with the given id or {@literal Optional#empty()} if none found.
* @throws IllegalArgumentException if {@literal id} is {@literal null}.
*/
Optional<T> findById(ID id);

/**
* Returns whether an entity with the given id exists.
*
* @param id must not be {@literal null}.
* @return {@literal true} if an entity with the given id exists, {@literal false} otherwise.
* @throws IllegalArgumentException if {@literal id} is {@literal null}.
*/
boolean existsById(ID id);

/**
* Returns all instances of the type.
*
* @return all entities
*/
Iterable<T> findAll();

/**
* Returns all instances of the type {@code T} with the given IDs.
* <p>
* If some or all ids are not found, no entities are returned for these IDs.
* <p>
* Note that the order of elements in the result is not guaranteed.
*
* @param ids must not be {@literal null} nor contain any {@literal null} values.
* @return guaranteed to be not {@literal null}. The size can be equal or less than the number of given
* {@literal ids}.
* @throws IllegalArgumentException in case the given {@link Iterable ids} or one of its items is {@literal null}.
*/
Iterable<T> findAllById(Iterable<ID> ids);

/**
* Returns the number of entities available.
*
* @return the number of entities.
*/
long count();

/**
* Deletes the entity with the given id.
*
Expand Down
@@ -0,0 +1,74 @@
/*
* Copyright 2011-2022 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.repository;

import java.util.Optional;

/**
* Interface for read-only CRUD operations on a repository for a specific type.
*
* @author Greg Turnquist
* @since 3.0
*/
@NoRepositoryBean
public interface ReadOnlyRepository<T, ID> extends Repository<T, ID> {

/**
* Retrieves an entity by its id.
*
* @param id must not be {@literal null}.
* @return the entity with the given id or {@literal Optional#empty()} if none found.
* @throws IllegalArgumentException if {@literal id} is {@literal null}.
*/
Optional<T> findById(ID id);

/**
* Returns whether an entity with the given id exists.
*
* @param id must not be {@literal null}.
* @return {@literal true} if an entity with the given id exists, {@literal false} otherwise.
* @throws IllegalArgumentException if {@literal id} is {@literal null}.
*/
boolean existsById(ID id);

/**
* Returns all instances of the type.
*
* @return all entities
*/
Iterable<T> findAll();

/**
* Returns all instances of the type {@code T} with the given IDs.
* <p>
* If some or all ids are not found, no entities are returned for these IDs.
* <p>
* Note that the order of elements in the result is not guaranteed.
*
* @param ids must not be {@literal null} nor contain any {@literal null} values.
* @return guaranteed to be not {@literal null}. The size can be equal or less than the number of given
* {@literal ids}.
* @throws IllegalArgumentException in case the given {@link Iterable ids} or one of its items is {@literal null}.
*/
Iterable<T> findAllById(Iterable<ID> ids);

/**
* Returns the number of entities available.
*
* @return the number of entities.
*/
long count();
}
Expand Up @@ -28,6 +28,7 @@
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.ReadOnlyRepository;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.CrudMethods;
import org.springframework.data.repository.core.RepositoryInformation;
Expand Down Expand Up @@ -111,10 +112,10 @@ void detectsOverloadedMethodsCorrectly() throws Exception {
void ignoresWrongOverloadedMethods() throws Exception {

var type = RepositoryWithAllCrudMethodOverloadedWrong.class;
assertFindOneMethodOn(type, CrudRepository.class.getDeclaredMethod("findById", Object.class));
assertFindOneMethodOn(type, ReadOnlyRepository.class.getDeclaredMethod("findById", Object.class));
assertDeleteMethodOn(type, CrudRepository.class.getDeclaredMethod("delete", Object.class));
assertSaveMethodOn(type, CrudRepository.class.getDeclaredMethod("save", Object.class));
assertFindAllMethodOn(type, CrudRepository.class.getDeclaredMethod("findAll"));
assertFindAllMethodOn(type, ReadOnlyRepository.class.getDeclaredMethod("findAll"));
}

@Test // DATACMNS-464
Expand Down

0 comments on commit c82af80

Please sign in to comment.