JPA 2标准API:如何在不使用Metamodel的情况下从各种联接表中选择值?

JPA 2标准API:如何在不使用Metamodel的情况下从各种联接表中选择值?

问题描述:

我具有以下类型的查询,希望将其转换为jpa条件查询和以下表结构:

I have the following type of query that I wish to convert into a jpa criteria query and the following table structure:

Table A 1-->1 Table B  1<--* Table C (proceedings) *-->1 Table D(prcoeedingsstatus)
--------      --------       -------                     -------
 aID           bID           cID                         dID
 ...           ....          timestamp                   textValue
 f_bID         ....          f_bID       
                             f_dID

1 A有1 B,1 B有很多程序,每个程序都有一个程序。p>

1 A has 1 B, 1 B has many proceedings and each proceeding has a proceedingstatus.

SELECT a.*

FROM ((a LEFT JOIN b ON a.f_b = b.id)
        LEFT JOIN proceedings ON b.id = proceedings.f_b)
        RIGHT JOIN proceedingsstatus ON proceedings.f_d = proceedingsstatus.id

WHERE   d.textValue IN ("some unique text")
        AND c.timestamp BETWEEN 'somedate' AND 'anotherdate'

当我现在尝试对谓词执行以下操作时:

When I now try to do something like this for the predicates:

Predicate conditions = (root.join("tableB")
                            .joinList("proceedings")
                            .join("proceedingsstatus").get("textValue"))
                            .in(constraintList.getSelectedValues());

Predicate time = cb.between((root.join("tableB")
                            .joinList("proceedings")
                            .<Date>get("timestamp")), dt1.toDate(), dt2.toDate());

constraints = cb.and(conditions, time);

现在它根据条件选择条目A,其中至少出现1次正确的诉讼状态-谓词,如果在A的任何程序中,时间戳记与我构建的时间谓词匹配。
因此,如果至少有一个条目C属于A且D的文本值正确,那么C.timestamp对于D中错误的textValue的处理正确时,它也会选择条目A。

Right now it selects entries A where there is at least 1 occurrence of the right proceedingsstatus according to the conditions-predicate if in any of A's proceedings the 'timestamp' matches the time-predicate that I built. So it would also select an entry A when C.timestamp is correct for a proceeding with the wrong textValue in D, if there is at least one entry C belonging to A with the right textvalue in D.

如何更改它,以便仅选择A的程序状态具有正确的值并且收益时间正确?

How can I change it so that it only selects A's where the proceedingsstatus has the right value AND the time of proceeds is correct?

重用联接,而不是为每个谓词创建新联接。

Reuse the joins instead of creating new ones for each predicate.

Join proceedings = root.join("tableB").joinList("proceedings");