我对jdo fitler.add

时间:2019-02-01 16:44:21

标签: java sql jdo

我在JDO过滤器添加中的语法有问题。我已经在下面发布了代码。我对JDO还是很陌生,并试图从代码库中的其他工作示例进行反向工程。

我想做的是通过附在产品上的ID获取成就对象的列表。

我收到“变量'成就'是未绑定的,无法确定”,但是我不确定filter的语法。在这种情况下添加作品。

public Resolution list() {
    Filter filter = em.newFilter();
    Identity identity = getIdentity();

    // apply security restrictions
    if (identity.getSelectedSchoolId() != null){
        Long school = params().getLong("school.id");
        if (school != null){
            filter.add(":allowed.contains(id) || schoolId == :school");
            filter.set("allowed", getSchoolAccess(school).getAchievements());
            filter.set("school", identity.getSelectedSchoolId());
        }else{
            filter.add(":allowed.contains(id) || :schools.contains(schoolId)");
            filter.set("allowed", getUserAccess().getAchievements());
            filter.set("schools", getUserAccess().getSchools());
        }
    }else{
        filter.add("schoolId == null");
    }

    Long categoryId = params().getLong("filter.category");
    if ( categoryId != null && categoryId != 0 ){
        Category filterCategory = params().getEntity("filter.category", Category.class);
        List<Category> categories = categoryManager.getTree(filterCategory);
        List<Long> ids = new ArrayList<>();

        for (Category cat : categories){
            ids.add(cat.getId());
        }

        filter.add(":categories.contains(category.id)");
        filter.set("categories", ids);
    }

    // filter by product
    Long productId = params().getLong("filter.product");
    if (productId != null && productId != 0 ){
          Product product = em.get(Product.class, productId);
         List<Achievement> achievements = product.getAchievements();
         System.out.println(achievements);
         List<Long> ids = new ArrayList<>();
         for (Achievement ach : achievements){
                ids.add(ach.getId());
            }
         filter.add(":achievements.contains(achievement.id)");
            filter.set("achievements", ids);     
      }


    Query q = em.newQuery(Achievement.class);
    q.setFilter(filter.toString());
    q.setOrdering("category.key,name");

    // prepare paging 
    Integer limit = params().getInt("limit");
    Integer start = params().getInt("start");

    if ( start == null )
        start = 0;

    if (limit != null)
        q.setRange(start, limit);

    try {
        em.with(Achievement.class, "image", "category");
        List<Achievement> list = (List<Achievement>) q.executeWithMap(filter.args());
        stash().put("list", list);

        mapper().filter(Achievement.class).add("image", "category");
        return json();
    } finally {
        q.closeAll();
    }
}

0 个答案:

没有答案