在JSF视图中JPA延迟加载Collections-比使用Filters更好的方法吗? [英] JPA lazy loading Collections in JSF view - better way than using Filters?
问题描述
当前,我正在使用事务视图"模式来使视图中的集合的延迟加载成为可能.
Currently I am using a Transaction View pattern to make lazy-loading of collections possible in views.
我在web.xml中有以下内容
I have the following in web.xml
<filter>
<filter-name>view</filter-name>
<filter-class>com.jasoni.ViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>view</filter-name>
<url-pattern>*.xhtml</url-pattern>
</filter-mapping>
Filter类具有以下内容...
And the Filter class has the following...
public class ViewFilter implements Filter {
@Resource UserTransaction tx;
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
try {
tx.begin();
chain.doFilter(request, response);
}
//catch here
finally {
//another try-catch
tx.commit();
}
}
}
然后假设我有以下(人为设计的)后备豆
Then assuming I have the following (rather contrived) backing bean
@ManagedBean
@RequestScoped
public class DepartmentEmployees {
@EJB
private DepartmentServiceBean deptService;
@ManagedProperty(value="#{param.deptId}")
private Integer deptId;
private Department dept;
@PostConstruct
public String init() {
dept = deptService.findById(deptId);
}
}
我可以在我的视图(.xhtml文件)中做类似的事情
I can do something like this in my view (.xhtml file)
<ul>
<c:forEach var="emp" items="#{departmentEmployees.dept.employees}">
<li>#{emp.firstName} #{emp.lastName}</li>
</c:forEach>
</ul>
只是想知道是否有人知道不使用过滤器(或servlet)就可以完成同一件事的另一种方式.
Just wondering if anybody knows of a different way to accomplish the same thing without using filters (or servlets).
推荐答案
此方法(打开的会话可见")有两个主要缺点.除了有点骇人听闻(肯定不是控制业务会话的servlet过滤器的设计思想)之外,您没有很多选项可以适当地处理呈现JSF页面时发生的任何实际"异常.
This approach ("open session in view") has a couple of major disadvantages. Besides being kind of hacky (it's certainly not the design idea of a servlet filter to control a business session) you don't have many options to appropriately process any "real" exception that occurs while rendering the JSF page.
关于基础架构/技术堆栈的介绍不多,但是我假设您使用的是Java EE 6服务器.
You don't write much about your infrastructure / technology stack, but I assume that you are on a Java EE 6 server.
我通常在扩展模式下使用EntityManger
,并通过仅注释我的业务外观的某些方法来显式控制的事务将其刷新.看一下这个例子(摘自 Adam Bien-真实世界Java EE模式,反思最佳实践):
I usually use the EntityManger
in Extended Mode and flush it with transactions which I control explicitly by annotating only certain methods of my business facade. Have a look at this example (taken from Adam Bien - Real World Java EE Patterns, Rethinking Best Practices):
@Stateful
@TransactionAttribute(TransactionAttributeType.NEVER)
public class BookFacadeBean implements BookFacade {
@PersistenceContext(type=PersistenceContextType.EXTENDED)
private EntityManager em;
private Book currentBook;
public Book find(long id){
this.currentBook = this.em.find(Book.class, id);
return this.currentBook;
}
public void create(Book book){
this.em.persist(book);
this.currentBook = book;
}
public Book getCurrentBook() {
return currentBook;
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void save(){
//nothing to do here
}
}
此方法的下一个层次是将EntityManager绑定到CDI对话范围.看看(a)焊接(b)
A next level in this approach would be to bind the EntityManager to a CDI conversation scope. Have a look at (a) Weld (b) Seam 3 Persistence for further discussions on that topic.
这只是替代方案的粗略草图,而不是详细的操作方法.我希望您所要询问的是这些信息,您可以随时提出其他问题. :-)
This is rather a rough sketch of an alternative than a detailed how-to. I hope this level of information is what you were asking about - feel free to ask further questions. :-)
这篇关于在JSF视图中JPA延迟加载Collections-比使用Filters更好的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!