JPA:何时在StackOverflowError中提前获取结果? [英] JPA: When do eager fetching result in StackOverflowError?

查看:166
本文介绍了JPA:何时在StackOverflowError中提前获取结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 我们是否应该放弃使用 FetchType.EAGER


  2. 我听说一些JPA框架(例如Hibernate)应该解决循环依赖关系。




    • 当转发框架有风险时,我们是否有一些建议?

    • c $ c> StackOverflowError 以及 FetchType.EAGER 总是关于框架中的错误(我的意思是如果我在两个表中有3行)?



解决方案




  • 方便,但速度缓慢


  • 当您的父类始终需要关联类时使用Eager

  • p>


    • 更多的编码,但更有效率

    • 基本上懒惰加载比渴望的替代(性能,资源的使用)更有益处。除非遇到某些问题,否则通常不应将其配置为提前获取。


      • 在不同的hibernate会话中共享域实例(例如,将域类实例放入http会话范围并访问属性(如用户)


      • 如果您确定,每次(或大部分时间)都会访问某个关系属性被取回的时候,配置这个关系用于提前获取也是有意义的。





    例如:关系人和地址
    在这里,它不是必需的,当我加载人体实体时,我需要一直提取地址,以便你可以保持懒惰。



    结论

    EAGER提取策略是一种代码异味。大多数情况下,为了简单起见,不考虑长期性能损失。提取策略决不应该是实体映射责任。每个业务用例具有不同的实体负载需求,因此应该将获取策略委托给每个单独的查询。
    全局提取计划只应该定义LAZY关联,这是根据每个查询获取的。结合始终检查生成的查询策略,基于查询的提取计划可以提高应用程序性能并降低维护成本。


    1. Do we have some set of rules when we should abstain of using FetchType.EAGER?

    2. I have heard that some of JPA frameworks (eg. Hibernate) should resolve cyclic dependencies.

      • Do we have some advice when it comes risky to relay on frameworks?
      • Do StackOverflowError along with FetchType.EAGER is always about bug in framework(I mean if I have like 3 rows in two tables)?

    解决方案

    EAGER:

    • Convenient, but slow

    • Use Eager when your parent class always need associate class.

    LAZY:

    • More coding, but much more efficient

    • Basically lazy loading has more benefits than the eager alternative (performance, use of resources) . you should generally not configure it for eager fetching, unless you experience certain issues.

      • Sharing a domain instance accross different hibernate sessions (eg. when putting a domain class instance into the http session scope and accessing properties from it - such as a User)

      • When you're sure, you will access a certain relation property everytime (or most of the time) when an instance is fetched, it would also make sense to configure this relation for eager fetching.

    eg: Relation Person and Address Here, its not necessary that when i load Person entity i need to fetch Address all the time so you can keep lazy .

    Conclusion

    The EAGER fetching strategy is a code smell. Most often it’s used for simplicity sake without considering the long-term performance penalties. The fetching strategy should never be the entity mapping responsibility. Each business use case has different entity load requirements and therefore the fetching strategy should be delegated to each individual query. The global fetch plan should only define LAZY associations, which are fetched on a per query basis. Combined with the always check generated queries strategy, the query based fetch plans can improve application performance and reduce maintaining costs.

    这篇关于JPA:何时在StackOverflowError中提前获取结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆