Databases Reference
In-Depth Information
Creating the builder method for the repository
factory
We can create new instances of our custom repository factory class by overriding the
createRepositoryFactory()
method of the
JpaRepositoryFactoryBean
class in
the
BaseRepositoryFactoryBean
class. This method simply creates a new instance
of the
BaseRepositoryFactory
class and passes an entity manager reference as a
constructor parameter. The source code of the overridden method is given as follows:
@Override
protected RepositoryFactorySupport createRepositoryFactory(EntityManag
er entityManager) {
return new BaseRepositoryFactory(entityManager);
}
Configuring Spring Data JPA
Next we have to configure Spring Data JPA to use the custom repository factory
bean when it is creating concrete implementations of repository interfaces.
We can do this by using the
repositoryFactoryBeanClass
property of the
@EnableJpaRepositories
annotation. In other words, we have to add the following
annotation to the
ApplicationContext
class:
@EnableJpaRepositories(basePackages = {"com.packtpub.springdata.jpa.
repository"}, repositoryFactoryBeanClass = BaseRepositoryFactoryBean.
class)
If we are using XML to configure our application, we can
use the
factory-class
attribute of Spring Data JPA's
repositories
namespace element.
Creating the repository interface
We have now made the custom functionality available to all repositories. Now
we have to create a repository interface for the
Contact
entity. We can do this by
following these steps:
1.
Remove the
JpaRepository
and the
QueryDslPredicateExecutor
interfaces from the list of extended interfaces.
2.
Extend the
BaseRepository<T, ID>
interface.