|
| 1 | +package org.hibernate.orm.test.jpa.query; |
| 2 | + |
| 3 | +import java.time.Instant; |
| 4 | +import java.util.Date; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import org.hibernate.orm.test.jpa.Wallet; |
| 8 | +import org.hibernate.orm.test.jpa.Wallet_; |
| 9 | + |
| 10 | +import org.hibernate.testing.TestForIssue; |
| 11 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 12 | +import org.hibernate.testing.orm.junit.Jpa; |
| 13 | +import org.junit.jupiter.api.AfterEach; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import jakarta.persistence.Entity; |
| 17 | +import jakarta.persistence.GeneratedValue; |
| 18 | +import jakarta.persistence.Id; |
| 19 | +import jakarta.persistence.Query; |
| 20 | +import jakarta.persistence.TypedQuery; |
| 21 | +import jakarta.persistence.criteria.CriteriaBuilder; |
| 22 | +import jakarta.persistence.criteria.CriteriaQuery; |
| 23 | +import jakarta.persistence.criteria.ParameterExpression; |
| 24 | +import jakarta.persistence.criteria.Root; |
| 25 | + |
| 26 | +import static org.junit.Assert.assertEquals; |
| 27 | + |
| 28 | + |
| 29 | +@Jpa( |
| 30 | + annotatedClasses = { Wallet.class, ReuseCriteriaWithMixedParametersTest.Person.class } |
| 31 | +) |
| 32 | +@TestForIssue(jiraKey = "HHH-15142") |
| 33 | +public class ReuseCriteriaWithMixedParametersTest { |
| 34 | + |
| 35 | + @AfterEach |
| 36 | + public void tearDown(EntityManagerFactoryScope scope) { |
| 37 | + scope.inTransaction( |
| 38 | + entityManager -> |
| 39 | + entityManager.createQuery( "delete from Person" ).executeUpdate() |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void cqReuse(EntityManagerFactoryScope scope) { |
| 45 | + scope.inTransaction( entityManager -> { |
| 46 | + final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); |
| 47 | + final CriteriaQuery<Wallet> criteriaQuery = criteriaBuilder.createQuery( Wallet.class ); |
| 48 | + final Root<Wallet> root = criteriaQuery.from( Wallet.class ); |
| 49 | + |
| 50 | + final ParameterExpression<String> stringValueParameter = criteriaBuilder.parameter( String.class ); |
| 51 | + |
| 52 | + criteriaQuery.where( |
| 53 | + criteriaBuilder.like( |
| 54 | + root.get( Wallet_.model ), |
| 55 | + stringValueParameter |
| 56 | + ), |
| 57 | + criteriaBuilder.lessThan( |
| 58 | + root.get( Wallet_.marketEntrance ), |
| 59 | + criteriaBuilder.literal( Date.from( Instant.EPOCH ) ) |
| 60 | + ) |
| 61 | + ); |
| 62 | + |
| 63 | + Query query = entityManager.createQuery( criteriaQuery ); |
| 64 | + query.setParameter( stringValueParameter, "Z%" ); |
| 65 | + |
| 66 | + query.getResultList(); |
| 67 | + |
| 68 | + query = entityManager.createQuery( criteriaQuery ); |
| 69 | + query.setParameter( stringValueParameter, "A%" ); |
| 70 | + |
| 71 | + query.getResultList(); |
| 72 | + |
| 73 | + } ); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void likeCqReuse(EntityManagerFactoryScope scope) { |
| 78 | + scope.inTransaction( entityManager -> { |
| 79 | + |
| 80 | + final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); |
| 81 | + final CriteriaQuery<Wallet> criteriaQuery = criteriaBuilder.createQuery( Wallet.class ); |
| 82 | + final Root<Wallet> root = criteriaQuery.from( Wallet.class ); |
| 83 | + |
| 84 | + final ParameterExpression<String> stringValueParameter = criteriaBuilder.parameter( String.class ); |
| 85 | + |
| 86 | + criteriaQuery.where( |
| 87 | + criteriaBuilder.like( |
| 88 | + root.get( Wallet_.model ), |
| 89 | + stringValueParameter, |
| 90 | + '/' |
| 91 | + ) |
| 92 | + ); |
| 93 | + |
| 94 | + Query query = entityManager.createQuery( criteriaQuery ); |
| 95 | + query.setParameter( stringValueParameter, "Z%" ); |
| 96 | + |
| 97 | + query.getResultList(); |
| 98 | + |
| 99 | + query = entityManager.createQuery( criteriaQuery ); |
| 100 | + query.setParameter( stringValueParameter, "A%" ); |
| 101 | + |
| 102 | + query.getResultList(); |
| 103 | + |
| 104 | + } ); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void predicateReuse(EntityManagerFactoryScope scope) { |
| 109 | + scope.inTransaction( entityManager -> { |
| 110 | + final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); |
| 111 | + final CriteriaQuery<Wallet> criteriaQuery = criteriaBuilder.createQuery( Wallet.class ); |
| 112 | + final Root<Wallet> root = criteriaQuery.from( Wallet.class ); |
| 113 | + |
| 114 | + final ParameterExpression<String> stringValueParameter = criteriaBuilder.parameter( String.class ); |
| 115 | + final ParameterExpression<Date> dateValueParameter = criteriaBuilder.parameter( Date.class ); |
| 116 | + |
| 117 | + criteriaQuery.where( |
| 118 | + criteriaBuilder.like( |
| 119 | + root.get( Wallet_.model ), |
| 120 | + stringValueParameter |
| 121 | + ) |
| 122 | + ); |
| 123 | + |
| 124 | + Query query = entityManager.createQuery( criteriaQuery ); |
| 125 | + query.setParameter( stringValueParameter, "Z%" ); |
| 126 | + |
| 127 | + query.getResultList(); |
| 128 | + |
| 129 | + criteriaQuery.where( |
| 130 | + criteriaBuilder.like( |
| 131 | + root.get( Wallet_.model ), |
| 132 | + stringValueParameter |
| 133 | + ), |
| 134 | + criteriaBuilder.lessThan( |
| 135 | + root.get( Wallet_.marketEntrance ), |
| 136 | + dateValueParameter |
| 137 | + ) |
| 138 | + ); |
| 139 | + |
| 140 | + query = entityManager.createQuery( criteriaQuery ); |
| 141 | + query.setParameter( stringValueParameter, "A%" ); |
| 142 | + query.setParameter( dateValueParameter, Date.from( Instant.EPOCH ) ); |
| 143 | + |
| 144 | + query.getResultList(); |
| 145 | + } ); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void testLikePredicate(EntityManagerFactoryScope scope) { |
| 150 | + scope.inTransaction( |
| 151 | + entityManager -> { |
| 152 | + entityManager.persist( new Person( "Person 1" ) ); |
| 153 | + entityManager.persist( new Person( "Person 2" ) ); |
| 154 | + } |
| 155 | + ); |
| 156 | + |
| 157 | + scope.inTransaction( |
| 158 | + entityManager -> { |
| 159 | + final CriteriaBuilder cb = entityManager.getCriteriaBuilder(); |
| 160 | + final CriteriaQuery<Person> personQuery = cb.createQuery( Person.class ); |
| 161 | + final Root<Person> root = personQuery.from( Person.class ); |
| 162 | + final ParameterExpression<String> pattern = cb.parameter( String.class ); |
| 163 | + CriteriaQuery<Person> criteriaQuery = personQuery |
| 164 | + .where( cb.like( |
| 165 | + root.get( "name" ), |
| 166 | + pattern, |
| 167 | + cb.literal( '\\' ) |
| 168 | + ) ); |
| 169 | + for ( int i = 0; i < 2; i++ ) { |
| 170 | + final TypedQuery<Person> query = entityManager.createQuery( criteriaQuery ); |
| 171 | + query.setParameter( pattern, "%_1" ); |
| 172 | + final List<Person> result = query.getResultList(); |
| 173 | + |
| 174 | + assertEquals( 1, result.size() ); |
| 175 | + } |
| 176 | + } |
| 177 | + ); |
| 178 | + |
| 179 | + } |
| 180 | + |
| 181 | + @Entity(name = "Person") |
| 182 | + public static class Person { |
| 183 | + @Id |
| 184 | + @GeneratedValue |
| 185 | + private Long id; |
| 186 | + |
| 187 | + private String name; |
| 188 | + |
| 189 | + public Person() { |
| 190 | + } |
| 191 | + |
| 192 | + public Person(String name) { |
| 193 | + this.name = name; |
| 194 | + } |
| 195 | + |
| 196 | + public Long getId() { |
| 197 | + return id; |
| 198 | + } |
| 199 | + |
| 200 | + public void setId(Long id) { |
| 201 | + this.id = id; |
| 202 | + } |
| 203 | + |
| 204 | + public String getName() { |
| 205 | + return name; |
| 206 | + } |
| 207 | + |
| 208 | + public void setName(String name) { |
| 209 | + this.name = name; |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | +} |
| 214 | + |
0 commit comments