克服Struts 2中使用Hibernate进行延迟初始化和性能的问题 [英] Overcoming the issue of Lazy Initialization and Performance using Hibernate in Struts 2
问题描述
在Hibernate中,由于默认获取类型为 EAGER
,因此 @ManyToOne
关系几乎不会导致任何问题.
但是,我们在 @OnetoMany
端遇到了关键问题.当我们尝试获取集合时,
LazyInitializationException:无法初始化代理
因为一对多的默认提取类型为 LAZY
,所以会抛出
.
在这种情况下,我们可以设置:
-
@LazyCollection(LazyCollectionOption.FALSE)
,虽然可以解决问题,但是当有大量记录时,它会显着降低性能. -
获取
模式:左联接但会抛出org.hibernate.HibernateException:无法同时获取多个包
据我所知,Hibernate建议了一些机制来克服此问题:
-
选项1:
-
指定
@IndexColumn
,而不是创建LazyCollectionOption.FALSE
.因此,确定了以下行为也会影响性能: -
使用相应实体的主键(
@OnetoMany
)创建一个临时表.因此,当有许多一对多关系时,将为每个关系创建临时表.
-
-
选项2:
-
将集合的数据类型从
List
更改为Set
.这会导致Struts 2前端出现问题,因为Set
是基于索引的集合,因为我们需要以迭代方式动态添加记录. -
为了克服此问题,我们将使用DTO捕获UI输入.通过在DTO中创建集合类型
List
,并在 domain 中创建Set
,并使用汇编程序,我们将从List 进行所需的转换代码>→<代码>设置代码>(DTO→域),反之亦然.
-
在选项2下提到的解决方案是否可行?如果没有,请提供建议和反馈.
如果您尝试访问一对多属性中的对象或抛出 LazyInitializationException
收藏集,目前没有公开的会议.
默认情况下,该关联是为惰性初始化设计的,这对于具有一对多属性可以按需初始化的性能问题来说是个好方法./p>
唯一的问题是会话状态.为此,Hibernate施加了在视图中打开会话" 模式.
您可以使用自定义拦截器手动实现它,也可以使用已经通过展示柜中查看如何将此插件与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 throwsorg.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 makingLazyCollectionOption.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
toSet
. This is leads to a problematic situation at the front-end with Struts 2 becauseSet
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 andSet
in domain, and by having assembler we shall do the required conversion fromList
→Set
(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.
这篇关于克服Struts 2中使用Hibernate进行延迟初始化和性能的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!