java - 标准 eclipselink 加入

标签 java eclipselink criteria-api

我无法理解 Eclipse 中的连接如何与条件链接。 这是我的实体:

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 子句绑定(bind)到 CriteriaQuery?

最佳答案

首先,为了获得包含 A 和 B 中所有字段的结果列表,您需要将结果塑造为 Tuple 列表或 Object 列表>就像在article中解释的那样(预测结果一章)。

这需要使用 multiselect声明或使用 construct像这样:

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

其中 b 应该这样获得:

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

关于java - 标准 eclipselink 加入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13031138/

相关文章:

java - 同一个 BufferedImage 对象加载多个图像时出现内存泄漏

jpa - 将完整实体标记为 "updatable=false"

java - 将 <provider> 添加到 Persistence.xml 时,它“显示 "Invalid content was found starting with element ' 提供程序”。”

java - CriteriaBuilder 查询在两列之间使用 Like

java - jxls读取行数未知的excel

java - 将 scala 与 java 集成

java - 从 javax.persistence.Query 构建 org.eclipse.persistence.queries.ReportQuery

java - 不使用元模型的 JPA 2 Criteria API 不区分大小写的条件

hibernate - JPA 2.0/hibernate : No supertype found

java - 如何单击列表 selenium2 中的链接