|
1 | 1 | package com.introproventures.graphql.jpa.query.example;
|
2 | 2 |
|
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.tuple; |
| 5 | + |
| 6 | +import jakarta.persistence.EntityGraph; |
| 7 | +import jakarta.persistence.EntityManager; |
| 8 | +import jakarta.persistence.PersistenceContext; |
| 9 | +import jakarta.persistence.TypedQuery; |
| 10 | +import jakarta.persistence.criteria.CriteriaBuilder; |
| 11 | +import jakarta.persistence.criteria.CriteriaQuery; |
| 12 | +import jakarta.persistence.criteria.Join; |
| 13 | +import jakarta.persistence.criteria.Root; |
| 14 | +import jakarta.persistence.metamodel.EntityType; |
| 15 | +import jakarta.persistence.metamodel.SingularAttribute; |
| 16 | +import java.util.List; |
| 17 | +import org.activiti.cloud.services.query.model.ProcessInstanceEntity; |
| 18 | +import org.activiti.cloud.services.query.model.ProcessVariableEntity; |
| 19 | +import org.hibernate.jpa.SpecHints; |
3 | 20 | import org.junit.jupiter.api.Disabled;
|
4 | 21 | import org.junit.jupiter.api.Test;
|
5 | 22 | import org.springframework.boot.test.context.SpringBootTest;
|
6 | 23 | import org.springframework.context.annotation.Import;
|
| 24 | +import org.springframework.transaction.annotation.Transactional; |
7 | 25 | import org.testcontainers.junit.jupiter.Testcontainers;
|
8 | 26 |
|
9 | 27 | @Disabled
|
|
12 | 30 | @Import(TestApplication.class)
|
13 | 31 | class ApplicationTest {
|
14 | 32 |
|
| 33 | + @PersistenceContext |
| 34 | + private EntityManager entityManager; |
| 35 | + |
15 | 36 | @Test
|
16 | 37 | void contextLoads() {}
|
| 38 | + |
| 39 | + @Test |
| 40 | + @Transactional |
| 41 | + void criteriaQueryVariableValueLoad() { |
| 42 | + EntityType<ProcessInstanceEntity> entityType = entityManager.getMetamodel().entity(ProcessInstanceEntity.class); |
| 43 | + SingularAttribute<?, ?> parentIdAttribute = entityType.getId(Object.class); |
| 44 | + |
| 45 | + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); |
| 46 | + CriteriaQuery<Object[]> query = cb.createQuery(Object[].class); |
| 47 | + Root<?> from = query.from(entityType); |
| 48 | + |
| 49 | + from.alias("owner"); |
| 50 | + |
| 51 | + Join<?, ?> join = from.join("variables").on(from.get(parentIdAttribute.getName()).in("0")); |
| 52 | + |
| 53 | + query.multiselect(from.get(parentIdAttribute.getName()), join.alias("variables")); |
| 54 | + |
| 55 | + EntityGraph<ProcessVariableEntity> entityGraph = |
| 56 | + this.entityManager.createEntityGraph(ProcessVariableEntity.class); |
| 57 | + entityGraph.addAttributeNodes("name", "value"); |
| 58 | + |
| 59 | + TypedQuery<Object[]> typedQuery = entityManager |
| 60 | + .createQuery(query.distinct(true)) |
| 61 | + .setHint(SpecHints.HINT_SPEC_LOAD_GRAPH, entityGraph); |
| 62 | + |
| 63 | + List<Object[]> resultList = typedQuery.getResultList(); |
| 64 | + |
| 65 | + assertThat(resultList) |
| 66 | + .isNotEmpty() |
| 67 | + .extracting(record -> (ProcessVariableEntity) record[1]) |
| 68 | + .extracting(ProcessVariableEntity::getName, ProcessVariableEntity::getValue) |
| 69 | + .containsOnly( |
| 70 | + tuple("approverlist", List.of("andrelaksmana")), |
| 71 | + tuple("moduleid", "LBU"), |
| 72 | + tuple("nullable", null), |
| 73 | + tuple("applicationDate", "2023-10-22T00:00:00.000+0000"), |
| 74 | + tuple("applicationId", "232951752337576"), |
| 75 | + tuple("isApproved", true) |
| 76 | + ); |
| 77 | + } |
17 | 78 | }
|
0 commit comments