You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
DATACMNS-649 - Fixed detail in reference docs on extending all repositories.
The custom factory created to extend all repositories with additional behavior needs to return the type of the custom repository base class from getRepositoryBaseClass(…) to make sure the infrastructure can use it to inspect the CRUD methods correctly. Previously the documentations showed an interface being returned.
Code formatting in the example and inline code highlighting.
Copy file name to clipboardExpand all lines: src/main/asciidoc/repositories.adoc
+29-24Lines changed: 29 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -505,15 +505,16 @@ The preceding approach is not feasible when you want to add a single method to a
505
505
====
506
506
[source, java]
507
507
----
508
+
@NoRepositoryBean
508
509
public interface MyRepository<T, ID extends Serializable>
509
-
extends JpaRepository<T, ID> {
510
+
extends PagingAndSortingRepository<T, ID> {
510
511
511
512
void sharedCustomMethod(ID id);
512
513
}
513
514
----
514
515
====
515
516
516
-
. Now your individual repository interfaces will extend this intermediate interface instead of the Repository interface to include the functionality declared.
517
+
. Now your individual repository interfaces will extend this intermediate interface instead of the `Repository` interface to include the functionality declared.
517
518
518
519
. Next, create an implementation of the intermediate interface that extends the persistence technology-specific repository base class. This class will then act as a custom base class for the repository proxies.
519
520
+
@@ -524,13 +525,12 @@ public interface MyRepository<T, ID extends Serializable>
524
525
public class MyRepositoryImpl<T, ID extends Serializable>
// There are two constructors to choose from, either can be used.
530
530
public MyRepositoryImpl(Class<T> domainClass, EntityManager entityManager) {
531
531
super(domainClass, entityManager);
532
532
533
-
// This is the recommended method for accessing inherited class dependencies.
533
+
// Keep the EntityManager around to used from the newly introduced methods.
534
534
this.entityManager = entityManager;
535
535
}
536
536
@@ -541,49 +541,45 @@ public class MyRepositoryImpl<T, ID extends Serializable>
541
541
----
542
542
====
543
543
+
544
-
The default behavior of the Spring `<repositories />` namespace is to provide an implementation for all interfaces that fall under the `base-package`. This means that if left in its current state, an implementation instance of MyRepository will be created by Spring. This is of course not desired as it is just supposed to act as an intermediary between Repository and the actual repository interfaces you want to define for each entity. To exclude an interface that extends Repository from being instantiated as a repository instance, you can either annotate it with @NoRepositoryBean or move it outside of the configured `base-package`.
544
+
The default behavior of the Spring `<repositories />` namespace is to provide an implementation for all interfaces that fall under the `base-package`. This means that if left in its current state, an implementation instance of `MyRepository` will be created by Spring. This is of course not desired as it is just supposed to act as an intermediary between `Repository` and the actual repository interfaces you want to define for each entity. To exclude an interface that extends `Repository` from being instantiated as a repository instance, you can either annotate it with `@NoRepositoryBean` (as seen above) or move it outside of the configured `base-package`.
545
545
546
-
. Then create a custom repository factory to replace the default RepositoryFactoryBean that will in turn produce a custom RepositoryFactory. The new repository factory will then provide your MyRepositoryImpl as the implementation of any interfaces that extend the Repository interface, replacing the SimpleJpaRepository implementation you just extended.
546
+
. Then create a custom repository factory to replace the default `RepositoryFactoryBean` that will in turn produce a custom `RepositoryFactory`. The new repository factory will then provide your `MyRepositoryImpl` as the implementation of any interfaces that extend the `Repository` interface, replacing the `SimpleJpaRepository` implementation you just extended.
547
547
+
548
548
.Custom repository factory bean
549
549
====
550
550
[source, java]
551
551
----
552
-
public class MyRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable>
// The RepositoryMetadata can be safely ignored, it is used by the JpaRepositoryFactory
578
-
//to check for QueryDslJpaRepository's which is out of scope.
579
-
return MyRepository.class;
575
+
return MyRepositoryImpl.class;
580
576
}
581
577
}
582
578
}
583
579
----
584
580
====
585
581
586
-
. Finally, either declare beans of the custom factory directly or use the `factory-class` attribute of the Spring namespace to tell the repository infrastructure to use your custom factory implementation.
582
+
. Finally, either declare beans of the custom factory directly or use the `factory-class` attribute of the Spring namespace or `@Enable…` annotation to instruct the repository infrastructure to use your custom factory implementation.
587
583
+
588
584
.Using the custom factory with the namespace
589
585
====
@@ -593,6 +589,15 @@ public class MyRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends
0 commit comments