0

我在过滤/排序role不是实体类属性的数据表的列(惰性数据模型)时遇到问题,因为列值是根据当前登录的用户动态创建的,当前登录的用户可能是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. 这如何实施?谁能帮我?任何指针都会对我很有帮助。谢谢

Update-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;
}
4

0 回答 0