标准JPA 2有3个表格

时间:2013-06-17 18:52:06

标签: java jpa-2.0 jpql criteria-api

我试图创建一个标准来从3个表中检索一些对象(关联,更新和详细信息)。详细信息引用了“关联”和“更新”,“更新”引用了“详细信息”列表。我的目标是在给定Associate id的情况下检索在指定字段中至少具有null值的Detail的更新列表。在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个实体的任何示例。

3 个答案:

答案 0 :(得分:28)

每个连接都会将您从左侧类型参数带到右侧类型参数。因此,我的代码的details联接(第二行)从fromUpdates开始,即Path<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)
);

答案 1 :(得分:6)

涉及三张桌子。

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

public class 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;
}

}

答案 2 :(得分:0)

使用三个以上的表签出此测试。另外,请使用静态元模型,而不要使用直接属性名称。

    @Test
@Rollback(false)
@Transactional
public void   
fetch() {
    CriteriaBuilder cb = 
entityManager.getCriteriaBuilder();
    CriteriaQuery<Instructor> cq = 
cb.createQuery(Instructor.class);
    Root<Instructor> root = 
cq.from(Instructor.class);
    root.join(Instructor_.idProof);
    root.join(Instructor_.vehicles);
    Join<Instructor, Student> insStuJoin = 
root.join(Instructor_.students);
    insStuJoin.join(Student_.instructors);
    Join<Student, Vehicle> stuVehcileJoin.   
= insStuJoin.join(Student_.vehicles);
    Join<Vehicle, Document> 
vehicleDocumentJoin = 
stuVehcileJoin.join(Vehicle_.documents);

DataPrinters.
listDataPrinter.accept.   
(queryExecutor.fetchListForCriteriaQuery
      (cq.select(root).where
(cb.greaterThan(root.get(Instructor_.id), 2),                        
           cb.in(vehicleDocumentJoin.get
                          (Document_.name)).value("1")
.value("2").value("3")));
}