English 中文(简体)
jjpa 很多人关系的标准
原标题:jpa criteria for many to many relationship

我在贾瓦、答复和合伙人中有两门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. What I need to do from Hibernate CriteriaQuery is to find the collaborators for an answer given by id.

我已经通过Hibernate Criteria(org.hibernate.Criteria,使用结果变压器,但我在使用Criteria Query时 st了,因为我没有一份加入名单。

最佳回答

It s done, finally...

Here s the code:

public List<Collaborator> getCollaborators(Long answerId) {
  CriteriaBuilder cb = entityManager.getCriteriaBuilder();
  CriteriaQuery<Collaborator> criteriaQuery = cb.createQuery(Collaborator.class);
  
  Root<Answer> answerRoot = criteriaQuery.from(Answer.class);
  SetJoin<Answer, Collaborator> answers = answerRoot.join(Answer_.collaborators);
  criteriaQuery.where(cb.equal(answerRoot.get(Answer_.id), answerId));
  
  return entityManager
    .createQuery(criteriaQuery.select(answers))
    .getResultList();
}
问题回答

总部:

你们可以这样做:

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

获取与某一合作者相关的所有答复。

而且:

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"));

接触所有与某种答复相关的学者。

利用JPA2标准 页: 1

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
Join<Answer , Collaborator> join = root.join("collaborators",JoinType.INNER);

predicates.add(criteriaBuilder.equal(join.get("id"),id));

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%"));




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签