Skip to content

Commit

Permalink
Regression issue with criteria inList failing to compile (#3108)
Browse files Browse the repository at this point in the history
* Regression issue with criteria inList failing to compile

* Fix IN predicate

---------

Co-authored-by: Denis Stepanov <denis.s.stepanov@oracle.com>
  • Loading branch information
radovanradic and dstepanov authored Sep 5, 2024
1 parent 250a2e1 commit eafdc4e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
@Internal
abstract class AbstractSourcePersistentEntityJoinSupport<T, E> extends AbstractPersistentEntityJoinSupport<T, E> {

private final CriteriaBuilder criteriaBuilder;
protected final CriteriaBuilder criteriaBuilder;

AbstractSourcePersistentEntityJoinSupport(CriteriaBuilder criteriaBuilder) {
this.criteriaBuilder = criteriaBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@
import io.micronaut.data.model.Association;
import io.micronaut.data.model.jpa.criteria.PersistentAssociationPath;
import io.micronaut.data.model.jpa.criteria.PersistentEntityFrom;
import io.micronaut.data.model.jpa.criteria.impl.predicate.PersistentPropertyInPredicate;
import io.micronaut.data.processor.model.SourceAssociation;
import io.micronaut.data.processor.model.SourcePersistentEntity;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.Path;
import jakarta.persistence.criteria.Predicate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* The internal source implementation of {@link PersistentAssociationPath}.
Expand Down Expand Up @@ -62,6 +69,27 @@ final class SourcePersistentAssociationPath<Owner, E> extends AbstractSourcePers
this.alias = alias;
}

@Override
public Predicate in(Object... values) {
return in(Arrays.asList(Objects.requireNonNull(values)));
}

@Override
public Predicate in(Collection<?> values) {
List<Expression<?>> expressions = Objects.requireNonNull(values).stream().map(criteriaBuilder::literal).collect(Collectors.toList());
return new PersistentPropertyInPredicate<>(this, expressions, criteriaBuilder);
}

@Override
public Predicate in(Expression<?>... values) {
return new PersistentPropertyInPredicate<>(this, Arrays.asList(values), criteriaBuilder);
}

@Override
public Predicate in(Expression<Collection<?>> values) {
return new PersistentPropertyInPredicate<>(this, List.of(Objects.requireNonNull(values)), criteriaBuilder);
}

@Override
public PersistentEntityFrom<?, Owner> getParent() {
return parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1969,4 +1969,27 @@ interface TestRepository extends GenericRepository<Book, Long> {
findByTitleIlikeQuery.endsWith('FROM "book" book_ WHERE (LOWER(book_."title") LIKE LOWER(?))')
findByTitleNotLikeQuery.endsWith('FROM "book" book_ WHERE (NOT(book_."title" LIKE ?))')
}

void "test IN"() {
given:
def repository = buildRepository('test.TestRepository', """
import io.micronaut.data.jdbc.annotation.JdbcRepository;
import io.micronaut.data.repository.GenericRepository;
import io.micronaut.data.tck.entities.Book;
import io.micronaut.data.tck.entities.Author;
import io.micronaut.data.model.query.builder.sql.Dialect;
import java.util.List;
@JdbcRepository(dialect = Dialect.MYSQL)
interface TestRepository extends GenericRepository<Book, Long> {
List<Book> findByAuthorInList(List<Author> authors);
}
""")
def findByAuthorInListMethod = repository.findPossibleMethods("findByAuthorInList").findFirst().get()
expect:
getQuery(findByAuthorInListMethod) == "SELECT book_.`id`,book_.`author_id`,book_.`genre_id`,book_.`title`,book_.`total_pages`,book_.`publisher_id`,book_.`last_updated` FROM `book` book_ WHERE (book_.`author_id` IN (?))"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3078,7 +3078,18 @@ abstract class AbstractRepositorySpec extends Specification {
basicTypeRepository.deleteById(entity.myId)
TimeZone.setDefault(defaultTimeZone)
}


void "find by joined entity in list"() {
given:
saveSampleBooks()

when:
def author = authorRepository.findByName("Stephen King")
def books = bookRepository.findByAuthorInList(List.of(author))
then:
books.size() > 0
}

private GregorianCalendar getYearMonthDay(Date dateCreated) {
def cal = dateCreated.toCalendar()
def localDate = LocalDate.of(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,6 @@ protected Book newBook(Author author, String title, int pages) {

@Query("SELECT b FROM Book b WHERE b.author.id in :authorIds")
public abstract List<Book> findByAuthorIds(List<Long> authorIds);

public abstract List<Book> findByAuthorInList(List<Author> authors);
}

0 comments on commit eafdc4e

Please sign in to comment.