`
aooboo
  • 浏览: 84566 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

seam中使用hibernate filter详解

阅读更多

最近用到hibernater filter,查阅了好多资料,感觉本文不错,特摘录如下。

 

原文地址:http://www.next-presso.fr/2008/10/how-to-use-hibernate-filters-in-seam/lang/en

 

How to use Hibernate filters in Seam

 

JBoss Seam is very rich and powerful framework but some of its features are badly documented.

Hibernate Filters is one of these forgotten features. In fact filters documentation in Seam is rather useless.

 

I won’t explain in details Hibernate Filters here. Hibernate documentation does it ways better. To make short it’s a smart solution to add conditions to the “where” clause of a request at runtime.

To create such a filter in Seam, one will also have to read Hibernate annotations on that matter.

For instance let say we have an used cars stock for which we had developed a bunch of forms and pages to search and navigate through it. We also created a Seam component to handle a requests collection to populate search form’s combo box fields and orchestrate all the search and navigation.

In database, cars have a “stockVoLib” field which gives car origin (demonstration, management car, etc…). Until now this field wasn’t used by the application and search were done on all the stock db. But now, our customer ask for a new navigation having exactly the same look at the first one but restricted on a given car origin.

We can answer to this request in two different ways.

  • Refactor all the running website’s engine to introduce the new “stcokVoLib” parameter in all the requests and code calling these requests
  • Create a filter which will save us a lot of refactoring work on existing code.

Guess what ? I prefer to create a filter :-) (good news for the end of this post).

1.Hibernate filter creation with annotations

First thing to do : define the filter. We can dot it in the target entity or at the package level. As a filter can be applied to more than one entity I prefer the package level option. So I add a “package-info.java” file to my entities package to define the filter in it.
@FilterDef(name = "stockLib", defaultCondition = "stockLibele = :aStockLib", parameters = @ParamDef(name = “aStockLib”, type = “string”)) package org.fpp.domain; import org.hibernate.annotations.FilterDef; import org.hibernate.annotations.ParamDef;

The name of the filter is “stockLib”, it has a default condition which will be used if there is no condition defined when the filter is applied on the entity and it also has one string parameter : “aStockLib”.
Keep in mind that the condition defined here is not a HQL “where” but a SQL one.

After that I apply the filter on one or more entities (I could also attach it to a collection). In my use case I only have one entity concerned : “usedCar”.
@Entity @Filter(name = “stockLib”) public class usedCar { …

3.Using the filter with Hibernate

Now, I can use the filter with Hibernate. I can activate it and send it parameter values by getting Hibernate session (if we are using JPA we do that with EntityManager.getDelegate())
Session session=(Session) entityManager.getDelegate(); session.enableFilter(”stockLib”).setParameter(”aStockLib”, “VD”); … session.disableFilter(”stockLib”);

3.What about Seam ?

You can do better with Seam by creating a filter component (wraping Hibernate filter) in which you can inject parameter at runtime.
We only have to have this component in the Seam components.xml file.
<persistence:filter name="stockLibFilter"> <persistence:name>stockLib</persistence:name> <persistence:parameters> <key>aStockLib</key> <value>#{parameterHdl.stockLib}</value> </persistence:parameters> </persistence:filter>

Remember that parameters value in this “meta-filter” should always be expressed in expression language even if we want inject a constant : #{’VD’}.
In our example “parameterHdl” is a Seam component in which the stockLib parameter is injected from the URL according tto the the following configuration in pages.xml
<param name="stockLib" value="#{parameterHdl.stockLib}" required="false"/>

At that point Seam filter can be injected in a specific EntityManager on which the filter will be always activated.
<persistence:managed-persistence-context name="emStockLib" auto-create="true" persistence-unit-jndi-name=”java:/fppEntityManagerFactory” > <persistence:filters> <value>#{stockLibFilter}</value> </persistence:filters> </persistence:managed-persistence-context>

This new entityManager could be injected in components instead of the standard entityManager (without filter). There is nothing to do to activate the filter, it is on by default.
But that’s not all !

 

4.Dynamic filters activation in Seam

It’s here that Seam documentation is lacking something very useful : filters activation can be dynamically set at run time.

Thus we can write someting like :
<persistence:filter name="stockLibFilter" enabled=”#{!(empty parameterHdl.stockLib)}”> <persistence:name>stockLib</persistence:name> <persistence:parameters> <key>aStockLib</key> <value>#{parameterHdl.stockLib}</value> </persistence:parameters> </persistence:filter>

Pay attention to the “enabled” parameter in which we put a boolean expression. It means that the filter is activated only if stockLib field of component parameterHdl is not empty. As this field is filled by URL parameter injection, an existing stockLib parameter in URL activate the filter.

In this usage we can apply the filter to the application’s main entityManager (no need to create a specific one) and this filter will be activated if its enabled parameter is satisfied. This test will be evaluated each time the entityManager is called (that’s Seam’s “magic”).
<persistence:managed-persistence-context name="entityManager" auto-create=”true” persistence-unit-jndi-name=”java:/fppEntityManagerFactory” > <persistence:filters> <value>#{stockLibFilter}</value> </persistence:filters> </persistence:managed-persistence-context>

So we have injected a dynamically activated Hibernate filter in the entityManager and its activation will depend of the application context.
One last thing to keep in mind : if the enabled parameter of the filter uses a Seam component (like in my example), you should not inject the “filtered” entityManager in this component. If you did you would get an exception at the application launch caused by the circular reference.

 

分享到:
评论

相关推荐

    seam 框架整合hibernate+jsf

    在Seam中,JSF和Hibernate的整合主要体现在以下几个方面: 1. **数据绑定**:Seam支持JSF的数据绑定,使得JSF组件可以直接绑定到 Seam managed beans上的属性,这简化了视图和模型之间的通信。 2. **事件处理**:...

    seam+hibernate注册例子

    - 在JSP中使用Seam的EL表达式(例如#{userBean.register})来调用注册方法。 5. **编写注册逻辑** - 创建一个Seam组件,例如`UserBean`,使用@SessionScoped和@Name注解,以处理用户的注册信息。 - 在`UserBean`...

    seam+spring+hibernate+richfaces配置

    Seam、Spring、Hibernate和RichFaces是Java Web开发中的四大框架,它们的集成可以构建功能强大的企业级应用程序。本文将详细解析这四个组件的核心概念、它们如何协同工作以及配置过程。 **Seam框架** Seam是一款...

    jsf seam hibernate 初学者练习

    **JSF、Seam与Hibernate简介** JSF(JavaServer Faces)是Java平台上的一个用于构建用户界面的组件模型框架,它提供了一种声明式的方式来创建动态Web应用程序。JSF的核心概念是组件,这些组件可以组合成用户界面,...

    SEAM 中文开发指南

    - **使用“推”风格的MVC**:展示了如何使用 SEAM 的推送机制来更新数据。 #### 三、使用Seam-gen快速启动 - **准备活动**:设置开发环境。 - **建立一个新的Eclipse项目**:通过 Eclipse 创建 SEAM 项目。 - **...

    seam build.properties文件详解

    hibernate.connection.password=testSeamGen #工作目录 workspace.home=E\:/workspace_seam #实体存放路径 model.package=com.gresoft.core.entity #数据源jar包 driver.jar=F\:\\jboss-seam-2.1.1.GA\\lib\\ojdbc14...

    JBoss Seam 工作原理、seam和hibernate的范例、RESTFul的seam、seam-gen起步、seam组件、配置组件、jsf,jboss、标签、PDF、注解等等

    1.3. Seam 中的可点击列表:消息示例............................................................................................................................. 27 1.3.1. 理解代码.........................

    maven+jetty+seam+hibernate

    在这个集成环境中,Maven负责整个项目的构建和依赖管理,Jetty作为运行时服务器,提供快速的开发和测试环境,Seam作为集成框架,简化了Java EE应用的开发,而Hibernate则作为持久层框架,处理数据的存取操作。...

    jboss seam 中文文档集合

    这个文档集合包含了对Seam及其相关技术的详尽介绍,帮助开发者深入了解并有效地使用Seam。 **1. Seam Reference** Seam Reference 提供了Seam框架的完整指南,包括英文版和中文版。中文版的Seam_2.0_Reference_zh_...

    seam in action 中文 english

    同时,Seam还支持Hibernate,使得对ORM(Object-Relational Mapping)的使用更加灵活。 6. **安全性**:Seam提供了内置的安全机制,如身份验证和授权,使得开发人员能够快速为应用添加安全特性。 7. **国际化与...

    Configuring Seam and packaging Seam applications

    这涉及到在web.xml文件中配置Seam Filter和Seam Listener,以便处理JSF请求生命周期中的事件,并确保Seam组件与JSF页面正确交互。 #### 1.1.2. 使用Facelets Facelets是JSF推荐的视图表示技术,提供了更简洁、可...

    Spring 2.5中文手册+hibernate手册+Seam_2.0_R中文手册

    Hibernate 3.x系列,包括手册中可能涵盖的版本,引入了HQL(Hibernate Query Language),一种面向对象的查询语言,以及Criteria API,提供了更灵活的查询方式。同时,Hibernate支持事务管理、缓存机制和第二级缓存...

    Seam框架文档简述

    ### Seam框架核心知识点详解 #### 一、Seam框架简介 Seam,全称为JBoss Seam,是一款基于Java EE 5的技术栈构建的应用框架。它通过整合JSF(JavaServer Faces)与EJB 3.0(Enterprise JavaBeans 3.0)组件,并充分...

    jboss seam 中文文档

    - **工作原理**:深入探讨了示例中使用的 Seam 特性,如事件处理机制和页面流。 ##### 1.4 Seam 和 jBPM:待办事项列表示例 - **代码理解**:展示了如何集成 Seam 和 jBPM 这两个框架,实现一个简单的待办事项列表...

    seam 中文文档 pdf

    seam 中文文档 pdf 格式 JSF+EJB3.0快速开发框架Seam的中文版向导。。。

    Jboss Seam中文版

    ### JBoss Seam中文版知识点详解 #### JBoss Seam简介 JBoss Seam是一个强大的企业级应用开发框架,基于Java EE标准,特别强调简化Web应用的开发流程。它通过整合多种技术如JSF、EJB 3.0等,提供了一种更为高效、...

    JBOSS SEAM组件中文手册

    Seam的主要目标是减少开发中的样板代码,提高生产力,同时保持灵活性和可扩展性。 **二、Seam核心概念** 1. **组件(Components)**: Seam的核心是组件模型,它允许开发者定义和管理应用中的对象。组件可以是简单的...

    seam 中使用fckeditor 的点点滴滴

    本文将围绕“Seam框架中使用FCKeditor”的主题进行详细探讨,结合给出的标签“源码”和“工具”,我们将深入理解如何在Seam项目中集成并使用FCKeditor,以及相关的依赖库。 Seam是一个Java EE框架,它整合了JSF...

    jboss seam 学习资料,seam in action和官方手册

    5. **持久化支持**:讨论Seam对JPA和Hibernate的支持,包括实体管理和事务管理。 6. **安全管理**:学习Seam的认证和授权机制,如使用Security子模块。 7. **事件和回调**:了解Seam事件系统的工作方式,以及如何...

Global site tag (gtag.js) - Google Analytics