Databases Reference
In-Depth Information
The source code of the
ContactRepository
interface is given as follows:
public interface ContactRepository extends BaseRepository<Contact,
Long> {
}
Implementing the service layer
Because the old implementation of the
delete()
method of the
RepositoryContactService
class contained the same functionality as our new
deleteById()
repository method, we have to change the
delete()
method of the
RepositoryContactService
class to delegate the method call forward to the new
repository method. The source code of our new
delete()
method is given as follows:
@Transactional(rollbackFor = NotFoundException.class)
@Override
public Contact deleteById(Long id) throws NotFoundException {
return repository.deleteById(id);
}
What did we just do?
We implemented a generic delete method that is automatically available to all
repositories of our application. This eliminates the need to add entity specific delete
logic to the service layer and reduces code duplication. We also created a custom
repository factory that provides
GenericBaseRepository
as an implementation for
our repository interface. The result of our work is illustrated in the following diagram: