Criteria eclipselink join

那年仲夏 提交于 2019-12-13 02:56:48

问题


I can't understand how do a join in eclipse link with criteria. This my entity:

public class A implements Serializable {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Basic(optional = false)
  @Column(name = "id")
  private Long id;
  @Column(name = "value")
  private String value;   

  @OneToMany(mappedBy = "aid")
  private Collection<B> bCollection; 
}

public class B implements Serializable {

  @Id      
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Basic(optional = false)
  @Column(name = "id")
  private Long id;

  @JoinColumn(name = "a_id", referencedColumnName = "id")
  @ManyToOne
  private A aid;  
}

I do this:

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery();
    Root<A> a = cq.from(A.class);
    Join<A, B> j = a.join("aid",JoinType.INNER);
    cq.distinct(true);
    //and now what?        
    cq.where(cb.equal(a.get("id"), "aid"));
    Object r = em.createQuery(cq).getResultList();

And now, how I can to bind my join clause to CriteriaQuery?


回答1:


First, in order to get a result list containing all fields from A and B, you need to shape your results as a list of Tuples or a list of Objects like explained in this article (chapter Projecting the result).

This requires using a multiselect statement or using construct like this:

cq.multiselect(a, b));
cq.select(cb.construct(a, b));

where b should be obtained like this:

CollectionJoin<A, B> b = a.join(A_.bCollection, JoinType.INNER);  // using Metamodel



回答2:


If all you want is "Select a from A a join a.bCollection" such that only As with a B are returned, then all you need is:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery();
Root<A> a = cq.from(A.class);
Join<A, B> j = a.join("bcollection",JoinType.INNER);
cq.distinct(true);
Object r = em.createQuery(cq).getResultList();

JPA will create the A->B table join for you using the relationship defined on B's aid attribute, so you don't need to. Returned A's will be complete, except that since you are using the default fetch type on bCollection mapping, Bs maybe lazily fetched. IF you want them eagerly brought in and associated to the As in a single query, use the fetch method instead of the join:

a.fetch("bcollection",JoinType.INNER);


来源:https://stackoverflow.com/questions/13031138/criteria-eclipselink-join

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!