|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.query.hql.instantiation; |
| 8 | + |
| 9 | +import java.io.Serializable; |
| 10 | + |
| 11 | +import org.hibernate.annotations.Imported; |
| 12 | + |
| 13 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 14 | +import org.hibernate.testing.orm.junit.Jira; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 17 | +import org.junit.jupiter.api.AfterAll; |
| 18 | +import org.junit.jupiter.api.AfterEach; |
| 19 | +import org.junit.jupiter.api.BeforeAll; |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import jakarta.persistence.Entity; |
| 24 | +import jakarta.persistence.Id; |
| 25 | +import jakarta.persistence.MappedSuperclass; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | + |
| 29 | +/** |
| 30 | + * @author Marco Belladelli |
| 31 | + */ |
| 32 | +@DomainModel(annotatedClasses = { |
| 33 | + InstantiationWithGenericsExpressionTest.AbstractEntity.class, |
| 34 | + InstantiationWithGenericsExpressionTest.ConcreteEntity.class, |
| 35 | + InstantiationWithGenericsExpressionTest.ConstructorDto.class, |
| 36 | + InstantiationWithGenericsExpressionTest.InjectionDto.class, |
| 37 | +}) |
| 38 | +@SessionFactory |
| 39 | +@Jira("https://hibernate.atlassian.net/browse/HHH-18218") |
| 40 | +public class InstantiationWithGenericsExpressionTest { |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + public void setUp(SessionFactoryScope scope) { |
| 44 | + scope.inTransaction( session -> { |
| 45 | + final ConcreteEntity entity = new ConcreteEntity(); |
| 46 | + entity.setId( 1 ); |
| 47 | + entity.setGen( 1L ); |
| 48 | + entity.setData( "entity_1" ); |
| 49 | + session.persist( entity ); |
| 50 | + } ); |
| 51 | + } |
| 52 | + |
| 53 | + @AfterEach |
| 54 | + public void tearDown(SessionFactoryScope scope) { |
| 55 | + scope.inTransaction( session -> session.createMutationQuery( "delete from ConcreteEntity" ).executeUpdate() ); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testConstructorBinaryExpression(SessionFactoryScope scope) { |
| 60 | + scope.inTransaction( session -> assertThat( session.createQuery( |
| 61 | + "select new ConstructorDto(e.gen+e.gen, e.data) from ConcreteEntity e", |
| 62 | + ConstructorDto.class |
| 63 | + ).getSingleResult() ).extracting( ConstructorDto::getGen, ConstructorDto::getData ) |
| 64 | + .containsExactly( 2L, "entity_1" ) ); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testImplicitConstructorBinaryExpression(SessionFactoryScope scope) { |
| 69 | + scope.inTransaction( session -> assertThat( session.createQuery( |
| 70 | + "select e.gen+e.gen, e.data from ConcreteEntity e", |
| 71 | + ConstructorDto.class |
| 72 | + ).getSingleResult() ).extracting( ConstructorDto::getGen, ConstructorDto::getData ) |
| 73 | + .containsExactly( 2L, "entity_1" ) ); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testInjectionBinaryExpression(SessionFactoryScope scope) { |
| 78 | + scope.inTransaction( session -> assertThat( session.createQuery( |
| 79 | + "select new InjectionDto(e.gen+e.gen as gen, e.data as data) from ConcreteEntity e", |
| 80 | + InjectionDto.class |
| 81 | + ).getSingleResult() ).extracting( InjectionDto::getGen, InjectionDto::getData ) |
| 82 | + .containsExactly( 2L, "entity_1" ) ); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testConstructorUnaryExpression(SessionFactoryScope scope) { |
| 87 | + scope.inTransaction( session -> assertThat( session.createQuery( |
| 88 | + "select new ConstructorDto(-e.gen, e.data) from ConcreteEntity e", |
| 89 | + ConstructorDto.class |
| 90 | + ).getSingleResult() ).extracting( ConstructorDto::getGen, ConstructorDto::getData ) |
| 91 | + .containsExactly( -1L, "entity_1" ) ); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testImplicitConstructorUnaryExpression(SessionFactoryScope scope) { |
| 96 | + scope.inTransaction( session -> assertThat( session.createQuery( |
| 97 | + "select -e.gen, e.data from ConcreteEntity e", |
| 98 | + ConstructorDto.class |
| 99 | + ).getSingleResult() ).extracting( ConstructorDto::getGen, ConstructorDto::getData ) |
| 100 | + .containsExactly( -1L, "entity_1" ) ); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testInjectionUnaryExpression(SessionFactoryScope scope) { |
| 105 | + scope.inTransaction( session -> assertThat( session.createQuery( |
| 106 | + "select new InjectionDto(-e.gen as gen, e.data as data) from ConcreteEntity e", |
| 107 | + InjectionDto.class |
| 108 | + ).getSingleResult() ).extracting( InjectionDto::getGen, InjectionDto::getData ) |
| 109 | + .containsExactly( -1L, "entity_1" ) ); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testConstructorFunction(SessionFactoryScope scope) { |
| 114 | + scope.inTransaction( session -> { |
| 115 | + assertThat( session.createQuery( |
| 116 | + "select new ConstructorDto(abs(e.gen), e.data) from ConcreteEntity e", |
| 117 | + ConstructorDto.class |
| 118 | + ).getSingleResult() ).extracting( ConstructorDto::getGen, ConstructorDto::getData ) |
| 119 | + .containsExactly( 1L, "entity_1" ); |
| 120 | + } ); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testImplicitFunction(SessionFactoryScope scope) { |
| 125 | + scope.inTransaction( session -> { |
| 126 | + assertThat( session.createQuery( |
| 127 | + "select abs(e.gen), e.data from ConcreteEntity e", |
| 128 | + ConstructorDto.class |
| 129 | + ).getSingleResult() ).extracting( ConstructorDto::getGen, ConstructorDto::getData ) |
| 130 | + .containsExactly( 1L, "entity_1" ); |
| 131 | + } ); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void testInjectionFunction(SessionFactoryScope scope) { |
| 136 | + scope.inTransaction( session -> { |
| 137 | + assertThat( session.createQuery( |
| 138 | + "select new InjectionDto(abs(e.gen) as gen, e.data as data) from ConcreteEntity e", |
| 139 | + InjectionDto.class |
| 140 | + ).getSingleResult() ).extracting( InjectionDto::getGen, InjectionDto::getData ) |
| 141 | + .containsExactly( 1L, "entity_1" ); |
| 142 | + } ); |
| 143 | + } |
| 144 | + |
| 145 | + @MappedSuperclass |
| 146 | + static abstract class AbstractEntity<K extends Serializable> { |
| 147 | + @Id |
| 148 | + protected Integer id; |
| 149 | + |
| 150 | + protected K gen; |
| 151 | + |
| 152 | + public Integer getId() { |
| 153 | + return id; |
| 154 | + } |
| 155 | + |
| 156 | + public void setId(final Integer id) { |
| 157 | + this.id = id; |
| 158 | + } |
| 159 | + |
| 160 | + public K getGen() { |
| 161 | + return gen; |
| 162 | + } |
| 163 | + |
| 164 | + public void setGen(final K gen) { |
| 165 | + this.gen = gen; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + @Entity(name = "ConcreteEntity") |
| 170 | + static class ConcreteEntity extends AbstractEntity<Long> { |
| 171 | + protected String data; |
| 172 | + |
| 173 | + public String getData() { |
| 174 | + return data; |
| 175 | + } |
| 176 | + |
| 177 | + public void setData(String data) { |
| 178 | + this.data = data; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + @Imported |
| 183 | + public static class ConstructorDto { |
| 184 | + private final Long gen; |
| 185 | + |
| 186 | + private final String data; |
| 187 | + |
| 188 | + public ConstructorDto(Long gen, String data) { |
| 189 | + this.gen = gen; |
| 190 | + this.data = data; |
| 191 | + } |
| 192 | + |
| 193 | + public Long getGen() { |
| 194 | + return gen; |
| 195 | + } |
| 196 | + |
| 197 | + public String getData() { |
| 198 | + return data; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + @Imported |
| 203 | + public static class InjectionDto { |
| 204 | + private long gen; |
| 205 | + |
| 206 | + private String data; |
| 207 | + |
| 208 | + public long getGen() { |
| 209 | + return gen; |
| 210 | + } |
| 211 | + |
| 212 | + public void setGen(final long gen) { |
| 213 | + this.gen = gen; |
| 214 | + } |
| 215 | + |
| 216 | + public String getData() { |
| 217 | + return data; |
| 218 | + } |
| 219 | + |
| 220 | + public void setData(String data) { |
| 221 | + this.data = data; |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments