在枚举集合上使用IN运算符时出现问题

时间:2018-09-03 08:06:44

标签: jpa-2.0 criteria-api hibernate-5.x

我有以下实体:

A

现在,根据类别查找记录

public class MyEntity{
public static enum InterestType{
SPORTS,MUSIC
 }
//AutoGenerated
private Long id;
@Convert(converter = InterestsTypeToString.class) // convert Set<InterestType> to comma separated string e.g SPORTS,MUSIC and vice verca 
@Column(name = "CATEGORY")
private Set<InterestType> category = new HashSet<>();

//getter
//setters
}

当我执行上面的代码时,我得到一个异常:

try {
        CriteriaQuery<MyEntity> criteria = getCriteriaQuery();
        Root<MyEntity> enti = criteria.from(MyEntity.class);
        List<Predicate> restrictions = new ArrayList<>();
        Predicate isActive = getCriteriaBuilder().equal(enti.get("active"), true);
                        Set<Set<InterestType>> catagories = new HashSet<>();
                        for(String cat : filterEntry.getValue().split(",")) {
                            Set<InterestsType> tempSet = new HashSet<>();
                            tempSet.add(InterestType.getFromCode(Integer.parseInt(cat)));
                            catagories.add(tempSet);
                        }
                        restrictions.add(enti .get(filterEntry.getKey()).in(catagories));

        TypedQuery<MyEntity> findByCat= getEntityManager().createQuery(criteria.select(enti)
                    .where(getCriteriaBuilder().and(isActive),
                            getCriteriaBuilder().or(restrictions.get(0))));

        findByCat.setFirstResult(pageNo - 1);
        findByCat.setMaxResults(pageSize);
        return findByCat.getResultList();
    } catch (NoResultException ex) {
        return null;
    } catch (Exception e) {
        throw new ApplicationExceptions("db.find.exception", null, e);
    }

有人可以解释代码有什么问题吗?我该如何从枚举集合中过滤记录?

0 个答案:

没有答案