我无法理解 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/