|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.query.criteria.plan; |
| 6 | + |
| 7 | +import org.hibernate.cfg.AvailableSettings; |
| 8 | +import org.hibernate.engine.spi.SessionImplementor; |
| 9 | +import org.hibernate.query.criteria.HibernateCriteriaBuilder; |
| 10 | +import org.hibernate.query.criteria.JpaCriteriaQuery; |
| 11 | +import org.hibernate.query.criteria.JpaRoot; |
| 12 | +import org.hibernate.testing.orm.junit.*; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import jakarta.persistence.*; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +@DomainModel(annotatedClasses = {CriteriaPlanTest.Author.class, CriteriaPlanTest.Book.class}) |
| 22 | +@ServiceRegistry( |
| 23 | + settings = { |
| 24 | + @Setting(name = AvailableSettings.CRITERIA_COPY_TREE, value = "false"), |
| 25 | + @Setting(name = AvailableSettings.CRITERIA_PLAN_CACHE_ENABLED, value = "true"), |
| 26 | + } |
| 27 | +) |
| 28 | +@SessionFactory |
| 29 | +class CriteriaPlanTest { |
| 30 | + |
| 31 | + @Test |
| 32 | + void criteriaPlanCacheWithEntityParameters(SessionFactoryScope scope) { |
| 33 | + scope.inTransaction( session -> { |
| 34 | + final Author author = populateData(session); |
| 35 | + |
| 36 | + assertThat(runQuery(session, author)).hasSize(5); |
| 37 | + assertThat(runQuery(session, author)).hasSize(5); |
| 38 | + } ); |
| 39 | + } |
| 40 | + |
| 41 | + private static List<Book> runQuery(SessionImplementor session, Author author) { |
| 42 | + final HibernateCriteriaBuilder cb = session.getCriteriaBuilder(); |
| 43 | + final JpaCriteriaQuery<Book> q = cb.createQuery(Book.class); |
| 44 | + final JpaRoot<Book> root = q.from(Book.class); |
| 45 | + q.select(root); |
| 46 | + q.where(cb.equal(root.get("author"), author)); |
| 47 | + return session.createQuery(q).getResultList(); |
| 48 | + } |
| 49 | + |
| 50 | + public Author populateData(SessionImplementor entityManager) { |
| 51 | + final Author author = new Author(); |
| 52 | + author.name = "David Gourley"; |
| 53 | + entityManager.persist(author); |
| 54 | + |
| 55 | + for (int i = 0; i < 5; i++) { |
| 56 | + final Book book = new Book(); |
| 57 | + book.name = "HTTP Definitive guide " + i; |
| 58 | + book.author = author; |
| 59 | + entityManager.persist(book); |
| 60 | + author.books.add(book); |
| 61 | + } |
| 62 | + |
| 63 | + return author; |
| 64 | + } |
| 65 | + |
| 66 | + @Entity(name = "Author") |
| 67 | + @Table(name = "Author") |
| 68 | + public static class Author { |
| 69 | + @Id |
| 70 | + @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 71 | + public Long authorId; |
| 72 | + |
| 73 | + @Column |
| 74 | + public String name; |
| 75 | + |
| 76 | + @OneToMany(fetch = FetchType.LAZY, mappedBy = "author") |
| 77 | + public List<Book> books = new ArrayList<>(); |
| 78 | + |
| 79 | + @Override |
| 80 | + public boolean equals(Object o) { |
| 81 | + if (this == o) { |
| 82 | + return true; |
| 83 | + } |
| 84 | + if (o == null || getClass() != o.getClass()) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + final Author author = (Author) o; |
| 89 | + return authorId.equals(author.authorId); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public int hashCode() { |
| 94 | + return authorId.hashCode(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @org.hibernate.annotations.Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE) |
| 99 | + @Entity(name = "Book") |
| 100 | + @Table(name = "Book") |
| 101 | + public static class Book { |
| 102 | + @Id |
| 103 | + @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 104 | + public Long bookId; |
| 105 | + |
| 106 | + @Column |
| 107 | + public String name; |
| 108 | + |
| 109 | + @ManyToOne(fetch = FetchType.LAZY, optional = false) |
| 110 | + @JoinColumn(name = "author_id", nullable = false) |
| 111 | + public Author author; |
| 112 | + } |
| 113 | +} |
0 commit comments