Skip to content

Commit 0386ec0

Browse files
committed
DATACMNS-1188 - Reflect API changes in CrudRepository in reference documentation.
1 parent 78a874c commit 0386ec0

File tree

1 file changed

+50
-46
lines changed

1 file changed

+50
-46
lines changed

src/main/asciidoc/repositories.adoc

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ public interface CrudRepository<T, ID extends Serializable>
2626
2727
<S extends T> S save(S entity); <1>
2828
29-
T findOne(ID primaryKey); <2>
29+
Optional<T> findById(ID primaryKey); <2>
3030
3131
Iterable<T> findAll(); <3>
3232
33-
Long count(); <4>
33+
long count(); <4>
3434
3535
void delete(T entity); <5>
3636
37-
boolean exists(ID primaryKey); <6>
37+
boolean existsById(ID primaryKey); <6>
3838
3939
// … more functionality omitted.
4040
}
@@ -79,9 +79,9 @@ In addition to query methods, query derivation for both count and delete queries
7979
====
8080
[source, java]
8181
----
82-
public interface UserRepository extends CrudRepository<User, Long> {
82+
interface UserRepository extends CrudRepository<User, Long> {
8383
84-
Long countByLastname(String lastname);
84+
long countByLastname(String lastname);
8585
}
8686
----
8787
====
@@ -90,12 +90,11 @@ public interface UserRepository extends CrudRepository<User, Long> {
9090
====
9191
[source, java]
9292
----
93-
public interface UserRepository extends CrudRepository<User, Long> {
93+
interface UserRepository extends CrudRepository<User, Long> {
9494
95-
Long deleteByLastname(String lastname);
95+
long deleteByLastname(String lastname);
9696
9797
List<User> removeByLastname(String lastname);
98-
9998
}
10099
----
101100
====
@@ -164,12 +163,15 @@ Also, note that the JavaConfig variant doesn't configure a package explictly as
164163

165164
[source, java]
166165
----
167-
public class SomeClient {
166+
class SomeClient {
167+
168+
private final PersonRepository repository;
168169
169-
@Autowired
170-
private PersonRepository repository;
170+
SomeClient(PersonRepository repository) {
171+
this.repository = repository;
172+
}
171173
172-
public void doSomething() {
174+
void doSomething() {
173175
List<Person> persons = repository.findByLastname("Matthews");
174176
}
175177
}
@@ -196,9 +198,9 @@ NOTE: This allows you to define your own abstractions on top of the provided Spr
196198
@NoRepositoryBean
197199
interface MyBaseRepository<T, ID extends Serializable> extends Repository<T, ID> {
198200
199-
T findOne(ID id);
201+
Optional<T> findById(ID id);
200202
201-
T save(T entity);
203+
<S extends T> S save(S entity);
202204
}
203205
204206
interface UserRepository extends MyBaseRepository<User, Long> {
@@ -207,7 +209,7 @@ interface UserRepository extends MyBaseRepository<User, Long> {
207209
----
208210
====
209211

210-
In this first step you defined a common base interface for all your domain repositories and exposed `findOne(…)` as well as `save(…)`.These methods will be routed into the base repository implementation of the store of your choice provided by Spring Data ,e.g. in the case if JPA `SimpleJpaRepository`, because they are matching the method signatures in `CrudRepository`. So the `UserRepository` will now be able to save users, and find single ones by id, as well as triggering a query to find `Users` by their email address.
212+
In this first step you defined a common base interface for all your domain repositories and exposed `findById(…)` as well as `save(…)`.These methods will be routed into the base repository implementation of the store of your choice provided by Spring Data ,e.g. in the case if JPA `SimpleJpaRepository`, because they are matching the method signatures in `CrudRepository`. So the `UserRepository` will now be able to save users, and find single ones by id, as well as triggering a query to find `Users` by their email address.
211213

212214
NOTE: Note, that the intermediate repository interface is annotated with `@NoRepositoryBean`. Make sure you add that annotation to all repository interfaces that Spring Data should not create instances for at runtime.
213215

@@ -269,7 +271,7 @@ interface PersonRepository extends Repository<Person, Long> {
269271
}
270272
271273
@Entity
272-
public class Person {
274+
class Person {
273275
274276
}
275277
@@ -278,7 +280,7 @@ interface UserRepository extends Repository<User, Long> {
278280
}
279281
280282
@Document
281-
public class User {
283+
class User {
282284
283285
}
284286
----
@@ -299,7 +301,7 @@ interface MongoDBPersonRepository extends Repository<Person, Long> {
299301
300302
@Entity
301303
@Document
302-
public class Person {
304+
class Person {
303305
304306
}
305307
----
@@ -345,7 +347,7 @@ The query builder mechanism built into Spring Data repository infrastructure is
345347
====
346348
[source, java]
347349
----
348-
public interface PersonRepository extends Repository<User, Long> {
350+
interface PersonRepository extends Repository<User, Long> {
349351
350352
List<Person> findByEmailAddressAndLastname(EmailAddress emailAddress, String lastname);
351353
@@ -564,7 +566,7 @@ A sample configuration to enable Spring Data repositories looks something like t
564566
class ApplicationConfiguration {
565567
566568
@Bean
567-
public EntityManagerFactory entityManagerFactory() {
569+
EntityManagerFactory entityManagerFactory() {
568570
// …
569571
}
570572
}
@@ -601,7 +603,7 @@ To enrich a repository with custom functionality, you first define a fragment in
601603
[source, java]
602604
----
603605
interface CustomizedUserRepository {
604-
public void someCustomMethod(User user);
606+
void someCustomMethod(User user);
605607
}
606608
----
607609
====
@@ -788,12 +790,12 @@ The preceding approach requires customization of all repository interfaces when
788790
====
789791
[source, java]
790792
----
791-
public class MyRepositoryImpl<T, ID extends Serializable>
793+
class MyRepositoryImpl<T, ID extends Serializable>
792794
extends SimpleJpaRepository<T, ID> {
793795
794796
private final EntityManager entityManager;
795797
796-
public MyRepositoryImpl(JpaEntityInformation entityInformation,
798+
MyRepositoryImpl(JpaEntityInformation entityInformation,
797799
EntityManager entityManager) {
798800
super(entityInformation, entityManager);
799801
@@ -946,15 +948,15 @@ Several Spring Data modules offer integration with Querydsl via `QueryDslPredica
946948
----
947949
public interface QueryDslPredicateExecutor<T> {
948950
949-
T findOne(Predicate predicate); <1>
951+
Optional<T> findById(Predicate predicate); <1>
950952
951-
Iterable<T> findAll(Predicate predicate); <2>
953+
Iterable<T> findAll(Predicate predicate); <2>
952954
953-
long count(Predicate predicate); <3>
955+
long count(Predicate predicate); <3>
954956
955-
boolean exists(Predicate predicate); <4>
957+
boolean exists(Predicate predicate); <4>
956958
957-
// … more functionality omitted.
959+
// … more functionality omitted.
958960
}
959961
----
960962
<1> Finds and returns a single entity matching the `Predicate`.
@@ -999,7 +1001,7 @@ Spring Data modules ships with a variety of web support if the module supports t
9991001
@Configuration
10001002
@EnableWebMvc
10011003
@EnableSpringDataWebSupport
1002-
class WebConfiguration { }
1004+
class WebConfiguration {}
10031005
----
10041006
====
10051007

@@ -1035,10 +1037,10 @@ The `DomainClassConverter` allows you to use domain types in your Spring MVC con
10351037
----
10361038
@Controller
10371039
@RequestMapping("/users")
1038-
public class UserController {
1040+
class UserController {
10391041
10401042
@RequestMapping("/{id}")
1041-
public String showUserForm(@PathVariable("id") User user, Model model) {
1043+
String showUserForm(@PathVariable("id") User user, Model model) {
10421044
10431045
model.addAttribute("user", user);
10441046
return "userForm";
@@ -1047,7 +1049,7 @@ public class UserController {
10471049
----
10481050
====
10491051

1050-
As you can see the method receives a User instance directly and no further lookup is necessary. The instance can be resolved by letting Spring MVC convert the path variable into the id type of the domain class first and eventually access the instance through calling `findOne(…)` on the repository instance registered for the domain type.
1052+
As you can see the method receives a User instance directly and no further lookup is necessary. The instance can be resolved by letting Spring MVC convert the path variable into the id type of the domain class first and eventually access the instance through calling `findById(…)` on the repository instance registered for the domain type.
10511053

10521054
NOTE: Currently the repository has to implement `CrudRepository` to be eligible to be discovered for conversion.
10531055

@@ -1061,12 +1063,16 @@ The configuration snippet above also registers a `PageableHandlerMethodArgumentR
10611063
----
10621064
@Controller
10631065
@RequestMapping("/users")
1064-
public class UserController {
1066+
class UserController {
10651067
1066-
@Autowired UserRepository repository;
1068+
private final UserRepository repository;
1069+
1070+
UserController(UserRepository repository) {
1071+
this.repository = repository;
1072+
}
10671073
10681074
@RequestMapping
1069-
public String showUsers(Model model, Pageable pageable) {
1075+
String showUsers(Model model, Pageable pageable) {
10701076
10711077
model.addAttribute("users", repository.findAll(pageable));
10721078
return "users";
@@ -1100,7 +1106,7 @@ In case you need multiple `Pageable` or `Sort` instances to be resolved from the
11001106

11011107
[source, java]
11021108
----
1103-
public String showUsers(Model model,
1109+
String showUsers(Model model,
11041110
@Qualifier("foo") Pageable first,
11051111
@Qualifier("bar") Pageable second) { … }
11061112
----
@@ -1224,7 +1230,7 @@ interface UserRepository extends CrudRepository<User, String>,
12241230
QuerydslBinderCustomizer<QUser> { <2>
12251231
12261232
@Override
1227-
default public void customize(QuerydslBindings bindings, QUser user) {
1233+
default void customize(QuerydslBindings bindings, QUser user) {
12281234
12291235
bindings.bind(user.username).first((path, value) -> path.contains(value)) <3>
12301236
bindings.bind(String.class)
@@ -1324,21 +1330,20 @@ Given you are developing a Spring MVC web application you typically have to reso
13241330
----
13251331
@Controller
13261332
@RequestMapping("/users")
1327-
public class UserController {
1333+
class UserController {
13281334
13291335
private final UserRepository userRepository;
13301336
1331-
@Autowired
1332-
public UserController(UserRepository userRepository) {
1337+
UserController(UserRepository userRepository) {
13331338
Assert.notNull(repository, "Repository must not be null!");
13341339
this.userRepository = userRepository;
13351340
}
13361341
13371342
@RequestMapping("/{id}")
1338-
public String showUserForm(@PathVariable("id") Long id, Model model) {
1343+
String showUserForm(@PathVariable("id") Long id, Model model) {
13391344
13401345
// Do null check for id
1341-
User user = userRepository.findOne(id);
1346+
User user = userRepository.findById(id);
13421347
// Do null check for user
13431348
13441349
model.addAttribute("user", user);
@@ -1347,7 +1352,7 @@ public class UserController {
13471352
}
13481353
----
13491354

1350-
First you declare a repository dependency for each controller to look up the entity managed by the controller or repository respectively. Looking up the entity is boilerplate as well, as it's always a `findOne(…)` call. Fortunately Spring provides means to register custom components that allow conversion between a `String` value to an arbitrary type.
1355+
First you declare a repository dependency for each controller to look up the entity managed by the controller or repository respectively. Looking up the entity is boilerplate as well, as it's always a `findById(…)` call. Fortunately Spring provides means to register custom components that allow conversion between a `String` value to an arbitrary type.
13511356

13521357
[[web.legacy.property-editors]]
13531358
===== PropertyEditors
@@ -1373,14 +1378,13 @@ If you have configured Spring MVC as in the preceding example, you can configure
13731378
----
13741379
@Controller
13751380
@RequestMapping("/users")
1376-
public class UserController {
1381+
class UserController {
13771382
13781383
@RequestMapping("/{id}")
1379-
public String showUserForm(@PathVariable("id") User user, Model model) {
1384+
String showUserForm(@PathVariable("id") User user, Model model) {
13801385
13811386
model.addAttribute("user", user);
13821387
return "userForm";
13831388
}
13841389
}
13851390
----
1386-

0 commit comments

Comments
 (0)