-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Hibernate Filters are not working
PostPosted: Tue May 27, 2008 2:20 am 
Newbie

Joined: Tue May 27, 2008 1:37 am
Posts: 2
Location: Lahore
Hi
I have applied filter annotations but they are not working. Following is my Class UserProfile and then this class's service which returns data. But filters are not working there. I have enabled them and they are not working how to resolve this issue.
i ve used
Code:
@FilterDef(name="adminFilter", parameters=@ParamDef( name="adminUser", type="string" ))
@Filters( {   
    @Filter(name="adminUser", condition=":adminUser = userid")
} )

say userid= henry then the result should only contain henry as userid. but other records are also appearing in records. Thanx in advance.
what is the problem with this:
Code:
   Filter filter = session.enableFilter("adminFilter");
          filter.setParameter("adminUser","admin");

Regards
Asad Naeem


[b]Hibernate version:3.1

[b]Name and version of the database you are using: PostgreSQL 8.2


Code:
/*
*UserProfile.java
*
*/

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.AccessType;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratorType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

import javax.persistence.Table;
import javax.persistence.Lob;
import javax.persistence.LobType;
import javax.persistence.ManyToOne;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;

import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.ParamDef;
import org.hibernate.annotations.Filters;

@Entity (access = AccessType.FIELD)
@Table (name="UserProfile")
@XmlType(name="UserProfile")
@FilterDef(name="adminFilter", parameters=@ParamDef( name="adminUser", type="string" ))
@Filters( {   
    @Filter(name="adminUser", condition=":adminUser = userid")
} )
@XmlAccessorType(XmlAccessType.FIELD)
public class UserProfile
{
    @Id (generate = GeneratorType.NONE)
    private String userID;
   
    @ManyToMany (fetch=FetchType.EAGER,targetEntity=com.catalisse.security.UserGroup.class,cascade = {CascadeType.ALL})   
    private Set < UserGroup > groups = new HashSet < UserGroup > ();
   
    @ManyToMany (fetch=FetchType.EAGER,targetEntity=com.catalisse.security.Operation.class,cascade = {CascadeType.PERSIST,CascadeType.REMOVE})   
    private Set < Operation > otherOperations = new HashSet < Operation > ();
   
    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private SecurityQuestion question;

    public SecurityQuestion getQuestion() {
        return question;
    }

    public void setQuestion(SecurityQuestion question) {
        this.question = question;
    }
   
    public UserProfile ()
    {
       
    }
       
    public String getUserID ()
    {
        return userID;
    }
   
    public void setUserID (String userID)
    {
        this.userID = userID;
    }
   
   
    public Set<UserGroup> getAllGroups()
    {
        return this.groups;
    }

   
    public void setAllGroups(Set<UserGroup> groups)
    {
        this.groups = groups;
    }
    public UserGroup[] getGroups() {
   if(this.groups!=null)
      {
         return (UserGroup[]) this.groups.toArray(new UserGroup[0]);         
      }
      return null;
   }

   public void setGroups(UserGroup[] groups) {
      this.groups.clear();
      this.groups.addAll(Arrays.asList(groups));
   }

   public Operation[] getOtherOperations() {
      if(this.otherOperations!=null)
      {
         return (Operation[]) this.otherOperations.toArray(new Operation[0]);
      }
      return null;
   }

   public void setOtherOperations(Operation[] otherOperations) {
      this.otherOperations.addAll(Arrays.asList(otherOperations));
   }
   
}

Here is my service class which filters data.
Code:
Session session = null;
Transaction trans = null;
List UserDtoList = new ArrayList<UserDTO>();   // for storing User Profile related data
   
         Object[] usergroupnames = this.retrieveGroupNames();
        List<Object> groupnames_list = new ArrayList<Object>();
         if(groupnames_list!=null)
         {
            groupnames_list.addAll(Arrays.asList(usergroupnames));
         }
          session = com.catalisse.common.util.HibernateUtil.getSession(); // Custom Method
         
          //session.enableFilter("adminFilter").setParameter("adminUser", "admin");
           
          //Show admin user only - these lines are not working neither they are showing any exception(s)
          Filter filter = session.enableFilter("adminFilter");
          filter.setParameter("adminUser","admin");
         
          trans = session.getTransaction();
          trans.begin();   
         
          Criteria hibernateCriteria = session.createCriteria(com.catalisse.security.UserProfile.class,"userprofile");
         
          hibernateCriteria.setProjection(Projections.projectionList().add(Projections.property("userprofile.userID"))
                  .add(Projections.property("userprofile.personID"))
                  .add(Projections.property("userprofile.enabled")));
          SearchCriteria searchCriteria = (SearchCriteria)criteria;                           
          String [] ownerArray = searchCriteria.getOwners ();
          List<String> ownerList = new ArrayList<String>();
          ownerList = Arrays.asList (ownerArray);
          if(ownerList.size () != 0)
                {
                   for(int count=0; count<ownerList.size(); count++)
                         {
                     if(ownerList.get(count).equals("groups"))
                      {
                       hibernateCriteria.createAlias ("userprofile.groups", "groups");
                     }
                         }
                        hibernateCriteria.add (CriteriaConverter.buildCriterion (searchCriteria,true));
                      }
                      else
                      {
                          hibernateCriteria.add (CriteriaConverter.buildCriterion (searchCriteria,false));
                      }
         
     hibernateCriteria.addOrder(org.hibernate.criterion.Order.asc("userprofile.userID"));
          List tempResults =  hibernateCriteria.list ();
        if ( tempResults.size()==1) // i have applied filters only for admin //records so there should be only admin user records and i have only 1 //record in my Database
        {
             System.out.println("Data is oknow.");
         }
Code:


Top
 Profile  
 
 Post subject: RE: hibernate filters are not working
PostPosted: Wed Jun 04, 2008 10:17 am 
Newbie

Joined: Wed Jun 04, 2008 10:02 am
Posts: 3
Location: Tielt, Belgium
hi,

Shouldn't your filter name be 'adminFilter' instead of 'adminUser' ?
like this:
@Filter(name="adminFilter", ...

g


Top
 Profile  
 
 Post subject: Hibernate filters arr again creating probems
PostPosted: Tue Jul 15, 2008 3:05 am 
Newbie

Joined: Tue May 27, 2008 1:37 am
Posts: 2
Location: Lahore
@FilterDef(name="activationFilter")
@Filter(name="activationFilter", condition="inactivatedate is null")
public class ClassB extends ClassA implements Activatable
{
private Calendar inActivateDate;
public ClassB()
{

}

/**
* @return the inActivateDate
*/
public Calendar getInActivateDate() {
return inActivateDate;
}

/**
* @param inActivateDate the inActivateDate to set
*/
public void setInActivateDate(Calendar inActivateDate) {
this.inActivateDate = inActivateDate;
}
}

this is my class structe. i want only those records where inactivatedate is null but it is not working although i enabled the filter. Why is it so?


Top
 Profile  
 
 Post subject: RE: Hibernate filters arr again creating probems
PostPosted: Wed Jul 16, 2008 10:27 am 
Newbie

Joined: Wed Jun 04, 2008 10:02 am
Posts: 3
Location: Tielt, Belgium
Try changing
@Filter(name="activationFilter", condition="inactivatedate is null")
to
@Filter(name="activationFilter", condition="inActivateDate is null")

OR
annotate your get method with an @Column mapping, like this:

/**
* @return the inActivateDate
*/
@Column(name = "inactivatedate")
public Calendar getInActivateDate() {
return inActivateDate;
}
BUT (in the last case) this means that the columnname in the underlying db-schema should also be 'inactivatedate' .

g
-wil-


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.