如何在JSF中使用OpenJPA EntityManagerFactory [英] How to use OpenJPA EntityManagerFactory in JSF

查看:134
本文介绍了如何在JSF中使用OpenJPA EntityManagerFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WebSphere Application Server上使用OpenJPA. 我的Web应用程序使用JSF 2(无EJB).

I use OpenJPA on WebSphere Application Server. My WebApplication uses JSF 2 (without EJB).

我想知道是否有最佳实践,如何在JSF中使用JPA.

I wanted to know if there are best practices how to use JPA in JSF.

我发现EntityManagerFactory(EMF)应该在应用程序范围内. 此外,应仅在应用程序停止时销毁EMF.

I found that the EntityManagerFactory (EMF) should be application scoped. Also the EMF should be (only) destroyed when the application stops.

但是我的消息来源不是最新的:

But my sources are not the newest:

Java记事本:Web应用程序中的JPA EMF 从2007年起

在Web层中使用JPA的最佳方法从2010年起

如何关闭JPA EntityManger在网络应用程序中 从2007年起

在JSF中是否还有其他实践或更好的方法来处理JPA? 尤其是EMF和DAO的范围.

Are there other practices or better ways to handle JPA in JSF? Especially Scope of EMF and DAO.

最好的问候,投票

推荐答案

创建一个简单的 servlet过滤器,该过滤器将创建init()方法中的EMF,在destroy()方法中将其关闭,并在doFilter()方法中创建一个EntityManager,将其设置为ThreadLocal<EntityManager>.您也可以在其中开始和结束交易.

Create a simple servlet filter which creates the EMF in init() method, closes it in destroy() method and creates an EntityManager in doFilter() method which get set as a ThreadLocal<EntityManager>. You can also begin and end the transactions in there.

这也称为视图中的打开会话"或视图中的打开实体管理器"模式.

This is also known as the "open session in view" or "open entitymanager in view" pattern.

这篇关于如何在JSF中使用OpenJPA EntityManagerFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆