|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.query; |
| 6 | + |
| 7 | +import jakarta.persistence.CascadeType; |
| 8 | +import jakarta.persistence.Entity; |
| 9 | +import jakarta.persistence.FetchType; |
| 10 | +import jakarta.persistence.GeneratedValue; |
| 11 | +import jakarta.persistence.Id; |
| 12 | +import jakarta.persistence.ManyToOne; |
| 13 | +import jakarta.persistence.OneToMany; |
| 14 | +import jakarta.persistence.Table; |
| 15 | +import jakarta.persistence.Tuple; |
| 16 | +import jakarta.persistence.Version; |
| 17 | +import org.hibernate.InstantiationException; |
| 18 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 19 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 20 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 21 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 22 | +import org.junit.jupiter.api.AfterEach; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Objects; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 32 | + |
| 33 | +@DomainModel( |
| 34 | + annotatedClasses = { |
| 35 | + SelectWithWrongResultTypeTest.Element.class, |
| 36 | + SelectWithWrongResultTypeTest.Node.class, |
| 37 | + } |
| 38 | +) |
| 39 | +@SessionFactory |
| 40 | +@JiraKey("HHH-19868") |
| 41 | +public class SelectWithWrongResultTypeTest { |
| 42 | + |
| 43 | + @BeforeEach |
| 44 | + public void setUp(SessionFactoryScope scope) { |
| 45 | + scope.inTransaction( |
| 46 | + session -> { |
| 47 | + Node basik = new Node( "Child" ); |
| 48 | + basik.parent = new Node( "Parent" ); |
| 49 | + basik.elements.add( new Element( basik ) ); |
| 50 | + basik.elements.add( new Element( basik ) ); |
| 51 | + basik.elements.add( new Element( basik ) ); |
| 52 | + |
| 53 | + session.persist( basik ); |
| 54 | + } |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + @AfterEach |
| 59 | + public void tearDown(SessionFactoryScope scope) { |
| 60 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testSelectWithWrongResultType(SessionFactoryScope scope) { |
| 65 | + assertThrows( InstantiationException.class, () -> scope.inTransaction( session -> { |
| 66 | + List<Node> resultList = session |
| 67 | + .createSelectionQuery( "select distinct n, e from Node n join n.elements e", Node.class ) |
| 68 | + .getResultList(); |
| 69 | + assertThat( resultList ).hasSize( 3 ); |
| 70 | + } ) ); |
| 71 | + |
| 72 | + assertThrows( InstantiationException.class, () -> scope.inTransaction( session -> { |
| 73 | + List<Node> resultList = session |
| 74 | + .createSelectionQuery( "select distinct n.id, e.id from Node n join n.elements e", Node.class ) |
| 75 | + .getResultList(); |
| 76 | + assertThat( resultList ).hasSize( 3 ); |
| 77 | + } ) ); |
| 78 | + |
| 79 | + assertThrows( InstantiationException.class, () -> scope.inTransaction( session -> { |
| 80 | + List<Node> resultList = session |
| 81 | + .createSelectionQuery( |
| 82 | + "select max(e.id), min(e.id), sum(e.id) from Node n join n.elements e group by n.id order by n.id", |
| 83 | + Node.class ) |
| 84 | + .getResultList(); |
| 85 | + assertThat( resultList ).hasSize( 1 ); |
| 86 | + } ) ); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void testSelect(SessionFactoryScope scope) { |
| 91 | + |
| 92 | + scope.inTransaction( session -> { |
| 93 | + List<Node> resultList = session |
| 94 | + .createSelectionQuery( "select distinct n from Node n left join fetch n.elements", Node.class ) |
| 95 | + .getResultList(); |
| 96 | + assertThat( resultList ).hasSize( 2 ); |
| 97 | + } ); |
| 98 | + scope.inTransaction( session -> { |
| 99 | + List<Tuple> resultList = session |
| 100 | + .createSelectionQuery( "select distinct n, e from Node n join n.elements e", Tuple.class ) |
| 101 | + .getResultList(); |
| 102 | + assertThat( resultList ).hasSize( 3 ); |
| 103 | + } ); |
| 104 | + scope.inTransaction( session -> { |
| 105 | + List<Tuple> resultList = session |
| 106 | + .createSelectionQuery( "select distinct n.id, e.id from Node n join n.elements e", Tuple.class ) |
| 107 | + .getResultList(); |
| 108 | + assertThat( resultList ).hasSize( 3 ); |
| 109 | + } ); |
| 110 | + scope.inTransaction( session -> { |
| 111 | + List<Tuple> resultList = session |
| 112 | + .createSelectionQuery( |
| 113 | + "select max(e.id), min(e.id), sum(e.id) from Node n join n.elements e group by n.id order by n.id", |
| 114 | + Tuple.class ) |
| 115 | + .getResultList(); |
| 116 | + assertThat( resultList ).hasSize( 1 ); |
| 117 | + } ); |
| 118 | + } |
| 119 | + |
| 120 | + @Entity(name = "Element") |
| 121 | + @Table(name = "Element") |
| 122 | + public static class Element { |
| 123 | + @Id |
| 124 | + @GeneratedValue |
| 125 | + Integer id; |
| 126 | + |
| 127 | + @ManyToOne |
| 128 | + Node node; |
| 129 | + |
| 130 | + public Element(Node node) { |
| 131 | + this.node = node; |
| 132 | + } |
| 133 | + |
| 134 | + public Element() { |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Entity(name = "Node") |
| 139 | + @Table(name = "Node") |
| 140 | + public static class Node { |
| 141 | + |
| 142 | + @Id |
| 143 | + @GeneratedValue |
| 144 | + Integer id; |
| 145 | + |
| 146 | + @Version |
| 147 | + Integer version; |
| 148 | + |
| 149 | + String string; |
| 150 | + |
| 151 | + @ManyToOne(fetch = FetchType.LAZY, |
| 152 | + cascade = CascadeType.PERSIST) |
| 153 | + Node parent; |
| 154 | + |
| 155 | + @OneToMany(fetch = FetchType.EAGER, |
| 156 | + cascade = CascadeType.PERSIST, |
| 157 | + mappedBy = "node") |
| 158 | + List<Element> elements = new ArrayList<>(); |
| 159 | + |
| 160 | + public Node(String string) { |
| 161 | + this.string = string; |
| 162 | + } |
| 163 | + |
| 164 | + public Node() { |
| 165 | + } |
| 166 | + |
| 167 | + public Integer getId() { |
| 168 | + return id; |
| 169 | + } |
| 170 | + |
| 171 | + public void setId(Integer id) { |
| 172 | + this.id = id; |
| 173 | + } |
| 174 | + |
| 175 | + public String getString() { |
| 176 | + return string; |
| 177 | + } |
| 178 | + |
| 179 | + public void setString(String string) { |
| 180 | + this.string = string; |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public String toString() { |
| 185 | + return id + ": " + string; |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public boolean equals(Object o) { |
| 190 | + if ( this == o ) { |
| 191 | + return true; |
| 192 | + } |
| 193 | + if ( o == null || getClass() != o.getClass() ) { |
| 194 | + return false; |
| 195 | + } |
| 196 | + Node node = (Node) o; |
| 197 | + return Objects.equals( string, node.string ); |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public int hashCode() { |
| 202 | + return Objects.hash( string ); |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments