[英]Right join using JPA criteria api
I'm trying to implement this sql query using jpa criteria api : 我正在尝试使用jpa条件api实现此sql查询:
SELECT F.* FROM PF right join F on F.FID = PF.FID WHERE PF.PFID is null;
Which also can be written as: 也可以写成:
SELECT F.* FROM F left join PF on F.FID = PF.FID WHERE PF.FID is null;
This is what I tried: 这是我尝试的:
public List<F> listFWithoutP() {
final CriteriaBuilder builder = getCriteriaBuilder();
final CriteriaQuery<F> query = builder.createQuery(F.class);
final Root<PF> from = query.from(PF.class);
Join<PF, F> join = from.join(PF_.f, JoinType.RIGHT);
query.select(join.get(PF_.f))
.where(builder.isNull(from.get(PF_.pFId)));
final TypedQuery<F> typedQuery = getEntityManager().createQuery(query);
return typedQuery.getResultList();
}
But it doesn't work, I get the following error in this line : query.select(join.get(PF_.f))
但这不起作用,我在此行中收到以下错误:
query.select(join.get(PF_.f))
The method get(SingularAttribute<? super F,Y>) in the type Path<F> is not applicable for the arguments (SingularAttribute<PF,F>)
How can I solve this ? 我该如何解决?
These are my entities : 这些是我的实体:
public class F extends AbstractDomain<Long> {
@Id
@Column
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "idgen")
private Long fId;
@Column(nullable = false)
private String lib;
}
public class PF extends AbstractDomain<Long> {
@Id
@Column
private Long pFId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false)
private P p;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false)
private F f;
}
It seems like RIGHT JOIN is not supported by the jpa criteria api, so this can be done using the second query. jpa条件api似乎不支持RIGHT JOIN,因此可以使用第二个查询来完成。
final Session session = entityManager.unwrap(Session.class);
final Criteria criteria = session.createCriteria(PF.class, "pf")
.createAlias("pf.f", "f")
.add(Restrictions.eqProperty("pf.pFId",
"f.fId"))
.add(Restrictions.isNull("pf.pFId"));
Hello Aimad,
please try this criteria query and let me know if that works,this criteria would exactly replicate your SQL query.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.