在长时间运行的对话中在 Wicket 和 Hibernate 之间安全地传递信息

发布于 2024-09-03 03:54:01 字数 564 浏览 4 评论 0 原文

我们在后台使用 Wicket 和 Hibernate。

作为用户界面的一部分,在更新的信息写回数据库之前,我们有相当长的运行对话跨越多个请求。

为了避免分离对象出现休眠错误,我们现在使用值对象将信息从服务层传输到 Wicket。

然而,我们现在最终会遇到几乎相同对象的爆炸:

例如,

  • Answer(在休眠中保存的映射实体)
  • AnswerVO(不可变值对象)
  • AnswerModel(会话域中的可变 bean)
  • IModel 包装的 Wicket 模型
  • ,通常它被包装在 CompoundPropertyModel

当对象中涉及其他对象的集合时,

这种管道会变得指数级恶化。必须有更好的方法来组织这个活动。

谁能分享一些技巧来减轻这件事的繁重?

也许使值对象可变,这样我们就可以消除 Wicket 中对单独支持 bean 的需要?

使用实体 bean,但绝对确定它们与休眠状态分离。 (说起来容易做起来难)?

还有其他技巧或模式吗?

We are using Wicket with Hibernate in the background.

As part of out UI we have quite long running conversations spanning multiple requests before the updated information is written back to the database.

To avoid getting hibernate errors with detached objects we are now using value objects to transfer info from the service layer to Wicket.

However we now end up with an explosion of almost the same objects :

e.g.

  • Answer (mapped entity saved in hibernate)
  • AnswerVO (immutable value object)
  • AnswerModel (A mutable bean in the session domain)
  • IModel wrapped Wicket Model
  • and usually this gets wrapped in a CompoundPropertyModel

This plumbing becomes exponentially worse when collections to other objects are involved in the objects.

There has to be a better way to organize this.

Can anyone share tips to make this less onerous?

Maybe make the value objects mutable so we can remove the need for a seaprate backing bean in Wicket?

Use the entity beans but absolutely make dead-certain they are detached from hibernate. (easier said than done)?

Some other tricks or patterns?

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

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

发布评论

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

评论(2

镜花水月 2024-09-10 03:54:01

使用 OpenSessionInViewFilter来自春天。这将帮助您为每个请求创建一个休眠会话。但是,它仅打开一个只读会话。要对实体 bean 执行任何写入操作,我建议启动 hibernate 事务(您可以使用 TransactionTemplate )。还有其他方法可以执行写入操作,但我发现这对我来说最简单。

现在,为了清理您的 bean,我倾向于这样做

  1. 仅拥有实体 bean
  2. 如果您想要无状态,只需将实体 bean 的密钥保存到会话中,并在多请求会话中的每个请求上重新加载 bean
  3. 即可Stateful 是一个小骗子。在将 Bean 持久保存到会话存储中之前,您需要分离实体 Bean,并且在执行后续请求时需要将其重新附加回来。当然,如果有人在后台更新了实体,您将必须在应用程序中处理它(即通知用户“数据库已被外部修改等”)。

我会选择上面的(2),因为它非常简单并且适用于大多数情况。当您的持久 bean 被两个单独的会话修改时,不建议这样做,因为您最终会覆盖以前的更新。

Hibernate 确实有一些关于处理的文档分离的对象。

Use OpenSessionInViewFilter from Spring. This will help you in creating a hibernate session per-request. However, it only opens a read-only session. To perform any write operation to your entity beans, I would recommend to start a hibernate transaction (you can use TransactionTemplate from Spring for this). There are other methods to perform a write operation, but I found this was easiest for me.

Now, to clean up your beans, here's what I tend to do

  1. Only have entity beans
  2. If you want to go stateless, only save the entity bean's key into the session and reload the bean on every single request in your multi-request conversation
  3. To remain stateful is a little tricker. You need to detach your entity bean before the beans are persisted into your session store, and you'll need to re-attach it back when the subsequent requests are performed. Naturally, if someone had updated the entity in the background, your will have to deal with it in your application (i.e. informing the user that "the database has been modifed externally, etc").

I would go with (2) above as it is pretty simple and would work on most cases. Its not recommended when your persistent beans are modified by two separate sessions as you'll end up overriding the previous updates.

Hibernate do have some documentation on dealing with detached objects.

记忆之渊 2024-09-10 03:54:01

通常的解决方案是在视图“模式”中打开会话,请参见 osiv with wicket< /a>

我对 OSIV 没有很好的经验,所以我宁愿建议在 GUI 层以下设置事务边界,并在业务或服务层中通过巧妙计划的数据检索来解决臭名昭著的lazyInitializationException

the usual solution would be the open session in view "pattern", see e.g. osiv with wicket

i have no good experiences with OSIV, so i rather advise to set transaction boundaries below the GUI layer and solve the infamous lazyInitializationException with clever planned data retrieval in business or service layer

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