Creating a read-only repository with SpringData

后端 未结 6 452
后悔当初
后悔当初 2020-12-13 18:12

Is it possible to create read-only repositories using Spring Data?

I have some entities linked to views and some child entities for which I would like to provide a r

6条回答
  •  青春惊慌失措
    2020-12-13 19:06

    Yes, the way to go is to add a handcrafted base repository. You usually use something like this:

    public interface ReadOnlyRepository extends Repository {
    
      T findOne(ID id);
    
      Iterable findAll();
    }
    

    You can now have you concrete repos extend that just defined one:

    public interface PersonRepository extends ReadOnlyRepository {
    
      T findByEmailAddress(String emailAddress);
    }
    

    The crucial part defining the base repo is that the method declarations carry the very same signature as the methods declared in CrudRepository if that's the case we can still route the calls into the implementation bean backing the repository proxy. I've written a more detailed blog post about that topic in the SpringSource blog.

提交回复
热议问题