Skip to content

Commit

Permalink
jakartaee/persistence#457 - add union/intersect/except to JPQL and cr…
Browse files Browse the repository at this point in the history
…iteria

Fixed issue with ResultType.PARTIAL in compound query.

Signed-off-by: Tomáš Kraus <[email protected]>
  • Loading branch information
Tomas-Kraus authored and lukasj committed Nov 30, 2023
1 parent 384556f commit a7caf4c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public static Test suite() {
new UnionCriteriaQueryTest("testExceptAllWithNoSelection"),
new UnionCriteriaQueryTest("testUnionWithEntityParameterInSelection"),
new UnionCriteriaQueryTest("testUnionWithMultiselectEntityParametersInSelection"),
new UnionCriteriaQueryTest("testUnionWithMultiselectEntityParametersInSelectionResultTypePartial"),
new UnionCriteriaQueryTest("testUnionWithMultiselectEntityInSelection")
);
}
Expand Down Expand Up @@ -415,8 +416,37 @@ public void testUnionWithMultiselectEntityParametersInSelection() {
}
}

// TODO testUnionWithMultiselectEntityParametersInSelection without supporting constructor
// to validate ResultType.PARTIAL with compound query
// Selects partial content of existing Trainer constructor prototype
// This shall trigger compound query builder with ResultType.PARTIAL.
@SuppressWarnings("deprecation")
public void testUnionWithMultiselectEntityParametersInSelectionResultTypePartial() {
try (EntityManager em = emf.createEntityManager()) {
EntityTransaction et = em.getTransaction();
try {
et.begin();
CriteriaBuilder cb = em.getCriteriaBuilder();

CriteriaQuery<Trainer> q1 = cb.createQuery(Trainer.class);
Root<Trainer> root1 = q1.from(Trainer.class);
q1.multiselect(root1.get("id"), root1.get("name"));
q1.where(cb.equal(root1.get("name"), cb.parameter(String.class, "name1")));

CriteriaQuery<Trainer> q2 = cb.createQuery(Trainer.class);
Root<Trainer> root2 = q2.from(Trainer.class);
q2.multiselect(root2.get("id"), root2.get("name"));
q2.where(cb.equal(root2.get("name"), cb.parameter(String.class, "name2")));

TypedQuery<Trainer> query = em.createQuery(cb.union(q1, q2));
query.setParameter("name1", TRAINERS[1].getName());
query.setParameter("name2", TRAINERS[2].getName());
List<Trainer> trainers = query.getResultList();
assertEquals(2, trainers.size());
} catch (Exception e) {
et.rollback();
throw e;
}
}
}

public void testUnionWithMultiselectEntityInSelection() {
try (EntityManager em = emf.createEntityManager()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,20 +498,21 @@ protected ReadAllQuery createCompoundQuery(boolean toReportQuery) {

if (this.queryResult.equals(ResultType.PARTIAL)) {
ReadAllQuery raq;
// TODO: allow force of ReportQuery creation for usage in UNION
// TODO: In progress, code is not working and tested !!!
// See testUnionWithMultiselectEntityParametersInSelection TODO in UnionCriteriaQueryTest
@SuppressWarnings("unchecked")
List<SelectionImpl<?>> selectionItems = (List<SelectionImpl<?>>) (List<?>) this.selection.getCompoundSelectionItems();
if (toReportQuery) {
//raq = createReportQuery(this.queryType);
raq = createReportQueryWithItem(this.queryType);
raq = createReportQuery(this.queryType);
for (SelectionImpl<?> nested : selectionItems) {
((ReportQuery) raq).addAttribute(nested.getAlias(), nested.getCurrentNode(), nested.getJavaType());
}
} else {
raq = new ReadAllQuery(this.queryType);
}
// TODO: double check whether this may be avoided
// Partial object queries are not allowed to maintain the cache or be edited. Requires dontMaintainCache().
// See hasPartialAttributeExpressions() in ObjectLevelReadQuery#prepareQuery()
raq.dontMaintainCache();
// TODO: ROOTS?
for (Selection selection : this.selection.getCompoundSelectionItems()) {
raq.addPartialAttribute(((SelectionImpl) selection).currentNode);
for (SelectionImpl<?> selection : selectionItems) {
raq.addPartialAttribute((selection).currentNode);
}
raq.setExpressionBuilder(((InternalSelection) this.selection.getCompoundSelectionItems().get(0)).getCurrentNode().getBuilder());
query = raq;
Expand Down

0 comments on commit a7caf4c

Please sign in to comment.