Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract a ReadOnlyRepository between Repository and CrudRepository.
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
Showing
3 changed files
with
78 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/java/org/springframework/data/repository/ReadOnlyRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters