jjzjj

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

coder 2024-03-05 原文

我的期望是,当在事务范围内访问集合时,应该获取延迟加载的集合。例如,如果我想获取一个集合,我可以调用 foo.getBars.size()。缺少 Activity 事务将导致异常并显示错误消息,如

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

但是,我注意到我最新的应用程序中的行为有所不同。我将 Spring Boot 1.5.1 与“data-jpa”启动器一起使用。我过去使用过 Spring Boot,但 data-jpa starter 对我来说是新的。

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

@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;
    }

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

@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 构造函数的上下文

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

与我的预期和过去的经验相反,这段代码成功执行并获取了集合。此外,如果我在我的服务方法中设置断点,我可以单步执行代码并在我的日志中查看在我调用 fooRepo 之后获取条形图的 SQL 语句。调用 fooRepo 后,我希望交易结束。

这里发生了什么?

最佳答案

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

参见 the documentation有关此(和其他)JPA 属性的引用。

关于java - Spring Data JPA - 在没有@Transactional 的情况下获取延迟加载的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42584454/

有关java - Spring Data JPA - 在没有@Transactional 的情况下获取延迟加载的集合的更多相关文章

  1. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  2. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  3. ruby - 默认情况下使选项为 false - 2

    这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb

  4. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  5. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  6. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  7. ruby - RuntimeError(自动加载常量 Apps 多线程时检测到循环依赖 - 2

    我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("

  8. ruby-on-rails - 使用 config.threadsafe 时从 lib/加载模块/类的正确方法是什么!选项? - 2

    我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co

  9. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  10. 没有类的 Ruby 方法? - 2

    大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow

随机推荐