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: