|
| 1 | +package org.hibernate.orm.test.query.hql.instantiation; |
| 2 | + |
| 3 | +import org.hibernate.annotations.Imported; |
| 4 | + |
| 5 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 6 | +import org.hibernate.testing.orm.junit.Jira; |
| 7 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 8 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 9 | +import org.junit.jupiter.api.AfterAll; |
| 10 | +import org.junit.jupiter.api.BeforeAll; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import jakarta.persistence.Entity; |
| 14 | +import jakarta.persistence.Id; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | + |
| 18 | +@DomainModel( |
| 19 | + annotatedClasses = { MatchingConstructorTest.TestEntity.class } |
| 20 | +) |
| 21 | +@SessionFactory |
| 22 | +@Jira("https://hibernate.atlassian.net/browse/HHH-18322") |
| 23 | +public class MatchingConstructorTest { |
| 24 | + |
| 25 | + @BeforeAll |
| 26 | + public void prepareData(final SessionFactoryScope scope) { |
| 27 | + scope.inTransaction( |
| 28 | + session -> session.save( new TestEntity( 1, 42, "test", 13 ) ) |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + @AfterAll |
| 33 | + public void cleanUpData(final SessionFactoryScope scope) { |
| 34 | + scope.inTransaction( |
| 35 | + session -> session.createQuery( "delete TestEntity" ).executeUpdate() |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void testImplicitConstructor(final SessionFactoryScope scope) { |
| 41 | + scope.inSession( session -> { |
| 42 | + final var result = session.createQuery( |
| 43 | + "select num, str from TestEntity", |
| 44 | + ConstructorDto.class |
| 45 | + ) |
| 46 | + .setMaxResults( 1 ).getSingleResult(); |
| 47 | + assertEquals( 42, result.getNum() ); |
| 48 | + assertEquals( "test", result.getStr() ); |
| 49 | + } ); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void testImplicitConstructorWithPrimitive(final SessionFactoryScope scope) { |
| 54 | + scope.inSession( session -> { |
| 55 | + final var result = session.createQuery( |
| 56 | + "select intValue, str from TestEntity", |
| 57 | + ConstructorWithPrimitiveDto.class |
| 58 | + ) |
| 59 | + .setMaxResults( 1 ).getSingleResult(); |
| 60 | + assertEquals( 13, result.getIntValue() ); |
| 61 | + assertEquals( "test", result.getStr() ); |
| 62 | + } ); |
| 63 | + } |
| 64 | + |
| 65 | + @Entity(name = "TestEntity") |
| 66 | + public static class TestEntity { |
| 67 | + @Id |
| 68 | + private Integer id; |
| 69 | + |
| 70 | + private Integer num; |
| 71 | + |
| 72 | + private String str; |
| 73 | + |
| 74 | + private int intValue; |
| 75 | + |
| 76 | + public TestEntity() { |
| 77 | + } |
| 78 | + |
| 79 | + public TestEntity(final Integer id, final Integer num, final String str, final int intValue) { |
| 80 | + this.id = id; |
| 81 | + this.num = num; |
| 82 | + this.str = str; |
| 83 | + this.intValue = intValue; |
| 84 | + } |
| 85 | + |
| 86 | + public Integer getId() { |
| 87 | + return id; |
| 88 | + } |
| 89 | + |
| 90 | + public void setId(final Integer id) { |
| 91 | + this.id = id; |
| 92 | + } |
| 93 | + |
| 94 | + public Integer getNum() { |
| 95 | + return num; |
| 96 | + } |
| 97 | + |
| 98 | + public void setNum(final Integer num) { |
| 99 | + this.num = num; |
| 100 | + } |
| 101 | + |
| 102 | + public String getStr() { |
| 103 | + return str; |
| 104 | + } |
| 105 | + |
| 106 | + public void setStr(final String str) { |
| 107 | + this.str = str; |
| 108 | + } |
| 109 | + |
| 110 | + public int getIntValue() { |
| 111 | + return intValue; |
| 112 | + } |
| 113 | + |
| 114 | + public void setIntValue(final int intValue) { |
| 115 | + this.intValue = intValue; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Imported |
| 120 | + public static class ConstructorDto { |
| 121 | + private final Number num; |
| 122 | + private final String str; |
| 123 | + |
| 124 | + public ConstructorDto(final Number num, final String str) { |
| 125 | + this.num = num; |
| 126 | + this.str = str; |
| 127 | + } |
| 128 | + |
| 129 | + public Number getNum() { |
| 130 | + return num; |
| 131 | + } |
| 132 | + |
| 133 | + public String getStr() { |
| 134 | + return str; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Imported |
| 139 | + public static class ConstructorWithPrimitiveDto { |
| 140 | + private final int intValue; |
| 141 | + private final String str; |
| 142 | + |
| 143 | + public ConstructorWithPrimitiveDto(final int intValue, final String str) { |
| 144 | + this.intValue = intValue; |
| 145 | + this.str = str; |
| 146 | + } |
| 147 | + |
| 148 | + public int getIntValue() { |
| 149 | + return intValue; |
| 150 | + } |
| 151 | + |
| 152 | + public String getStr() { |
| 153 | + return str; |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments