在没有 Spring 的情况下对抗 LazyInitializationException

发布于 2024-12-05 07:48:11 字数 370 浏览 2 评论 0 原文

我正在使用 Hibernate,并且在我的应用程序中遇到 LazyInitializationException 问题。 这个问题可以通过Spring框架(一些注释,配置什么的)简单地解决,但我只是不知道这个框架,现在不想学习它(我只是初学者)。

这个问题已经很老了,所以我发现如何用 OpenSessioninView 模式解决它,但我敢打赌有一种更简单的方法可以做到这一点。

我的应用程序正在最新的 glassfish 应用程序服务器上运行,因此我想知道在 EJB 容器中使用延迟初始化的典型方法是什么?

I'm using Hibernate and have a problem with LazyInitializationException in my app.
This problem can be simply solved by Spring framework(some annotations,config or something) but I just dont know this framework and dont want to learn it now(I'm just the beginner).

This problem is pretty old and so I found how it can be solved with OpenSessioninView pattern, but I bet there is an easer way to do it.

My app is running on latest glassfish appliation server so I want to know what the typical way to work with lazy initialization in EJB container?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时间你老了 2024-12-12 07:48:11

您可以将所有内容设置为 EAGER 并使用 session.get 而不是 session.load,但我敢打赌,在视图中打开会话是更好的方法。

顺便说一下,这正是 SPring 正在做的事情。你可以在这里看到 Spring OpenSessionInView 实现:

http://static.springsource.org/spring/docs/3.0.6.RELEASE/javadoc-api/org/springframework/orm/hibernate3/support/OpenSessionInViewFilter.html

You can set everything as EAGER and use session.get instead of session.load, but I bet that open session in view is better approach.

By the way This is exactly what SPring is doing. you can see Spring OpenSessionInView implementation here:

http://static.springsource.org/spring/docs/3.0.6.RELEASE/javadoc-api/org/springframework/orm/hibernate3/support/OpenSessionInViewFilter.html

败给现实 2024-12-12 07:48:11

这是你的问题:

“但我只是不知道这个框架,现在不想学习它(我只是初学者)。”

如果事情能自动发生就好了,我们甚至不需要编写代码,但可惜的是,事情并不总是那么美好。使用一项技术但不想付出努力/学习使用其机制是没有任何意义的。

有很多选择。

1) 在视图中打开会话。花时间来做这件事。这并不难。它的作用是在请求的生命周期内保持会话打开,因此如果您访问惰性关系,会话就在那里并且可以加载数据。 @Danny.lesnik 的答案是不正确的——从我读到的来看,他暗示 OSiV 只是让关联变得渴望,而这并不是它所做的。如果你不想使用Spring,你也许可以找到一个手动实现OSiV模式的例子,它应该不会太糟糕,并且可能是一个很好的学习经验。

2) 编写自定义映射/hql,以便加载给定场景所需的所有数据。这并不是说加载所有对象;而是说加载所有对象。您可以从表中加载某些字段,以使获取尽可能精简。例如,您可以使 hibernate/jpa 映射/注释变得惰性,然后编写一个自定义 DAO 方法,该方法仅从联接中跨表的列中获取某些字段。通过一些调整/缓存/索引,这可以非常快。

在这两个选项中,1 更容易,你只需要查看文档,它主要是配置。请注意,OSiV 有其自身的复杂性,例如,如果您修改数据但向用户呈现结果时出现异常,并且您的事务以某种方式配置,则您的修改将被回滚。选项 2 的优点是不需要 OSiV 及其所有随之而来的复杂性,并且可以让您更好地控制与数据库的交互,但代价是必须做更多的工作。

this is your problem:

"but I just dont know this framework and dont want to learn it now(I'm just the beginner)."

It would be nice if things just happened automatically, and we didn't even need to write code, but alas, things aren't always nice. It doesn't make any sense to use a technology but not want to put in the effort/learn to use its mechanisms.

There are a bunch of options.

1) Open session in view. Spend the time to do this. It's not hard. What it does is keep the session open for the life of the request, so if you access a lazy relationship, the session is there and the data can be loaded. @Danny.lesnik's answer is incorrect -- from what I read, he is implying that OSiV just makes associations eager, which is not what it does. If you don't want to use Spring, you can probably find an example of implementing the OSiV pattern by hand, it shouldn't be too bad, and is probably a good learning experience.

2) Write custom mappings/hql so you load all the data you need for a given scenario. This is not to say load all the objects; you can load certain fields off of tables to make the fetch as lean as possible. For example, you can make your hibernate/jpa mappings/annotations lazy, and then write a custom DAO method that fetches only certain fields off columns across tables in a join. That can be really fast with some tuning/caches/indexes.

Of both options, 1 is easier, you just need to look at the documentation, its mostly config. Note that OSiV has its own complications, for example, if you modify data but have an exception presenting the results to the user, and your transactions are configured a certain way, your modification will be rolled back. Option 2 has the advantage of not needing OSiV, with all of its attendant complications, and has the benefit of giving you more control of your interaction with your database, with the tradeoff of having to do more work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文