Skip to content

Commit

Permalink
fix the getOrders() method (#2829)
Browse files Browse the repository at this point in the history
Co-authored-by: Kareem Elzayat <kareem.elzayat@uberall.com>
  • Loading branch information
kareemelzayat and elzayakm authored Mar 8, 2024
1 parent 3755130 commit 942d1cb
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -620,14 +620,14 @@ protected final <R> void collectCountOf(CriteriaBuilder criteriaBuilder, S sessi

private <T> void bindCriteriaSort(CriteriaQuery<T> criteriaQuery, Root<?> root, CriteriaBuilder builder, @NonNull Sort sort) {
List<Order> orders = new ArrayList<>();
Path<?> path = null;
for (Sort.Order order : sort.getOrderBy()) {
Path<String> path = root.get(order.getProperty());
Expression expression = order.isIgnoreCase() ? builder.lower(path) : path;
if (order.getDirection() == Sort.Order.Direction.DESC) {
orders.add(builder.desc(expression));
} else {
orders.add(builder.asc(expression));
String[] propertyArray = order.getProperty().split("\\.");
for (String property : propertyArray) {
path = path == null ? root.get(property) : path.get(property);
}
Expression<?> expression = order.isIgnoreCase() && path != null ? builder.lower(path.type().as(String.class)) : path;
orders.add(order.isAscending() ? builder.asc(expression) : builder.desc(expression));
}
criteriaQuery.orderBy(orders);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import io.micronaut.data.runtime.convert.DataConversionService;
import io.micronaut.data.runtime.operations.ExecutorAsyncOperations;
import io.micronaut.data.runtime.operations.ExecutorAsyncOperationsSupportingCriteria;
import io.micronaut.data.runtime.operations.ExecutorReactiveOperations;
import io.micronaut.data.runtime.operations.ExecutorReactiveOperationsSupportingCriteria;
import io.micronaut.transaction.TransactionOperations;
import jakarta.inject.Named;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@
import jakarta.persistence.criteria.CriteriaDelete;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.CriteriaUpdate;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.Order;
import jakarta.persistence.criteria.Path;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -555,9 +557,14 @@ protected <K> CriteriaUpdateBuilder<K> getCriteriaUpdateBuilder(MethodInvocation

private List<Order> getOrders(Sort sort, Root<?> root, CriteriaBuilder cb) {
List<Order> orders = new ArrayList<>();
Path<?> path = null;
for (Sort.Order order : sort.getOrderBy()) {
Path<Object> propertyPath = root.get(order.getProperty());
orders.add(order.isAscending() ? cb.asc(propertyPath) : cb.desc(propertyPath));
String[] propertyArray = order.getProperty().split("\\.");
for (String property : propertyArray) {
path = path == null ? root.get(property) : path.get(property);
}
Expression<?> expression = order.isIgnoreCase() && path != null ? cb.lower(path.type().as(String.class)) : path;
orders.add(order.isAscending() ? cb.asc(expression) : cb.desc(expression));
}
return orders;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package example

import io.micronaut.data.annotation.Repository
import io.micronaut.data.repository.CrudRepository
import io.micronaut.data.repository.jpa.kotlin.CoroutineJpaSpecificationExecutor

@Repository
interface ChildSuspendRepository : CrudRepository<Child, Int>, CoroutineJpaSpecificationExecutor<Child>
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ import io.micronaut.data.repository.jpa.criteria.PredicateSpecification
import io.micronaut.data.repository.jpa.criteria.QuerySpecification
import io.micronaut.data.runtime.criteria.get
import io.micronaut.data.runtime.criteria.joinMany
import io.micronaut.data.runtime.criteria.joinOne
import io.micronaut.data.runtime.criteria.query
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import jakarta.inject.Inject
import jakarta.persistence.criteria.Join
import jakarta.persistence.criteria.JoinType
import jakarta.persistence.criteria.ListJoin
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking
import org.hibernate.query.sqm.tree.domain.SqmListJoin
import org.hibernate.query.sqm.tree.domain.SqmSingularJoin
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.MethodOrderer
Expand All @@ -37,9 +41,12 @@ class HibernateTxTest {
private lateinit var repositoryForCustomDb: ParentRepositoryForCustomDb
@Inject
private lateinit var service: PersonSuspendRepositoryService
@Inject
private lateinit var childRepository: ChildSuspendRepository

@BeforeEach
fun cleanup() {
childRepository.deleteAll()
repository.deleteAll()
repositoryForCustomDb.deleteAll()
}
Expand Down Expand Up @@ -284,4 +291,37 @@ class HibernateTxTest {
}
}

@Test
fun `coroutineCriteria failing`() {
runBlocking {
val parent1 = Parent("abc", mutableListOf()).apply { children.add(Child("cde", parent = this)) }
val parent2 = Parent("cde", mutableListOf()).apply { children.add(Child("abc", parent = this)) }
repositorySuspended.save(parent1)
repositorySuspended.save(parent2)

val query = query<Child> {
val parent: Join<Child, Parent?> = if (query.resultType.kotlin != Long::class) {
root.fetch<Child, Parent?>("parent") as SqmSingularJoin<Child, Parent?>
} else {
root.joinOne(Child::parent)
}

where {
or {
root[Child::name] inList listOf("abc", "cde")
parent[Parent::name] inList listOf("abc", "cde")
}
}
}

val resultAsc = childRepository.findAll(query, Sort.of(Sort.Order("parent.name", Sort.Order.Direction.ASC, false))).toList()
assertEquals(parent1.name, resultAsc.first().parent?.name)
assertEquals(parent2.name, resultAsc.last().parent?.name)

val resultDesc = childRepository.findAll(query, Sort.of(Sort.Order("parent.name", Sort.Order.Direction.DESC, false))).toList()
assertEquals(parent2.name, resultDesc.first().parent?.name)
assertEquals(parent1.name, resultDesc.last().parent?.name)
}
}

}

2 comments on commit 942d1cb

@elzayakm
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dstepanov I see the sonarqube step is failing here. Not sure I understand why, and if that is affecting the deployment

@dstepanov
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't affect the snapshot release

Please sign in to comment.