[jboss-user] [JBoss Seam] - Re: @Filter example

fernando_jmt do-not-reply at jboss.com
Mon May 14 11:37:23 EDT 2007


1.- Define the Filter in your Entity.


  | 
  | @Entity
  | @Table(name="NODE")
  |  @org.hibernate.annotations.FilterDef(
  |     name = "accessLevelFilter",
  |     parameters = {@org.hibernate.annotations.ParamDef(name = "currentAccessLevel", type="integer")}
  | )
  | @org.hibernate.annotations.Filter(
  |     name = "accessLevelFilter",
  |     condition = "READ_ACCESS_LEVEL <= :currentAccessLevel"
  | )
  | public abstract class Node implements Serializable {
  | ...
  | @Column(name = "READ_ACCESS_LEVEL", nullable = false)
  |     protected int readAccessLevel;
  | 
  | ...
  | }
  | 
  | 

2.- Configure components.xml


  |  <core:filter name="accessLevelFilter">
  |         <core:name>accessLevelFilter</core:name>
  |         <core:parameters>
  |             <key>currentAccessLevel</key>
  |             <value>#{currentAccessLevel}</value>
  |         </core:parameters>
  |     </core:filter>
  | 
  |  <core:managed-persistence-context name="restrictedEntityManager"
  |                                       auto-create="true"
  |                                       entity-manager-factory="#{wikiEntityManagerFactory}">
  |         <core:filters><value>#{accessLevelFilter}</value></core:filters>
  |     </core:managed-persistence-context>
  | 
  | 
  | 

3.- Set somewhere a value for #{currentAccessLevel}.

  |         Contexts.getSessionContext().set("currentAccessLevel", bestRole.getAccessLevel());
  | 

4.- Finally use the filtered Persistence Context.


  | @Name("nodeDAO")
  | @AutoCreate
  | @Transactional
  | public class NodeDAO {
  | 
  |     // Most of the DAO methods use this
  |     @In protected EntityManager restrictedEntityManager;
  | ...
  |  public Node findNode(Long nodeId) {
  |         restrictedEntityManager.joinTransaction();
  |         try {
  |             return (Node) restrictedEntityManager
  |                     .createQuery("select n from Node n where n.id = :nodeId")
  |                     .setParameter("nodeId", nodeId)
  |                     .getSingleResult();
  |         } catch (EntityNotFoundException ex) {
  |         } catch (NoResultException ex) {
  |         }
  |         return null;
  |     }
  | 
  | }
  | 
  | 


Above steps are based on the Seam wiki example. If you want to see more, check it out.

HTH.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4045523#4045523

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4045523



More information about the jboss-user mailing list