我们在后台使用 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?
发布评论
评论(2)
使用 OpenSessionInViewFilter来自春天。这将帮助您为每个请求创建一个休眠会话。但是,它仅打开一个只读会话。要对实体 bean 执行任何写入操作,我建议启动 hibernate 事务(您可以使用 TransactionTemplate )。还有其他方法可以执行写入操作,但我发现这对我来说最简单。
现在,为了清理您的 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
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.
通常的解决方案是在视图“模式”中打开会话,请参见 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