克服懒惰初始化异常和性能问题的困境 [英] Overcoming the dilemma of Lazy Initialization Exception and Performance Issues

查看:91
本文介绍了克服懒惰初始化异常和性能问题的困境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在hibernate中, @ManyToOne 关系几乎不会导致任何问题,因为默认获取类型是渴望的。



我们在 @OnetoMany 末端遇到了严重问题。当我们试图获取一个集合时,由于对于一对多的默认获取类型是懒惰的,所以会抛出'LazyInitializationException:无法初始化代理'。

在这种情况下,我们将设置:


  • @LazyCollection(LazyCollectionOption.FALSE),尽管它解决了这个问题,但是当有大量记录时,它会显着提高性能。

  • .HibernateException:不能同时获取多个包$ h
    $ b根据我的知识,hibernate提出了几种解决此问题的机制:


    • 选项1


      • <指定 @IndexColumn 而不是制作 LazyCollectionOption.FALSE 。作为结果,以下行为被识别出来,这些行为也会影响性能:
      • 临时表是使用各个实体的主键(@OnetoMany)创建的。所以当有很多'一对多'的关系时,临时表就会被创建。

      • >选项2




        • 将集合的数据类型从 List 更改为。这导致Struts 2在前端出现问题,因为 Set 是基于索引的集合,因为我们需要以迭代方式动态添加记录。为了解决这个问题,我们将使用DTO来捕获UI输入。通过在DTO中设置集合类型List并设置为'domain',并且通过汇编程序,我们将从 List → Set (DTO→域名),反之亦然。




        选项2下提到的决议是否可行?如果不是,请提供建议和反馈。 如果您尝试访问对象,则会抛出 LazyInitializationException 一对多属性或集合,目前没有公开会话。该关联在默认情况下设计为延迟初始化,并且这对于性能问题具有可以根据需要初始化的一对多属性的好方法。唯一的问题是会话状态。为此,Hibernate强制在会话中打开会话模式。您可以使用自定义拦截器手动实施,也可以通过完整休眠插件为Struts2 。看看这个插件如何与他们的展示中的Struts2 jQuery Grid插件一起使用。


        In hibernate, @ManyToOne relationship hardly results any problem since default fetch type is eager.

        However, we encounter critical problems at the @OnetoMany end. As we attempt to fetch a collection, 'LazyInitializationException: could not initialize proxy' is thrown since the default fetch type is lazy for one-to-many.

        In such case, we shall either set:

        • @LazyCollection(LazyCollectionOption.FALSE), which although it resolves the problem, it hits the performance significantly when there are bunch of records.

        • Fetch mode: left join but it thows org.hibernate.HibernateException: cannot simultaneously fetch multiple bags

        According to my knowledge, hibernate suggests couple of mechanisms to overcome this issue:

        • Option 1

          • Specifying @IndexColumn instead of making LazyCollectionOption.FALSE. As a consequnce, the following behaviors are identified which also impacts on the performance:

          • A temporary table is created with primary keys of respective entities (@OnetoMany). So when there are many 'one-to-many' relationships, temporary table is created for each.

        • Option 2

          • Changing data type of collection from List to Set. This is leads to a problematic situation at the front-end with Struts 2 because Set is index based collection as we need to add records dynamically in an iterative manner.

          • In order to overcome this issue, we shall make use of DTOs to capture UI inputs. By making collection type List in DTO and Set in 'domain', and by having assembler we shall do the required conversion from ListSet (DTO → Domain), and vice versa.

        Is the resolution mentioned under option 2 a viable solution? If not, please provide suggestions and feedback.

        解决方案

        The LazyInitializationException is thrown if you are trying to access objects in one-to-many properties or collections and doesn't have an open session at the moment. The association is designed by default for lazy initialization and this is good approach for performance issue to have one-to-many properties that could be initialized on demand. The only problem is with the session state. For this purpose Hibernate has imposed Open Session In View pattern. You could implement it manually using a custom interceptor or use already implemented via Full Hibernate Plugin for Struts2. See how this plugin could be used with Struts2 jQuery Grid plugin in their showcase.

        这篇关于克服懒惰初始化异常和性能问题的困境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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