How to add custom method to Spring Data JPA

后端 未结 12 1903
盖世英雄少女心
盖世英雄少女心 2020-11-22 12:58

I am looking into Spring Data JPA. Consider the below example where I will get all the crud and finder functionality working by default and if I want to customize a finder t

相关标签:
12条回答
  • 2020-11-22 13:18

    I use SimpleJpaRepository as the base class of repository implementation and add custom method in the interface,eg:

    public interface UserRepository  {
        User FindOrInsert(int userId);
    }
    
    @Repository
    public class UserRepositoryImpl extends SimpleJpaRepository implements UserRepository {
    
        private RedisClient redisClient;
    
        public UserRepositoryImpl(RedisClient redisClient, EntityManager em) {
            super(User.class, em);
            this.redisClient = redisClient;
        }
    
    
    @Override
    public User FindOrInsert(int userId) {
    
        User u = redisClient.getOrSet("test key.. User.class, () -> {
            Optional<User> ou = this.findById(Integer.valueOf(userId));
            return ou.get();
        });
        …………
        return u;
    }
    
    0 讨论(0)
  • 2020-11-22 13:19

    There is another issue to be considered here. Some people expect that adding custom method to your repository will automatically expose them as REST services under '/search' link. This is unfortunately not the case. Spring doesn't support that currently.

    This is 'by design' feature, spring data rest explicitly checks if method is a custom method and doesn't expose it as a REST search link:

    private boolean isQueryMethodCandidate(Method method) {    
      return isQueryAnnotationPresentOn(method) || !isCustomMethod(method) && !isBaseClassMethod(method);
    }
    

    This is a qoute of Oliver Gierke:

    This is by design. Custom repository methods are no query methods as they can effectively implement any behavior. Thus, it's currently impossible for us to decide about the HTTP method to expose the method under. POST would be the safest option but that's not in line with the generic query methods (which receive GET).

    For more details see this issue: https://jira.spring.io/browse/DATAREST-206

    0 讨论(0)
  • 2020-11-22 13:20

    This is limited in usage, but for simple custom methods you can use default interface methods like:

    import demo.database.Customer;
    import org.springframework.data.repository.CrudRepository;
    
    public interface CustomerService extends CrudRepository<Customer, Long> {
    
    
        default void addSomeCustomers() {
            Customer[] customers = {
                new Customer("Józef", "Nowak", "nowakJ@o2.pl", 679856885, "Rzeszów", "Podkarpackie", "35-061", "Zamknięta 12"),
                new Customer("Adrian", "Mularczyk", "adii333@wp.pl", 867569344, "Krosno", "Podkarpackie", "32-442", "Hynka 3/16"),
                new Customer("Kazimierz", "Dejna", "sobieski22@weebly.com", 996435876, "Jarosław", "Podkarpackie", "25-122", "Korotyńskiego 11"),
                new Customer("Celina", "Dykiel", "celina.dykiel39@yahoo.org", 947845734, "Żywiec", "Śląskie", "54-333", "Polna 29")
            };
    
            for (Customer customer : customers) {
                save(customer);
            }
        }
    }
    

    EDIT:

    In this spring tutorial it is written:

    Spring Data JPA also allows you to define other query methods by simply declaring their method signature.

    So it is even possible to just declare method like:

    Customer findByHobby(Hobby personHobby);
    

    and if object Hobby is a property of Customer then Spring will automatically define method for you.

    0 讨论(0)
  • 2020-11-22 13:20

    Adding custom behavior to all repositories :

    To add custom behavior to all repositories, you first add an intermediate interface to declare the shared behavior.

    public interface MyRepository <T, ID extends Serializable> extends JpaRepository<T, ID>
    {
        
        void sharedCustomMethod( ID id );
    }
    

    Now your individual repository interfaces will extend this intermediate interface instead of the Repository interface to include the functionality declared.

    Next, create an implementation of the intermediate interface that extends the persistence technology-specific repository base class. This class will then act as a custom base class for the repository proxies.

    public class MyRepositoryImpl <T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements MyRepository<T, ID>
    {
        
        private EntityManager entityManager;
        
           // There are two constructors to choose from, either can be used.
        public MyRepositoryImpl(Class<T> domainClass, EntityManager entityManager)
        {
            super( domainClass, entityManager );
            
            // This is the recommended method for accessing inherited class dependencies.
            this.entityManager = entityManager;
        }
        
        
        public void sharedCustomMethod( ID id )
        {
            // implementation goes here
        }
    }
    

    Spring Data Repositories Part I. Reference

    0 讨论(0)
  • 2020-11-22 13:24

    I extends the SimpleJpaRepository:

    public class ExtendedRepositoryImpl<T extends EntityBean> extends SimpleJpaRepository<T, Long>
        implements ExtendedRepository<T> {
    
        private final JpaEntityInformation<T, ?> entityInformation;
    
        private final EntityManager em;
    
        public ExtendedRepositoryImpl(final JpaEntityInformation<T, ?> entityInformation,
                                                          final EntityManager entityManager) {
           super(entityInformation, entityManager);
           this.entityInformation = entityInformation;
           this.em = entityManager;
        }
    }
    

    and adds this class to @EnableJpaRepositoryries repositoryBaseClass.

    0 讨论(0)
  • 2020-11-22 13:25

    In addition to axtavt's answer, don't forget you can inject Entity Manager in your custom implementation if you need it to build your queries:

    public class AccountRepositoryImpl implements AccountRepositoryCustom {
    
        @PersistenceContext
        private EntityManager em;
    
        public void customMethod() { 
            ...
            em.createQuery(yourCriteria);
            ...
        }
    }
    
    0 讨论(0)
提交回复
热议问题