我在JPA条件API查询中收到异常CAN_NOT_JOIN_TO_BASIC:
@Entity
public class Connection {
@Id
private String id;
private String sourceNodeId;
private String targetNodeId;
}
@Entity
public class Node {
@Id
private String id;
private String graphId;
}
我想检索属于特定图表的所有连接。查询:
final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Connection> query = cb.createQuery(Connection.class);
Root<Connection> connection = query.from(Connection.class);
Join<Connection,Node> node = connection.join("sourceNodeId");
TypedQuery<Connection> typedQuery = entityManager.createQuery(query
.select(connection)
.where(cb.equal(joiningSequenceObject.get("graphId"), someId)));
return typedQuery.getResultList();
我做错了什么?帮助赞赏!
答案 0 :(得分:1)
为了使用标准API中的JPQL JOIN
或等效From.join()
方法,必须通过关系连接实体。因此,一个合理的选择是将Connection
与Node
相关联。
如果这是不可能的,您可以使用简单的连接语法:
SELECT c
FROM Connection c, Node n
WHERE (c.sourceNodeId = n.id OR c.targetNodeId = n.id)
AND n.graphId = :graphId
或者在标准API中,例如:
final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Connection> query = cb.createQuery(Connection.class);
Root<Connection> connection = query.from(Connection.class);
Root<Node> node = query.from(Node.class);
TypedQuery<Connection> typedQuery = entityManager.createQuery(query
.select(connection)
.where(
cb.and(
cb.or(
cb.equal(connection.get("sourceNodeId"), node.get("id")),
cb.equal(connection.get("targetNodeId"), node.get("id"))
),
cb.equal(node.get("graphId"), someId)
)
);
return typedQuery.getResultList();