Hibernate Join two unrelated table when both has Composite Primary Key

前端 未结 5 1581
自闭症患者
自闭症患者 2020-12-31 09:21

I\'m writing java application using hibernate 5.2 but without HQL

there is two table, Transactions and ResponseCode

5条回答
  •  盖世英雄少女心
    2020-12-31 10:21

    Finally I found out that

    Criteria API does not support joining unrelated entities. JPQL does not support that either. However, Hibernate supports it in HQL since 5.1. https://discourse.hibernate.org/t/join-two-table-when-both-has-composite-primary-key/1966

    Maybe there is some workarounds, but in this case, I think the better way is to use HQL instead of Criteria API.

    Here is HQL implementation code snippet (nothing was changed in entity classes)

    String hql = "FROM Transaction t \r\n" + 
                 " LEFT OUTER JOIN FETCH t.rc r \r\n" +
                 " WHERE (t.merchantID IN (:merchant_id))\r\n" +
                 " AND (t.authRequestDate BETWEEN :from AND :to)\r\n" +
                 " AND (r.rcLang = :rcLang or r.rcLang is null)\r\n";
    
    Query query =  session.createQuery(hql,Transaction.class);
    query.setParameter("merchant_id", tRequest.getMerchantList());
    query.setParameter("rcLang", tRequest.getLang());
    query.setParameter("from", dateFrom);
    query.setParameter("to", dateTo);
    
    List dbTransaction = query.getResultList();
    

提交回复
热议问题