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.
@ -505,15 +505,16 @@ The preceding approach is not feasible when you want to add a single method to a
@@ -505,15 +505,16 @@ The preceding approach is not feasible when you want to add a single method to a
====
[source, java]
----
@NoRepositoryBean
public interface MyRepository<T, ID extends Serializable>
extends JpaRepository<T, ID> {
extends PagingAndSortingRepository<T, ID> {
void sharedCustomMethod(ID id);
}
----
====
. Now your individual repository interfaces will extend this intermediate interface instead of the Repository interface to include the functionality declared.
. Now your individual repository interfaces will extend this intermediate interface instead of the `Repository` interface to include the functionality declared.
. 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.
+
@ -524,13 +525,12 @@ public interface MyRepository<T, ID extends Serializable>
@@ -524,13 +525,12 @@ public interface MyRepository<T, ID extends Serializable>
public class MyRepositoryImpl<T, ID extends Serializable>
// There are two constructors to choose from, either can be used.
public MyRepositoryImpl(Class<T> domainClass, EntityManager entityManager) {
super(domainClass, entityManager);
// This is the recommended method for accessing inherited class dependencies.
// Keep the EntityManager around to used from the newly introduced methods.
this.entityManager = entityManager;
}
@ -541,49 +541,45 @@ public class MyRepositoryImpl<T, ID extends Serializable>
@@ -541,49 +541,45 @@ public class MyRepositoryImpl<T, ID extends Serializable>
----
====
+
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`.
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`.
. 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.
. 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.
+
.Custom repository factory bean
====
[source, java]
----
public class MyRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable>
// The RepositoryMetadata can be safely ignored, it is used by the JpaRepositoryFactory
//to check for QueryDslJpaRepository's which is out of scope.
return MyRepository.class;
return MyRepositoryImpl.class;
}
}
}
----
====
. 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.
. 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.
+
.Using the custom factory with the namespace
====
@ -593,6 +589,15 @@ public class MyRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends
@@ -593,6 +589,15 @@ public class MyRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends