|
| 1 | +package org.hibernate.orm.test.batch; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.hibernate.annotations.Fetch; |
| 7 | +import org.hibernate.annotations.FetchMode; |
| 8 | +import org.hibernate.cfg.AvailableSettings; |
| 9 | + |
| 10 | +import org.hibernate.testing.jdbc.SQLStatementInspector; |
| 11 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 12 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 13 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 16 | +import org.hibernate.testing.orm.junit.Setting; |
| 17 | +import org.junit.jupiter.api.BeforeAll; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import jakarta.persistence.Entity; |
| 21 | +import jakarta.persistence.FetchType; |
| 22 | +import jakarta.persistence.Id; |
| 23 | +import jakarta.persistence.JoinColumn; |
| 24 | +import jakarta.persistence.ManyToOne; |
| 25 | +import jakarta.persistence.OneToMany; |
| 26 | +import jakarta.persistence.Table; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 31 | + |
| 32 | +@DomainModel( |
| 33 | + annotatedClasses = { |
| 34 | + SubselectTest.EntityA.class, |
| 35 | + SubselectTest.EntityB.class, |
| 36 | + SubselectTest.EntityC.class, |
| 37 | + } |
| 38 | +) |
| 39 | +@SessionFactory(useCollectingStatementInspector = true) |
| 40 | +@ServiceRegistry( |
| 41 | + settings = { |
| 42 | + @Setting(name = AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, value = "10"), |
| 43 | + @Setting(name = AvailableSettings.FORMAT_SQL, value = "false"), |
| 44 | + } |
| 45 | +) |
| 46 | +@JiraKey("HHH-16624") |
| 47 | +public class SubselectTest { |
| 48 | + |
| 49 | + public static final int B_ID = 2; |
| 50 | + public static final int B1_ID = 3; |
| 51 | + public static final int A_ID = 1; |
| 52 | + |
| 53 | + @BeforeAll |
| 54 | + public void setUp(SessionFactoryScope scope) { |
| 55 | + scope.inTransaction( |
| 56 | + session -> { |
| 57 | + EntityA entityA = new EntityA( A_ID, "A" ); |
| 58 | + EntityB entityB = new EntityB( B_ID, "B" ); |
| 59 | + EntityB entityB1 = new EntityB( B1_ID, "B1" ); |
| 60 | + |
| 61 | + entityA.addEntityB( entityB ); |
| 62 | + entityA.addEntityB( entityB1 ); |
| 63 | + |
| 64 | + EntityC entityC = new EntityC( 4, "C" ); |
| 65 | + EntityC entityC1 = new EntityC( 5, "C1" ); |
| 66 | + |
| 67 | + entityB.addEntityC( entityC ); |
| 68 | + |
| 69 | + entityB1.addEntityC( entityC1 ); |
| 70 | + |
| 71 | + |
| 72 | + session.persist( entityA ); |
| 73 | + session.persist( entityB ); |
| 74 | + session.persist( entityB1 ); |
| 75 | + session.persist( entityC ); |
| 76 | + session.persist( entityC1 ); |
| 77 | + } |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testSkipSubselectWhenQueryResultIsOne(SessionFactoryScope scope) { |
| 83 | + SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector(); |
| 84 | + scope.inTransaction( |
| 85 | + session -> { |
| 86 | + List<EntityB> bs = session.createQuery( "select b from EntityB b where id= :id", EntityB.class ) |
| 87 | + .setParameter( "id", B_ID ) |
| 88 | + .list(); |
| 89 | + assertThat( bs.size() ).isEqualTo( 1 ); |
| 90 | + statementInspector.clear(); |
| 91 | + |
| 92 | + assertThat( bs.get( 0 ).getEntityCs() ).hasSize( 1 ); |
| 93 | + List<String> sqlQueries = statementInspector.getSqlQueries(); |
| 94 | + assertThat( sqlQueries.size() ).isEqualTo( 1 ); |
| 95 | + String query = sqlQueries.get( 0 ); |
| 96 | + assertFalse( containsSubquery( query ), " The query should contain a subquery" ); |
| 97 | + |
| 98 | + } |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testSubselectIsCreatedWhenQueryResultIsGreaterThanOne(SessionFactoryScope scope) { |
| 104 | + SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector(); |
| 105 | + scope.inTransaction( |
| 106 | + session -> { |
| 107 | + List<EntityB> bs = session.createQuery( "select b from EntityB b ", EntityB.class ) |
| 108 | + .list(); |
| 109 | + assertThat( bs.size() ).isEqualTo( 2 ); |
| 110 | + statementInspector.clear(); |
| 111 | + |
| 112 | + assertThat( bs.get( 0 ).getEntityCs() ).hasSize( 1 ); |
| 113 | + List<String> sqlQueries = statementInspector.getSqlQueries(); |
| 114 | + assertThat( sqlQueries.size() ).isEqualTo( 1 ); |
| 115 | + String query = sqlQueries.get( 0 ); |
| 116 | + assertTrue( containsSubquery( query ), " The query should contain a subquery" ); |
| 117 | + } |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testSubselectIsCreatedWhenQueryResultIsGreaterThanOne2(SessionFactoryScope scope) { |
| 123 | + SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector(); |
| 124 | + scope.inTransaction( |
| 125 | + session -> { |
| 126 | + EntityA a = session.get(EntityA.class,A_ID); |
| 127 | + List<EntityB> bs = a.getEntityBs(); |
| 128 | + assertThat( bs.size() ).isEqualTo( 2 ); |
| 129 | + statementInspector.clear(); |
| 130 | + |
| 131 | + assertThat( bs.get( 0 ).getEntityCs() ).hasSize( 1 ); |
| 132 | + List<String> sqlQueries = statementInspector.getSqlQueries(); |
| 133 | + assertThat( sqlQueries.size() ).isEqualTo( 1 ); |
| 134 | + String query = sqlQueries.get( 0 ); |
| 135 | + assertTrue( containsSubquery( query ), " The query should contain a subquery" ); |
| 136 | + } |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + private static boolean containsSubquery(String query) { |
| 141 | + return query.toLowerCase().substring( query.indexOf( "where" ) ).contains( "select" ); |
| 142 | + } |
| 143 | + |
| 144 | + @Entity(name = "EntityA") |
| 145 | + @Table(name = "ENTITY_A") |
| 146 | + public static class EntityA { |
| 147 | + @Id |
| 148 | + Integer id; |
| 149 | + |
| 150 | + String name; |
| 151 | + |
| 152 | + @OneToMany |
| 153 | + List<EntityB> entityBs = new ArrayList<>(); |
| 154 | + |
| 155 | + public EntityA() { |
| 156 | + } |
| 157 | + |
| 158 | + public EntityA(Integer id, String name) { |
| 159 | + this.id = id; |
| 160 | + this.name = name; |
| 161 | + } |
| 162 | + |
| 163 | + public Integer getId() { |
| 164 | + return id; |
| 165 | + } |
| 166 | + |
| 167 | + public List<EntityB> getEntityBs() { |
| 168 | + return entityBs; |
| 169 | + } |
| 170 | + |
| 171 | + public void addEntityB(EntityB entityB) { |
| 172 | + entityBs.add( entityB ); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + @Entity(name = "EntityB") |
| 177 | + @Table(name = "ENTITY_B") |
| 178 | + public static class EntityB { |
| 179 | + @Id |
| 180 | + Integer id; |
| 181 | + |
| 182 | + String name; |
| 183 | + |
| 184 | + @OneToMany(mappedBy = "entityB") |
| 185 | + @Fetch(FetchMode.SUBSELECT) |
| 186 | + List<EntityC> entityCs = new ArrayList<>(); |
| 187 | + |
| 188 | + public EntityB() { |
| 189 | + } |
| 190 | + |
| 191 | + public EntityB(Integer id, String name) { |
| 192 | + this.id = id; |
| 193 | + this.name = name; |
| 194 | + } |
| 195 | + |
| 196 | + public Integer getId() { |
| 197 | + return id; |
| 198 | + } |
| 199 | + |
| 200 | + public List<EntityC> getEntityCs() { |
| 201 | + return entityCs; |
| 202 | + } |
| 203 | + |
| 204 | + public void addEntityC(EntityC entityC) { |
| 205 | + entityCs.add( entityC ); |
| 206 | + entityC.entityB = this; |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + @Entity(name = "EntityC") |
| 211 | + @Table(name = "ENTITY_C") |
| 212 | + public static class EntityC { |
| 213 | + @Id |
| 214 | + Integer id; |
| 215 | + |
| 216 | + String name; |
| 217 | + |
| 218 | + public EntityC() { |
| 219 | + } |
| 220 | + |
| 221 | + public EntityC(Integer id, String name) { |
| 222 | + this.id = id; |
| 223 | + this.name = name; |
| 224 | + } |
| 225 | + |
| 226 | + @JoinColumn(name = "ENTITY_B") |
| 227 | + @ManyToOne |
| 228 | + EntityB entityB; |
| 229 | + |
| 230 | + public Integer getId() { |
| 231 | + return id; |
| 232 | + } |
| 233 | + |
| 234 | + public EntityB getEntityB() { |
| 235 | + return entityB; |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | +} |
0 commit comments