<i id='Apw95'><tr id='Apw95'><dt id='Apw95'><q id='Apw95'><span id='Apw95'><b id='Apw95'><form id='Apw95'><ins id='Apw95'></ins><ul id='Apw95'></ul><sub id='Apw95'></sub></form><legend id='Apw95'></legend><bdo id='Apw95'><pre id='Apw95'><center id='Apw95'></center></pre></bdo></b><th id='Apw95'></th></span></q></dt></tr></i><div id='Apw95'><tfoot id='Apw95'></tfoot><dl id='Apw95'><fieldset id='Apw95'></fieldset></dl></div>
        <bdo id='Apw95'></bdo><ul id='Apw95'></ul>
      <legend id='Apw95'><style id='Apw95'><dir id='Apw95'><q id='Apw95'></q></dir></style></legend>
    1. <tfoot id='Apw95'></tfoot>

        <small id='Apw95'></small><noframes id='Apw95'>

      1. Spring Data JPA - 在没有 @Transactional 的情况下获取延迟加载的集合

        时间:2024-08-23

            <tbody id='uOiJD'></tbody>
          <i id='uOiJD'><tr id='uOiJD'><dt id='uOiJD'><q id='uOiJD'><span id='uOiJD'><b id='uOiJD'><form id='uOiJD'><ins id='uOiJD'></ins><ul id='uOiJD'></ul><sub id='uOiJD'></sub></form><legend id='uOiJD'></legend><bdo id='uOiJD'><pre id='uOiJD'><center id='uOiJD'></center></pre></bdo></b><th id='uOiJD'></th></span></q></dt></tr></i><div id='uOiJD'><tfoot id='uOiJD'></tfoot><dl id='uOiJD'><fieldset id='uOiJD'></fieldset></dl></div>
        1. <small id='uOiJD'></small><noframes id='uOiJD'>

          <legend id='uOiJD'><style id='uOiJD'><dir id='uOiJD'><q id='uOiJD'></q></dir></style></legend>
                <bdo id='uOiJD'></bdo><ul id='uOiJD'></ul>
              • <tfoot id='uOiJD'></tfoot>

                1. 本文介绍了Spring Data JPA - 在没有 @Transactional 的情况下获取延迟加载的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的期望是在事务范围内访问集合时应该获取延迟加载的集合.例如,如果我想获取一个集合,我可以调用 foo.getBars.size().没有活动的事务应该会导致异常,并带有类似的错误消息

                  My expectation is that a lazy loaded collection should be fetched when the collection is accessed within a transactional scope. For example, if I want to fetch a collection I can call foo.getBars.size(). The absence of an active transaction should result in an exception with an error message like

                  未能延迟初始化条形集合:....无法初始化代理 - 没有会话

                  failed to lazily initialize a collection of bars: .... could not initialize proxy - no Session

                  但是,我注意到我的最新应用程序中的行为有所不同.我正在使用带有data-jpa"启动器的 Spring Boot 1.5.1.我过去使用过 Spring Boot,但 data-jpa 启动器对我来说是新的.

                  However, I noticed that the behavior is different in my latest application. I'm using Spring Boot 1.5.1 with the "data-jpa" starter. I have used Spring Boot in the past, but the data-jpa starter is new for me.

                  考虑以下情况.我有一个延迟加载的 ManyToMany 集合.

                  Consider the following case. I have a lazy loaded ManyToMany collection.

                  @SuppressWarnings("serial")
                  @Entity
                  @Table(name = "foo")
                  public class Foo implements java.io.Serializable {
                      ....
                      private Set<Bar> bars = new HashSet<Bar>(0);
                      ....
                  
                      @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
                      @JoinTable(name = "foo_bar_map",
                          joinColumns = {@JoinColumn(name = "foo_id", nullable = false, updatable = false)},
                          inverseJoinColumns = {@JoinColumn(name = "bar_id", nullable = false, updatable = false)})
                      public Set<Bar> getBars() {
                          return this.bars;
                      }
                  
                      public void setBar(Set<Bar> bars) {
                          this.bars = bars;
                      }
                  

                  我的服务方法未标记为事务性,但我正在访问延迟加载的集合

                  My service method is NOT marked as Transactional but I am accessing a lazy loaded collection

                  @Service
                  public class FooServiceImpl implements FooService {
                  
                      @Autowired
                      private FooRepository fooRepo;
                  
                  
                      @Override
                      public FooDTO findById(int fooId) {
                          Foo foo = fooRepo.findOne(fooId);
                          // The FooDTO constructor will access foo.getBars()  
                          return new FooDTO(foo);
                      }
                  

                  对于 FooDTO 构造函数的上下文

                  And for context on the FooDTO constructor

                  public FooDTO(Foo foo) {
                      ...
                      for (Bar bar : foo.getBars()) {
                          this.bars.add(bar);
                      }
                  }
                  

                  与我的期望和过去的经验相反,此代码成功执行并获取集合.此外,如果我在我的服务方法中抛出一个断点,我可以单步执行代码并查看我的日志中的 SQL 语句,这些语句在我调用 fooRepo 之后获取条形图.在我调用 fooRepo 之后,我希望交易会被关闭.

                  Contrary to my expectation and past experience, this code executes successfully and fetches the collection. Further, if I throw a breakpoint in my service method, I can step through the code and see the SQL statements in my logs that fetch the bars after my call to the fooRepo. After my call to fooRepo, I expect the transaction to be closed.

                  这里发生了什么?

                  推荐答案

                  Spring Boot 默认使用 OpenEntityManagerInView 拦截器.您可以通过将属性 spring.jpa.open-in-view 设置为 false 来关闭它.

                  Spring Boot uses an OpenEntityManagerInView interceptor by default. You can turn it off by setting the property spring.jpa.open-in-view to false.

                  参见文档 有关此(和其他)JPA 属性的参考.

                  See the documentation for the reference about this (and other) JPA properties.

                  这篇关于Spring Data JPA - 在没有 @Transactional 的情况下获取延迟加载的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Spring Data MongoDB 中使用乐观锁定? 下一篇:如何保持“in"中提供的订单Spring Data JPA 或 Hibernate 中的子句

                  相关文章

                2. <tfoot id='0slsW'></tfoot>
                  • <bdo id='0slsW'></bdo><ul id='0slsW'></ul>
                3. <small id='0slsW'></small><noframes id='0slsW'>

                  <i id='0slsW'><tr id='0slsW'><dt id='0slsW'><q id='0slsW'><span id='0slsW'><b id='0slsW'><form id='0slsW'><ins id='0slsW'></ins><ul id='0slsW'></ul><sub id='0slsW'></sub></form><legend id='0slsW'></legend><bdo id='0slsW'><pre id='0slsW'><center id='0slsW'></center></pre></bdo></b><th id='0slsW'></th></span></q></dt></tr></i><div id='0slsW'><tfoot id='0slsW'></tfoot><dl id='0slsW'><fieldset id='0slsW'></fieldset></dl></div>
                  <legend id='0slsW'><style id='0slsW'><dir id='0slsW'><q id='0slsW'></q></dir></style></legend>