在 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/