eclipselink标准加入 [英] Criteria eclipselink join

查看:64
本文介绍了eclipselink标准加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何使用条件进行eclipse联接。
这是我的实体,

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;  
}

我这样做:

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

现在,如何将我的join子句绑定到CriteriaQuery?

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

推荐答案

首先,要获取包含A和B中所有字段的结果列表,您需要将结果成形为元组或对象的列表,如此文章(第投影结果章)。

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).

此需要使用 multiselect 语句或使用构造,例如:

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

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

其中b应该这样获得:

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

这篇关于eclipselink标准加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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