标准 JPA 2,包含 3 个表格

2022-09-01 18:25:34

我正在尝试创建一个条件来从3个表(关联,更新和详细信息)中检索一些对象。“详细信息”引用“关联和”更新“,”更新“引用”详细信息“列表。我的目标是检索一个更新列表,该列表在给定关联 ID 的情况下,在指定字段中至少具有 null 值的详细信息。在JPQL中很容易做到,但客户说这必须用标准进行编码。

我的JPQL是:

public List<Update> getUpdates(long associateId) {
    TypedQuery<Update> query = em.createQuery("select distinct u from Update u, Detail dt, Associate a "
        + "where dt.update = u and dt.associate = a and a.associateId = :id and "
        + "dt.ack_date is null", Update.class);
    query.setParameter("id", associateId);
    return query.getResultList();
}

我尝试了以下方法,但它只是返回了数据库中的所有更新:

public List<Update> getUpdates(long associateId) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Update> query = builder.createQuery(Update.class);

    Root<Update> fromUpdates = query.from(Update.class);
    Root<Associate> fromAssociate = query.from(Associate.class);
    Root<Detail> fromDetail = query.from(Detail.class);

    Join<Detail, Associate> associateJoin = fromDetail.join("associate");
    Join<Detail, Update> updateJoin = fromDetail.join("update");

    TypedQuery<Update> typedQuery = em.createQuery(query

            .select(fromUpdates)
            .where(builder.and(
                    builder.equal(fromAssociate.get("associateId"), associateId),
                    builder.equal(fromDetail.get("associate"), associateJoin),
                    builder.equal(fromDetail.get("update"), updateJoin),
                    builder.isNull(fromDetail.get("ack_date"))
            ))

            .orderBy(builder.asc(fromUpdates.get("updateId")))
            .distinct(true)
    );

    return typedQuery.getResultList();
}

任何人都可以帮我吗?我搜索了一下,但找不到任何包含3个实体的示例。


答案 1

每个连接都会将您从左侧类型参数带到右侧类型参数。因此,我的代码(第二行)的联接从 开始,即 一个 ,并创建一些在幕后也是 .由此,您可以构建其他联接。试试这个(代码未测试):detailsfromUpdatesPath<Update>Path<Detail>

Root<Update> fromUpdates = query.from(Update.class);
Join<Update, Detail> details = fromUpdates.join("details");
Join<Detail, Associate> associate = details.join("associate");
List<Predicate> conditions = new ArrayList();
conditions.add(builder.equal(associate.get("associateId"), associateId));
conditions.add(builder.isNull(details.get("ack_date")));

TypedQuery<Update> typedQuery = em.createQuery(query
        .select(fromUpdates)
        .where(conditions.toArray(new Predicate[] {}))
        .orderBy(builder.asc(fromUpdates.get("updateId")))
        .distinct(true)
);

答案 2

对于涉及的三个表。

CriteriaBuilder builder = theEntityManager.getCriteriaBuilder();CriteriaQuery query1 = builder.createQuery(BasicMemberInfo.class);

    Root<Table1> table1 = query1.from(Table1.class); 
    Root<Table2> table2 = query1.from(Table2.class);
    Root<Table3> table3 = query1.from(Table3.class);

   List<Predicate> conditions = new ArrayList();
    conditions.add(builder.equal(table3.get("Table1").get("memberId"), table1.get("memberId")));
    conditions.add(builder.equal(table2.get("tableid").get("memberId"), table1.get("memberId")));
    conditions.add(builder.equal(table2.get("indicator"), 'Y'));
    conditions.add(builder.equal(table3.get("StatusCd"), "YES"));

    TypedQuery<BasicCustInfo> typedQuery = theEntityManager.createQuery(
            query1.multiselect(table1.get("memberId"), table2.get("AcctId"))
            .where(conditions.toArray(new Predicate[] {}))
    );

    List<BasicMemberInfo> custList = typedQuery.getResultList();

公共类 BasicMemberInfo {

String memberId;
String AcctId;

public BasicCustInfo() {
    // TODO Auto-generated constructor stub
}

public BasicMemberInfo( BigDecimal memberId,String AcctId ) {
    this.memberId = memberId;
    this.AcctId = AcctId;
}

public BigDecimal getmemberId() {
    return memberId;
}
public void setmemberId(BigDecimal memberId) {
    memberId = memberId;
}
public String getAcctId() {
    return AcctId;
}
public void setAcctId(String AcctId) {
    AcctId = AcctId;
}

}


推荐