简体   繁体   English

jpa 多对多关系的标准

[英]jpa criteria for many to many relationship

I have 2 POJO classes in Java, Answer and Collaborator, in a many-to-many relationship.我在 Java、Answer 和 Collaborator 中有 2 个 POJO 类,它们是多对多关系。

class Answer {
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "ANSWERS_COLLABORATORS", joinColumns = { @JoinColumn(name = "aid") }, inverseJoinColumns = { @JoinColumn(name = "cid") })
    private Set<Collaborator> collaborators = new HashSet<Collaborator>(0);
} 

Class Answer has a set of Collaborator , but a Collaborator doesn't keep a set of Answer . Class Answer有一组Collaborator ,但Collaborator不保留一组Answer What I need to do from Hibernate CriteriaQuery is to find the collaborators for an answer given by id.我需要从 Hibernate CriteriaQuery中找到合作者以获取 id 给出的答案。

I have already done this with Hibernate Criteria ( org.hibernate.Criteria ) using result transformer, but I'm stuck when it comes to using CriteriaQuery , because I don't have a list of answers to give to the join.我已经使用结果转换器使用 Hibernate Criteria ( org.hibernate.Criteria ) 完成了此操作,但是在使用CriteriaQuery时我被卡住了,因为我没有要给连接的答案列表。

It's done, finally... 它完成了,最后......

Here's the code: 这是代码:

public List<Collaborator> getCollaborators(Long answerId) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Collaborator> criteriaQuery = criteriaBuilder
                .createQuery(Collaborator.class);
        Root<Answer> answerRoot = criteriaQuery.from(Answer.class);
        criteriaQuery.where(criteriaBuilder.equal(answerRoot.get(Answer_.id),
                answerId));
        SetJoin<Answer, Collaborator> answers = answerRoot
                .join(Answer_.collaborators);
        CriteriaQuery<Collaborator> cq = criteriaQuery.select(answers);
        TypedQuery<Collaborator> query = entityManager.createQuery(cq);
        return query.getResultList();

    }

Using HQL: 使用HQL:

You can use this: 你可以用这个:

Criteria criteria = session.createCriteria(Answer.class);
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
criteria.createAlias("collaborators", "collaborators");
criteria.add(Restrictions.eq("collaborators.id",desiredCollaboratorId);

to get all the Answers associated to a certain Collaborator. 获取与某个协作者关联的所有答案。

And this: 还有这个:

Criteria criteria = session.createCriteria(Answer.class);
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
criteria.setFetchMode("collaborators", FetchMode.JOIN)
criteria.add(Restrictions.idEq(desiredAnswerId));
dsrTrackingCriteria.setProjection(Projections.property("collaborators"));

To get all Collaborators associated to a certain Answer. 让所有协作者与某个答案相关联。

Using JPA2 Criteria API you can do something like: 使用JPA2 Criteria API,您可以执行以下操作:

CriteriaBuilder cb = em.getCriteriaBuilder(); //creted from EntityManager instance

CriteriaQuery<Long> cq = cb.createQuery(Collaborator.class);
Root<Answer> rootAnswer = cq.from(Answer.class);
Join<Collaborator,Answer> joinAnswerCollaborator = rootAnswer.join("collaborators"); //(or rootAnswer.join(Answer_.collaborators); if you've created the metamodel with JPA2

Using criteria Builder :使用标准生成器:

Join<CLASS_A, CLASS_B> join = root.join(WHAT_UVE_DECLARED_IN_MAPPEDBY, JoinType.INNER);
searchCriteria.add(criteriaBuilder.like(join.get("FIELD_IN_SUBCLASS").as(String.class), "%blabla%"));

Join<Answer, Collaborator> join = root.join("collaborators ",JoinType.INNER); Join<Answer, Collaborator> join = root.join("collaborators",JoinType.INNER); predicates.add(criteriaBuilder.equal(partnerEntityCityEntityJoin.get("id"),Long.parseLong(v))); predicates.add(criteriaBuilder.equal(partnerEntityCityEntityJoin.get("id"),Long.parseLong(v)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM