Skip to content

Commit 727ab83

Browse files
committed
DATACMNS-944 - Moved to more consistent naming scheme for CrudRepository methods.
We now follow a more consistent naming scheme for the methods in repository that are driven by the following guidelines: * Methods referring to an identifier now all end on …ById(…). * Methods taking or returning a collection are named …All(…) That results in the following renames: * findOne(…) -> findById(…) * save(Iterable) -> saveAll(Iterable) * findAll(Iterable<ID>) -> findAllById(…) * delete(ID) -> deleteById(ID) * delete(Iterable<T>) -> deleteAll(Iterable<T>) * exists() -> existsById(…) As a side-effect of that, we can now drop the Serializable requirement for identifiers. Updated CRUD method detection to use the new naming scheme and moved the code to Java 8 streams and Optional. Adapted RepositoryInvoker API to reflect method name changes as well.
1 parent 382c912 commit 727ab83

File tree

55 files changed

+324
-397
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+324
-397
lines changed

src/main/java/org/springframework/data/domain/Auditable.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.domain;
1717

18-
import java.io.Serializable;
1918
import java.time.temporal.TemporalAccessor;
2019
import java.util.Optional;
2120

@@ -27,7 +26,7 @@
2726
* @param <ID> the type of the audited type's identifier
2827
* @author Oliver Gierke
2928
*/
30-
public interface Auditable<U, ID extends Serializable, T extends TemporalAccessor> extends Persistable<ID> {
29+
public interface Auditable<U, ID, T extends TemporalAccessor> extends Persistable<ID> {
3130

3231
/**
3332
* Returns the user who created this entity.

src/main/java/org/springframework/data/domain/Persistable.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@
1515
*/
1616
package org.springframework.data.domain;
1717

18-
import java.io.Serializable;
19-
2018
/**
2119
* Simple interface for entities.
2220
*
2321
* @param <ID> the type of the identifier
2422
* @author Oliver Gierke
2523
*/
26-
public interface Persistable<ID extends Serializable> extends Serializable {
24+
public interface Persistable<ID> {
2725

2826
/**
2927
* Returns the id of the entity.

src/main/java/org/springframework/data/querydsl/QuerydslRepositoryInvokerAdapter.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.querydsl;
1717

18-
import java.io.Serializable;
1918
import java.lang.reflect.Method;
2019
import java.util.Optional;
2120

@@ -112,22 +111,22 @@ public boolean hasSaveMethod() {
112111
return delegate.hasSaveMethod();
113112
}
114113

115-
/*
114+
/*
116115
* (non-Javadoc)
117-
* @see org.springframework.data.repository.support.RepositoryInvoker#invokeDelete(java.io.Serializable)
116+
* @see org.springframework.data.repository.support.RepositoryInvoker#invokeDeleteById(java.lang.Object)
118117
*/
119118
@Override
120-
public void invokeDelete(Serializable id) {
121-
delegate.invokeDelete(id);
119+
public void invokeDeleteById(Object id) {
120+
delegate.invokeDeleteById(id);
122121
}
123122

124-
/*
123+
/*
125124
* (non-Javadoc)
126-
* @see org.springframework.data.repository.support.RepositoryInvoker#invokeFindOne(java.io.Serializable)
125+
* @see org.springframework.data.repository.support.RepositoryInvoker#invokeFindById(java.lang.Object)
127126
*/
128127
@Override
129-
public <T> Optional<T> invokeFindOne(Serializable id) {
130-
return delegate.invokeFindOne(id);
128+
public <T> Optional<T> invokeFindById(Object id) {
129+
return delegate.invokeFindById(id);
131130
}
132131

133132
/*

src/main/java/org/springframework/data/repository/CrudRepository.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2011 the original author or authors.
2+
* Copyright 2008-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.repository;
1717

18-
import java.io.Serializable;
1918
import java.util.Optional;
2019

2120
/**
@@ -25,43 +24,43 @@
2524
* @author Eberhard Wolff
2625
*/
2726
@NoRepositoryBean
28-
public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
27+
public interface CrudRepository<T, ID> extends Repository<T, ID> {
2928

3029
/**
3130
* Saves a given entity. Use the returned instance for further operations as the save operation might have changed the
3231
* entity instance completely.
3332
*
34-
* @param entity
35-
* @return the saved entity
33+
* @param entity must not be {@literal null}.
34+
* @return the saved entity will never be {@literal null}.
3635
*/
3736
<S extends T> S save(S entity);
3837

3938
/**
4039
* Saves all given entities.
4140
*
42-
* @param entities
43-
* @return the saved entities
41+
* @param entities must not be {@literal null}.
42+
* @return the saved entities will never be {@literal null}.
4443
* @throws IllegalArgumentException in case the given entity is {@literal null}.
4544
*/
46-
<S extends T> Iterable<S> save(Iterable<S> entities);
45+
<S extends T> Iterable<S> saveAll(Iterable<S> entities);
4746

4847
/**
4948
* Retrieves an entity by its id.
5049
*
5150
* @param id must not be {@literal null}.
52-
* @return the entity with the given id or {@literal null} if none found
53-
* @throws IllegalArgumentException if {@code id} is {@literal null}
51+
* @return the entity with the given id or {@literal Optional#empty()} if none found
52+
* @throws IllegalArgumentException if {@code id} is {@literal null}.
5453
*/
55-
Optional<T> findOne(ID id);
54+
Optional<T> findById(ID id);
5655

5756
/**
5857
* Returns whether an entity with the given id exists.
5958
*
6059
* @param id must not be {@literal null}.
61-
* @return true if an entity with the given id exists, {@literal false} otherwise
62-
* @throws IllegalArgumentException if {@code id} is {@literal null}
60+
* @return {@literal true} if an entity with the given id exists, {@literal false} otherwise.
61+
* @throws IllegalArgumentException if {@code id} is {@literal null}.
6362
*/
64-
boolean exists(ID id);
63+
boolean existsById(ID id);
6564

6665
/**
6766
* Returns all instances of the type.
@@ -76,7 +75,7 @@ public interface CrudRepository<T, ID extends Serializable> extends Repository<T
7675
* @param ids
7776
* @return
7877
*/
79-
Iterable<T> findAll(Iterable<ID> ids);
78+
Iterable<T> findAllById(Iterable<ID> ids);
8079

8180
/**
8281
* Returns the number of entities available.
@@ -91,7 +90,7 @@ public interface CrudRepository<T, ID extends Serializable> extends Repository<T
9190
* @param id must not be {@literal null}.
9291
* @throws IllegalArgumentException in case the given {@code id} is {@literal null}
9392
*/
94-
void delete(ID id);
93+
void deleteById(ID id);
9594

9695
/**
9796
* Deletes a given entity.
@@ -107,7 +106,7 @@ public interface CrudRepository<T, ID extends Serializable> extends Repository<T
107106
* @param entities
108107
* @throws IllegalArgumentException in case the given {@link Iterable} is {@literal null}.
109108
*/
110-
void delete(Iterable<? extends T> entities);
109+
void deleteAll(Iterable<? extends T> entities);
111110

112111
/**
113112
* Deletes all entities managed by the repository.

src/main/java/org/springframework/data/repository/PagingAndSortingRepository.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.springframework.data.repository;
1717

18-
import java.io.Serializable;
19-
2018
import org.springframework.data.domain.Page;
2119
import org.springframework.data.domain.Pageable;
2220
import org.springframework.data.domain.Sort;
@@ -31,7 +29,7 @@
3129
* @see Page
3230
*/
3331
@NoRepositoryBean
34-
public interface PagingAndSortingRepository<T, ID extends Serializable> extends CrudRepository<T, ID> {
32+
public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> {
3533

3634
/**
3735
* Returns all entities sorted by the given options.

src/main/java/org/springframework/data/repository/Repository.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.springframework.data.repository;
1717

18-
import java.io.Serializable;
19-
2018
import org.springframework.stereotype.Indexed;
2119

2220
/**
@@ -33,6 +31,6 @@
3331
* @author Oliver Gierke
3432
*/
3533
@Indexed
36-
public interface Repository<T, ID extends Serializable> {
34+
public interface Repository<T, ID> {
3735

3836
}

src/main/java/org/springframework/data/repository/RepositoryDefinition.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.repository;
1717

18-
import java.io.Serializable;
1918
import java.lang.annotation.Documented;
2019
import java.lang.annotation.ElementType;
2120
import java.lang.annotation.Inherited;
@@ -53,5 +52,5 @@
5352
* @see Repository
5453
* @return
5554
*/
56-
Class<? extends Serializable> idClass();
55+
Class<?> idClass();
5756
}

src/main/java/org/springframework/data/repository/core/CrudMethods.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public interface CrudMethods {
6565

6666
/**
6767
* Returns the find one method of the repository. Usually signature compatible to
68-
* {@link CrudRepository#findOne(java.io.Serializable)}
68+
* {@link CrudRepository#findById(Object)}
6969
*
7070
* @return the find one method of the repository or {@literal null} if not available.
7171
* @see #hasFindOneMethod()

src/main/java/org/springframework/data/repository/core/EntityInformation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.repository.core;
1717

18-
import java.io.Serializable;
1918
import java.util.Optional;
2019

2120
import org.springframework.util.Assert;
@@ -25,7 +24,7 @@
2524
*
2625
* @author Oliver Gierke
2726
*/
28-
public interface EntityInformation<T, ID extends Serializable> extends EntityMetadata<T> {
27+
public interface EntityInformation<T, ID> extends EntityMetadata<T> {
2928

3029
/**
3130
* Returns whether the given entity is considered to be new.
@@ -49,6 +48,7 @@ public interface EntityInformation<T, ID extends Serializable> extends EntityMet
4948
* @param entity must not be {@literal null}.
5049
* @return the identifier of the given entity
5150
* @throws IllegalArgumentException in case no id could be obtained from the given entity
51+
* @since 2.0
5252
*/
5353
default ID getRequiredId(T entity) throws IllegalArgumentException {
5454

src/main/java/org/springframework/data/repository/core/RepositoryMetadata.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.repository.core;
1717

18-
import java.io.Serializable;
1918
import java.lang.reflect.Method;
2019
import java.util.Collection;
2120
import java.util.Set;
@@ -34,7 +33,7 @@ public interface RepositoryMetadata {
3433
*
3534
* @return the id class of the entity managed by the repository for or {@code null} if none found.
3635
*/
37-
Class<? extends Serializable> getIdType();
36+
Class<?> getIdType();
3837

3938
/**
4039
* Returns the domain class the repository is declared for.

src/main/java/org/springframework/data/repository/core/support/AbstractEntityInformation.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
*/
1616
package org.springframework.data.repository.core.support;
1717

18-
import java.io.Serializable;
18+
import lombok.NonNull;
19+
import lombok.RequiredArgsConstructor;
20+
1921
import java.util.Optional;
2022

2123
import org.springframework.data.repository.core.EntityInformation;
22-
import org.springframework.util.Assert;
2324

2425
/**
2526
* Base class for implementations of {@link EntityInformation}. Considers an entity to be new whenever
@@ -28,20 +29,10 @@
2829
* @author Oliver Gierke
2930
* @author Nick Williams
3031
*/
31-
public abstract class AbstractEntityInformation<T, ID extends Serializable> implements EntityInformation<T, ID> {
32-
33-
private final Class<T> domainClass;
32+
@RequiredArgsConstructor
33+
public abstract class AbstractEntityInformation<T, ID> implements EntityInformation<T, ID> {
3434

35-
/**
36-
* Creates a new {@link AbstractEntityInformation} from the given domain class.
37-
*
38-
* @param domainClass must not be {@literal null}.
39-
*/
40-
public AbstractEntityInformation(Class<T> domainClass) {
41-
42-
Assert.notNull(domainClass, "DomainClass must not be null!");
43-
this.domainClass = domainClass;
44-
}
35+
private final @NonNull Class<T> domainClass;
4536

4637
/*
4738
* (non-Javadoc)

src/main/java/org/springframework/data/repository/core/support/AnnotationRepositoryMetadata.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.springframework.data.repository.core.support;
1717

18-
import java.io.Serializable;
19-
2018
import org.springframework.data.repository.RepositoryDefinition;
2119
import org.springframework.data.repository.core.RepositoryMetadata;
2220
import org.springframework.util.Assert;
@@ -33,7 +31,7 @@ public class AnnotationRepositoryMetadata extends AbstractRepositoryMetadata {
3331
private static final String NO_ANNOTATION_FOUND = String.format("Interface must be annotated with @%s!",
3432
RepositoryDefinition.class.getName());
3533

36-
private final Class<? extends Serializable> idType;
34+
private final Class<?> idType;
3735
private final Class<?> domainType;
3836

3937
/**
@@ -56,7 +54,7 @@ public AnnotationRepositoryMetadata(Class<?> repositoryInterface) {
5654
* @see org.springframework.data.repository.core.RepositoryMetadata#getIdType()
5755
*/
5856
@Override
59-
public Class<? extends Serializable> getIdType() {
57+
public Class<?> getIdType() {
6058
return this.idType;
6159
}
6260

@@ -69,7 +67,7 @@ public Class<?> getDomainType() {
6967
return this.domainType;
7068
}
7169

72-
private Class<? extends Serializable> resolveIdType(Class<?> repositoryInterface) {
70+
private Class<?> resolveIdType(Class<?> repositoryInterface) {
7371

7472
RepositoryDefinition annotation = repositoryInterface.getAnnotation(RepositoryDefinition.class);
7573

0 commit comments

Comments
 (0)