我面临的问题是对数据表的列role
(Lazy DataModel)进行过滤/排序,该列不是实体类的属性,因为列值是动态创建的
依赖于当前登录的用户,当前登录的用户对于某些行可能为user
,对于某些行可能为manager
。
例如,我有实体类Activity
实体类的两个属性是:
@javax.persistence.Entity
@Table(name = "ACTIVITY")
public class Activity extends Entity {
private Set<User> users;
private Set<User> managers;
}
如果当前登录用户位于特定活动行的managers
,则该行的role
(不在Activity
实体中的列)将为Manager
。
如果当前登录的用户位于特定活动行的users
,则该行的role
将为user
。
如何实现?谁能帮我?任何指针对我都非常有帮助。谢谢
更新-1:
role
列值正确显示。在实体类中,我用这种方式计算role
:
@Transient
public String getRole(){
String role="";
//calculate role depending on currettly logged in user..
return role;
}
现在,当我要过滤时,会生成异常:
ERROR [com.edfx.adb.web.model.ActivityDataModel] null: org.hibernate.QueryException: could not resolve property: role of: com.edfx.adb.persist.entity.Activity
我将如何克服这个问题?
更新-2
ActivityDao课程:
public List<Activity> loadActivities(int startIndex, int maxResult, String sortField, boolean sortOrder, Map<String, Object> filters) {
Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(entityClass, "act");
createActivitiesFilterCriteria(criteria, filters);
return (List<Activity>) criteria.list();
}
private void createActivitiesFilterCriteria(Criteria criteria, Map<String, Object> filters) {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
String userName = session.getAttribute("username").toString();
if(filters.containsKey("role")){
String value = (String) filters.get("role");
if(!value.equals("all")){
criteria.add(Restrictions.eq("getRole();", value));
}
filters.remove("role");
}
for (Entry<String, Object> entry : filters.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
criteria.add(Restrictions.ilike(key, value.toString(), MatchMode.ANYWHERE));
}
}
ActivityDataModel的代码:
@Override
public List<ActivityDTO> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
Map<String, Object> daoFilters = new HashMap<String, Object>();
Map<String, Object> totalFilters = new HashMap<String, Object>(daoFilters);
try {
activityDTOs = activityService.loadActivities(first, pageSize, sortField, order, daoFilters);
setRowCount(activityService.getTotalNoOfFilteredActivity(totalFilters));
} catch (Throwable throwable) {
}
return activityDTOs;
}