Generic Repository in Spring JPA

前端 未结 2 851
感情败类
感情败类 2021-01-24 05:56

We are working on a Restful project with lots of DB tables. Though the operations on the tables are almost same and mainly INSERT/UPDATE/DELETE/FETCH.

my questions is:

2条回答
  •  自闭症患者
    2021-01-24 06:27

    For insert/update/delete operations such repository may be as simple as:

    @Component
    public class CommonRepository {
    
      @PersistenceContext
      EntityManager em;
    
      @Transactional
      public  E insert(E entity) {
         em.persist(entity);
         return entity;
      }
    
      @Transactional
      public  E update(E entity) {
         return em.merge(entity);
      }
    
      @Transactional
      public void delete(Object entity) {
         em.remove(entity);
      }
    
    }
    

    For more accurate code, refer SimpleJpaRepository implementation

提交回复
热议问题