CriteriaBuilder和isMember实体列表(休眠) [英] CriteriaBuilder and isMember of list on entity (Hibernate)

查看:282
本文介绍了CriteriaBuilder和isMember实体列表(休眠)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在尝试做一些非常奇怪的事情.我正在尝试使用CriteriaBuilder和CriteriaQuery返回在实体集合中包含给定对象的所有实体.我已经为此花了好几个小时的时间,相关的文档很少,而且不是特别有用.任何帮助将不胜感激.

Okay, I'm trying to do something quite strange. I'm trying to use CriteriaBuilder and CriteriaQuery to return all entities who contain a given object in a collection on the entity. I've been banging my head against this for hours and the relevant documentation is little and not particularly useful. Any help would be greatly appreciated.

这些类的相关摘录如下.

Relevant excerpts of the classes are as follows.

@Entity
public class Entry {
    @ManyToMany(fetch=FetchType.EAGER)
    private List<Tag> tags = new ArrayList<Tag>();
}

@Entity
public class Tag {
    @Size(min=1,max=63)
    @Column(unique=true)
    @NotNull
    private String name;
}

@Repository
@Transactional
public class EntryDaoImpl extends RefineableDao implements EntryDao{
    @Autowired
    private EntityManager em;

    @Override
    public List<Entry> search(final Map<String,Object> refinement){
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Entry> query = cb.createQuery(Entry.class);
        Root<Entry> entry = query.from(Entry.class);
        query.orderBy(cb.desc(entry.get("createTime")));
        query.select(entry);
        query.where(this.refineQuery(refinement, cb, query, entry));
        return em.createQuery(query).getResultList();
    }
}

因此,基本上,我需要能够找到包含给定标签的条目.我知道,如果这是我所做的唯一优化,则可以非常简单地完成,但是对同一个CriteriaQuery也有很多其他优化,而且我无法保证将要进行哪些优化.

So basically, I need to be able to find Entries which contain a given Tag. I know that this could be done quite simply if it was the only refinement I was doing, but there are a lot of other refinements being done to the same CriteriaQuery, and I never have any guarantees of which refinements are going to be going through.

如果您真的需要了解后端,请查看 http://pastebin.com/QJnkYRRh .这是一个可怕的未经评论的烂摊子.

If you really need to know the backend, take a look at http://pastebin.com/QJnkYRRh . It's a horrible un-commented mess.

推荐答案

好的.我已经找到了关于为什么我在CriteriaBuilder.isMember()上遇到问题的答案.问题是Path必须是Path<? extends Collection>.这样,isMember()就像我希望的那样工作.

Okay. I've found my answer as to why I was having problems with CriteriaBuilder.isMember(). The issue is that the Path needs to be Path<? extends Collection>. With that, isMember() works just like I was hoping it would.

这篇关于CriteriaBuilder和isMember实体列表(休眠)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆