Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 为什么控制器在收集流时有事务_Spring_Hibernate_Spring Boot_Transactional - Fatal编程技术网

Spring 为什么控制器在收集流时有事务

Spring 为什么控制器在收集流时有事务,spring,hibernate,spring-boot,transactional,Spring,Hibernate,Spring Boot,Transactional,我不知道为什么当我在控制器中更改实体时,它会保存在数据库中。看起来控制器有一个事务。当我在for循环中设置属性时,它不会保存在数据库中 我的弹簧控制器 @RestController public class CartController { @Autowired DeliveryTypeRepository deliveryTypeRepository; @GetMapping("/cartStepTwoAction")

我不知道为什么当我在控制器中更改实体时,它会保存在数据库中。看起来控制器有一个事务。当我在for循环中设置属性时,它不会保存在数据库中

我的弹簧控制器

  @RestController
    public class CartController {
        @Autowired
        DeliveryTypeRepository deliveryTypeRepository;

        @GetMapping("/cartStepTwoAction")
        public ModelAndView cartStepTwoAction() {
            List<DeliveryType> dtList = deliveryTypeRepository.findAll();
            dtList.stream().forEach(x -> x.setPriceBrutto(new BigDecimal(44)));
            // why dirty checking save change ?

            ...

            ModelAndView model = new ModelAndView();
            return model;
        }

    }
@RestController
公共类控制器{
@自动连线
DeliveryTypeRepository DeliveryTypeRepository;
@GetMapping(“/cartStepTwoAction”)
公共模型和视图cartStepTwoAction(){
List dtList=deliveryTypeRepository.findAll();
dtList.stream().forEach(x->x.setPriceBrutto(新的BigDecimal(44));
//为什么脏检查保存更改?
...
ModelAndView模型=新的ModelAndView();
收益模型;
}
}
,以及储存库

 @Repository
    public class DeliveryTypeRepositoryImpl implements DeliveryTypeRepository {

        @PersistenceContext
        EntityManager em;

        @Override
        public List<DeliveryType> findAll() {

                    String sql = "SELECT e FROM DeliveryType e";
                    Query query = em.createQuery(sql);
                    return query.getResultList();
        }
    }
@存储库
公共类DeliveryTypeRepositoryImpl实现DeliveryTypeRepository{
@持久上下文
实体管理器;
@凌驾
公共列表findAll(){
String sql=“从DeliveryType e中选择e”;
Query Query=em.createQuery(sql);
返回query.getResultList();
}
}

当下列条件为真时,Spring Boot将自动注册一个
OpenEntityManagerViewInterceptor

  • 您有一个web应用程序
  • 你使用JPA
在你的情况下,这两个条件都是正确的。此拦截器在整个请求期间保持实体管理器打开。自动配置发生在类
JpaBaseConfiguration

要禁用该行为,需要配置以下属性:

spring.jpa.open-in-view=false

您是否在自动配置或自定义配置中定义了
OpenEntityManagerViewFilter
?否我没有定义OpenEntityManagerViewFilter