Spring Data JPA -- custom Repository

In the last one Building the Demo of the most basic JPA project On the basis of this, writing a Demo of a custom Repository is still extremely simple and process oriented.

1, Define a custom Repository (generic) interface, which inherits PagingAndSortingRepository, in order to inherit its paging and sorting related methods

@NoRepositoryBean
public interface CustomRepository<T,ID extends Serializable> extends PagingAndSortingRepository<T,ID> {
    void add(T t);
}

2, Implement this interface

public class CustomRepositoryImpl<T,ID extends Serializable> extends SimpleJpaRepository<T,ID>
        implements CustomRepository<T, ID> {

    private EntityManager entityManager;

    public CustomRepositoryImpl(Class<T> domainClass, EntityManager entityManager) {
        super(domainClass, entityManager);
        this.entityManager = entityManager;
    }

    @Override
    public void add(T t) {
        if(null != t){
            entityManager.persist(t);
            entityManager.flush();
        }
    }
}

The add() method is our custom method, which can be defined according to the actual needs. The emphasis is on the use of entitymanager. Various methods of entitymanager have been introduced on the network. One of the highlights of EntityManager is that we can choose to operate regardless of the actual type of domain object to be operated on, including reflection mechanism.

3, Define customrepository factorybean, which will register the implementation of our custom Repository

//Custom RepositoryFactoryBean, inheriting jparerepositoryfactorybean
public class CustomRepositoryFactoryBean <T extends JpaRepository<S,ID>,S,ID extends Serializable> extends JpaRepositoryFactoryBean<T,S,ID> {

    public CustomRepositoryFactoryBean(Class<? extends T> repositoryInterface) {
        super(repositoryInterface);
    }

    //Override the createRepositoryFactory method to create an instance with the current CustomRepositoryFactory
    @Override
    protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
        return new CustomRepositoryFactory(entityManager);
    }

    //Create the internal class CustomRepositoryFactory and inherit JpaRepositoryFactory
    private static class CustomRepositoryFactory <T,ID extends Serializable> extends JpaRepositoryFactory{

        public CustomRepositoryFactory(EntityManager entityManager) {
            super(entityManager);
        }

        //Override the getTargetRepository method to get the current custom Repository implementation
        @Override
        protected SimpleJpaRepository<?,?> getTargetRepository(RepositoryInformation information,EntityManager entityManager){
            return new CustomRepositoryImpl<T,ID>((Class<T>)information.getDomainType(),entityManager);
        }

        //Override getRepositoryBaseClass to get the type of the current custom Repository implementation
        @Override
        protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata){
            return CustomRepositoryImpl.class;
        }
    }
}

4, Set the repositoryFactoryBeanClass property of the enablejprarepositories annotation of JpaConfiguration to CustomRepositoryFactoryBean.class

@EnableJpaRepositories(value="com.yuxx.springboot.springdatajpademo.repository",
        repositoryFactoryBeanClass= CustomRepositoryFactoryBean.class)

5, Let the personal Repository interface implement the generic interface of CustomRepository, and then we can directly call the methods in our custom Repository through personal Repository.

public interface PersonRepository extends JpaRepository<Person,Long>,CustomRepository<Person,Long> {
}

6, Use, directly through the PersonRepository call.

@Transactional(readOnly=false)
@RequestMapping(value = "/add")
public void add(String name,Integer age,String address){
    personRepository.add(new Person(null,name,age,address));
}

Note: at present, the configuration of the project is very simple and there is no transaction. If there is no @ Transactional(readOnly=false) annotation, the following error will be reported:

7, Operation:

Tags: Programming network SpringBoot

Posted on Wed, 15 Apr 2020 08:55:01 -0700 by cosmoparty