JPA 条件 api 加入:CAN_NOT_JOIN_TO_BASIC

JPA criteria api join: CAN_NOT_JOIN_TO_BASIC

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

我做错了什么?感谢帮助!

为了使用 JPQL JOIN 或条件 API 中的等效 From.join() 方法,必须通过关系连接实体。因此,一个合理的选择是将 ConnectionNode.

联系起来

如果这不可能,您可以使用简单的连接语法:

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