Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve spring data repository instance for given domain class?

Tags:

Given the list of all spring data repositories in some class Bar:

@Autowired private List<Repository> repositories; 

How can I find the repository for an existing domain class Foo in the above list?

Assuming that the following exists:

@Entity public class Foo {   ... } 

and

public interface FooRepository extends JpaRepository<Foo, String> {} 
like image 647
Udo Avatar asked Jan 10 '13 19:01

Udo


People also ask

How do I get Spring boot repository?

Spring boot framework provides us repository which is responsible to perform various operations on the object. To make any class repository in spring boot we have to use the repository annotation on that class, this annotation is provided by the spring framework itself.

What is the method name to fetch all data for a entity from database in repository class?

The Iterable<T> findAll() method returns all entities that are saved to the database. The T findOne(Long id) method returns the entity whose id is given as method parameter.

What is @repository in Spring boot?

Spring @Repository annotation is used to indicate that the class provides the mechanism for storage, retrieval, search, update and delete operation on objects.


2 Answers

Spring Data Commons contains a class Repositories that takes a ListableBeanFactory to find all repository beans defined in it and exposes an API to obtain these instances by domain class (through ….getRepository(Class<?> type)).

This class should be used with care. As there's some serious proxy generation going on for the repository instances you have to make sure the Repositories instance is created as late as possible during the ApplicationContext creation. The preferred way is to implement ApplicationListener and create the instance by listening to the ContextRefreshedEvent.

In case you're writing a web application, the safest way to use Repositories is by bootstrapping the repositories in the ApplicationContext created by the ContextLoaderListener and place the Repositories (see the reference documentation of Spring MVC for details.

like image 60
Oliver Drotbohm Avatar answered Oct 22 '22 02:10

Oliver Drotbohm


@Service public class GenericRepository {      @Autowired     private WebApplicationContext appContext;      Repositories repositories = null;      public GenericRepository() {         repositories = new Repositories(appContext);     }      public JpaRepository getRepository(AbstractPersistable entity) {         return (JpaRepository) repositories.getRepositoryFor(entity.getClass());     }      public Object save(AbstractPersistable entity) {         return getRepository(entity).save(entity);     }      public Object findAll(AbstractPersistable entity) {         return getRepository(entity).findAll();     }      public void delete(AbstractPersistable entity) {         getRepository(entity).delete(entity);     } } 
like image 23
Deepak Avatar answered Oct 22 '22 01:10

Deepak