使用 ExtendedPersistenceContext 和 ApplicationException 时出现问题
我正在尝试使用 ExtendedPersistenceContext 来使用 EJB 3 和 Seam 实现分离对象模式。
我还有一个业务规则引擎,当我基于数据库上的数据合并对象时,它会处理该对象。
当业务规则出现问题时,应用程序会启动标记为的异常
@ApplicationException(回滚= true)
不幸的是,根据 EJB 特定以及来自 SO 强制事务回滚 Seam 中的验证错误,该注释强制所有对象分离。
所以基本上我的对象处于与以前相同的状态(它包含用户所做的修改),但它无法使用 ExtendedPersistenceContext 解析其关系,因为它处于分离状态。
这会破坏我的所有页面,因为我有 AJAX 调用,即使在业务引擎出现故障后我也想解决这些调用。
我无法再次合并该对象,否则修改将传播到数据库上,并且如果存在 ApplicationException,我不想这样做。
如果业务验证失败,我想回滚事务,但我希望我的对象仍处于持久状态,以便它可以使用扩展持久上下文解析其关系。
有什么建议吗?
I'm trying to use a ExtendedPersistenceContext to implement the detached object pattern using EJB 3 and Seam.
I also have a business rule engine that processes my object when I merge it based on the data on the database.
When something goes wrong in the business rule, the app launches an Exception marked with
@ApplicationException(rollback = true)
Unfortunately, according to the EJB specific and this question from SO Forcing a transaction to rollback on validation errors in Seam, that annotations forces all the object to become detached.
So basically my object is in the same state as before (it contains modification made by the user) but it can't resolve its relations using the ExtendedPersistenceContext, since it's in the Detached state.
This breaks all my page, since I have AJAX calls that I want to resolve even after the failure of the Business Engine.
I can't merge the object again otherwise the modification will be propagated on the DB, and I don't want to do it if there is an ApplicationException.
I want to rollback the transaction if a business validation fail, but I want my object to be still in a persistent state so that it can resolve its relations using the extended persistence context.
Any suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要分离单个对象可以使用
entityManager.detach(object)
,否则可以使用entityManager.clear()
分离 EntityManager 的所有底层对象。您可以克隆对象以维护所做的更改和更改。防止他们在异常时回滚。更改将在克隆对象上完成然后在持久化之前将它们应用到托管对象。
如果对象已分离,则必须执行
entityManager.refresh(object)
以使其受管理&然后相应地应用克隆对象的更改。To detach a single object can use
entityManager.detach(object)
, else can useentityManager.clear()
to detach all underlying objects for a EntityManager.You can clone the objects to maintain the changes being made & prevent them from rolling back on exception. The changes are to be done on cloned object & then apply them to the managed object before persistence.
If the object is detached, then have to perform
entityManager.refresh(object)
to make it managed & then applying changes of cloned object accordingly to it.