zoukankan      html  css  js  c++  java
  • Spring Data JPA事务支持

    一 点睛

    Spring Data JPA对所有默认方法都开启了事务支持,且查询类事务默认启用readOnly=true属性。

    二 SimpleJpaRepository缩减版源码

    @Repository
    @Transactional(readOnly = true)
    public class SimpleJpaRepository<T, ID extends Serializable> implements JpaRepository<T, ID>,
            JpaSpecificationExecutor<T> {
        ......
        @Transactional
        public void delete(ID id) {
        ......
        }
     
        @Transactional
        public void delete(T entity) {
        ......
        }
     
     
        @Transactional
        public void delete(Iterable<? extends T> entities) {
        ......
        }
     
     
        @Transactional
        public void deleteInBatch(Iterable<T> entities) {
        ......
        }
     
     
        @Transactional
        public void deleteAll() {
        ......
        }
     
     
        @Transactional
        public void deleteAllInBatch() {
        ......
        }
     
     
        public T findOne(ID id) {
        ......
        }
     
     
        protected Map<String, Object> getQueryHints() {
        ......
        }
     
        private JpaEntityGraph getEntityGraph() {
        ......
        }
     
     
        @Override
        public T getOne(ID id) {
        ......
        }
     
     
        public boolean exists(ID id) {
        ......
        }
     
     
        public List<T> findAll() {
        ......
        }
     
     
        public List<T> findAll(Iterable<ID> ids) {
        ......
        }
     
     
        public List<T> findAll(Sort sort) {
        ......
        }
     
     
        public Page<T> findAll(Pageable pageable) {
        ......
        }
     
     
        public T findOne(Specification<T> spec) {
        ......
        }
     
     
        public List<T> findAll(Specification<T> spec) {
        ......
        }
     
     
        public Page<T> findAll(Specification<T> spec, Pageable pageable) {
        ......
        }
     
     
        public List<T> findAll(Specification<T> spec, Sort sort) {
        ......
        }
     
     
        public long count() {
        ......
        }
     
     
        public long count(Specification<T> spec) {
     
        ......
        }
     
     
        @Transactional
        public <S extends T> S save(S entity) {
        ......
        }
     
     
        @Transactional
        public <S extends T> S saveAndFlush(S entity) {
        ......
        }
     
        @Transactional
        public <S extends T> List<S> save(Iterable<S> entities) {
        ......
        }
     
        ......
    }

    三 源码分析

    从源码分析可以看出,SimpleJpaRepository在类级别定义了@Transactional(readOnly = true),而在和save、delete相关的操作重写了@Transactional属性,此时readOnly属性是false,其余查询操作readOnly仍然为true。

  • 相关阅读:
    python 面向对象编程的三大特征之一 多态
    python 访问控制
    python 面向对象编程的三大特征之一 继承
    朱兆祺教你如何攻破C语言学习、笔试与机试的难点
    如何画好流程图
    2013年个人计划
    30天敏捷结果(1):总体认识Getting Result敏捷方法
    每天一个linux命令目录
    国嵌C语言学习
    【head first python】1.初识python 人人都爱列表
  • 原文地址:https://www.cnblogs.com/zouhong/p/13289130.html
Copyright © 2011-2022 走看看