0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

What is implementation class of org.springframework.data.jpa.repository.JpaRepository interface?

Posted at

From my notice board example application (Spring Data JPA version). You may notice that only 2 interface is defined only on repository layer

interface.png

no implementation class on these interface is defined. So what actual instance is injected into info.saladlam.example.spring.noticeboard.service.MessageServiceImpl?

That instance is a proxy, with target class instance org.springframework.data.jpa.repository.support.SimpleJpaRepository and many advice/advisor/interceptor is attached. One important interceptor is org.springframework.data.repository.core.support.RepositoryFactorySupport.QueryExecutorMethodInterceptor.

Definition of info.saladlam.example.spring.noticeboard.repository.MessageRepository

public interface MessageRepository extends JpaRepository<Message, Long> {

	@Query("SELECT m FROM Message m WHERE m.approvedBy IS NOT NULL AND m.publishDate <= :at AND (m.removeDate IS NULL OR m.removeDate > :at) ORDER BY m.publishDate DESC")
	List<Message> findPublished(@Param("at") LocalDateTime at);

	@Query("SELECT m FROM Message m WHERE m.approvedBy IS NULL ORDER BY m.publishDate DESC")
	List<Message> findWaitingApprove();

	@Query("SELECT m FROM Message m WHERE m.owner = :owner ORDER BY m.publishDate DESC")
	List<Message> findByOwner(@Param("owner") String owner);

}

and its parent interface org.springframework.data.jpa.repository.JpaRepository

@NoRepositoryBean
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {

	List<T> findAll();
	List<T> findAll(Sort sort);
	List<T> findAllById(Iterable<ID> ids);
	<S extends T> List<S> saveAll(Iterable<S> entities);
	void flush();
	<S extends T> S saveAndFlush(S entity);
	void deleteInBatch(Iterable<T> entities);
	void deleteAllInBatch();
	T getOne(ID id);

	@Override
	<S extends T> List<S> findAll(Example<S> example);

	@Override
	<S extends T> List<S> findAll(Example<S> example, Sort sort);

}

For example, when a method findAll() defined in JpaRepository is called, as result method findAll() of SimpleJpaRepository is called. And when a method findWaitingApprove() defined in MessageRepository is called, interceptor org.springframework.data.repository.core.support.RepositoryFactorySupport.QueryExecutorMethodInterceptor is involved. In method org.springframework.data.repository.core.support.RepositoryFactorySupport.QueryExecutorMethodInterceptor.doInvoke()

		@Nullable
		private Object doInvoke(MethodInvocation invocation) throws Throwable {

			Method method = invocation.getMethod();
			Object[] arguments = invocation.getArguments();

			if (hasQueryFor(method)) {
				return queries.get(method).execute(arguments);
			}

			return invocation.proceed();
		}

queries is a map, which store org.springframework.data.jpa.repository.query.JpaQueryMethod instance with key method defined in MessageRepository.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?