java - JSF2 : Open Session in View with EJBs?

标签 java architecture jsf ejb-3.0 jsf-2

在 JSF2 应用程序中讨论 Open Session In View Pattern 有意义吗? 我的应用程序具有调用业务服务 EJB 的 JSF2 托管 Bean,这些 EJB 执行所有与数据库相关的工作(有一个 DAO 层,但现在这并不重要)。

拥有 OSIV 模式意味着托管 Bean 必须以某种方式确保底层 session 已打开。

我也在使用 JPA。

最佳答案

从理论上讲,问题是完全相同的:实体在离开 EJB 时将变得分离,除非有什么东西使 EntityManager 的范围保持打开状态。 (这里有一篇关于该主题的精彩帖子:JPA implementation patterns: Lazy loading)。

来自blog post我阅读:

8) No Open Entity Manager In View support. [...] In EJB3, when your entity leaves bean with transaction scoped EntityManager, it is detached from persistence context and you may no longer rely on lazy loading (in fact, JPA specification does not specify the behavior in such situation, probably some vendor dependent exception will be thrown...) Of course, you may use EntityManager with extended persistence context, holding the transaction and persistence context as long as you want. But this feature is only available for SFSB, while DAO classes are typical examples of stateless services, since they only dispatch calls to the persistence layer. Additionally, having dedicated DAO bean instance for each client seems to be a big overkill.

不过我不确定这是真的。根据我的理解,您应该能够编写一个使用 UserTransaction 的 servlet 过滤器。开始并提交事务(就像 OSIV 中的常规过滤器)。然后 EJB 将参与在过滤器中启动的事务,并且 EntityManager 将保持打开状态。虽然我还没有测试过,但我的建议是试一试。

关于java - JSF2 : Open Session in View with EJBs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3151727/

相关文章:

jsf - PrimeFaces升级到5.1后java.io.NotSerializableException : org. primefaces.component.datatable.DataTable

java - h :commandbutton, 如何重定向到外部站点?(JSF 2)

java - 重新启用长按 map

java - XMLRPC java 插入问题

c# - 使用Mono获取Unix/Linux系统架构

architecture - Web 服务应该允许用户创建和更新,还是仅仅保存?

java - 如何在java中捕获groovy抛出的异常?

java 从本地化的 NumberFormat 生成正则表达式

architecture - CQRS:查询和业务规则

jsf - 在 JSF 中,有没有一种方法可以根据条件或参数向组件添加 passThroughAttribute?