|
| 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; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import org.hibernate.dialect.TiDBDialect; |
| 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.hibernate.testing.orm.junit.SkipForDialect; |
| 18 | +import org.junit.jupiter.api.AfterAll; |
| 19 | +import org.junit.jupiter.api.BeforeAll; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import jakarta.persistence.Entity; |
| 23 | +import jakarta.persistence.FetchType; |
| 24 | +import jakarta.persistence.GeneratedValue; |
| 25 | +import jakarta.persistence.Id; |
| 26 | +import jakarta.persistence.ManyToMany; |
| 27 | +import jakarta.persistence.Tuple; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Marco Belladelli |
| 33 | + */ |
| 34 | +@SessionFactory |
| 35 | +@DomainModel( annotatedClasses = { |
| 36 | + CorrelatedListJoinInSubqueryTest.Property.class, |
| 37 | + CorrelatedListJoinInSubqueryTest.Entity1.class, |
| 38 | + CorrelatedListJoinInSubqueryTest.Entity2.class, |
| 39 | +} ) |
| 40 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-16888" ) |
| 41 | +@SkipForDialect( dialectClass = TiDBDialect.class, reason = "TiDB doesn't support subqueries in ON conditions yet" ) |
| 42 | +public class CorrelatedListJoinInSubqueryTest { |
| 43 | + @BeforeAll |
| 44 | + public void setUp(SessionFactoryScope scope) { |
| 45 | + scope.inTransaction( session -> { |
| 46 | + final Entity1 entity1 = new Entity1( "key" ); |
| 47 | + session.persist( entity1 ); |
| 48 | + final Property property1 = new Property( "admin" ); |
| 49 | + session.persist( property1 ); |
| 50 | + final Property property2 = new Property( "name" ); |
| 51 | + session.persist( property2 ); |
| 52 | + final Entity2 entity2 = new Entity2( "key", List.of( property1, property2 ) ); |
| 53 | + session.persist( entity2 ); |
| 54 | + } ); |
| 55 | + } |
| 56 | + |
| 57 | + @AfterAll |
| 58 | + public void tearDown(SessionFactoryScope scope) { |
| 59 | + scope.inTransaction( session -> { |
| 60 | + session.createMutationQuery( "delete from Entity1" ).executeUpdate(); |
| 61 | + session.createQuery( "from Entity2", Entity2.class ).getResultList().forEach( entity2 -> { |
| 62 | + entity2.getProperties().forEach( session::remove ); |
| 63 | + entity2.getProperties().clear(); |
| 64 | + session.remove( entity2 ); |
| 65 | + } ); |
| 66 | + } ); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testEntityJoin(SessionFactoryScope scope) { |
| 71 | + scope.inTransaction( session -> { |
| 72 | + final List<Tuple> resultList = session.createQuery( |
| 73 | + "select e1, e2 " + |
| 74 | + "from Entity1 e1 " + |
| 75 | + "join Entity2 e2 on e1.uniqueKey = e2.uniqueKey and exists (" + |
| 76 | + "select 1 from e2.properties p where p.name = :propertyName)", |
| 77 | + Tuple.class |
| 78 | + ).setParameter( "propertyName", "admin" ).getResultList(); |
| 79 | + assertThat( resultList ).hasSize( 1 ); |
| 80 | + } ); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testCrossJoin(SessionFactoryScope scope) { |
| 85 | + scope.inTransaction( session -> { |
| 86 | + final List<Tuple> resultList = session.createQuery( |
| 87 | + "select e1, e2 " + |
| 88 | + "from Entity1 e1, Entity2 e2 " + |
| 89 | + "where e1.uniqueKey = e2.uniqueKey and exists (" + |
| 90 | + "select 1 from e2.properties p where p.name = :propertyName)", |
| 91 | + Tuple.class |
| 92 | + ).setParameter( "propertyName", "admin" ).getResultList(); |
| 93 | + assertThat( resultList ).hasSize( 1 ); |
| 94 | + } ); |
| 95 | + } |
| 96 | + |
| 97 | + @Entity( name = "Property" ) |
| 98 | + public static class Property { |
| 99 | + @Id |
| 100 | + @GeneratedValue |
| 101 | + private Long id; |
| 102 | + private String name; |
| 103 | + |
| 104 | + public Property() { |
| 105 | + } |
| 106 | + |
| 107 | + public Property(String name) { |
| 108 | + this.name = name; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Entity( name = "Entity1" ) |
| 113 | + public static class Entity1 { |
| 114 | + @Id |
| 115 | + @GeneratedValue |
| 116 | + private Long id; |
| 117 | + private String uniqueKey; |
| 118 | + |
| 119 | + public Entity1() { |
| 120 | + } |
| 121 | + |
| 122 | + public Entity1(String uniqueKey) { |
| 123 | + this.uniqueKey = uniqueKey; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Entity( name = "Entity2" ) |
| 128 | + public static class Entity2 { |
| 129 | + |
| 130 | + @Id |
| 131 | + @GeneratedValue |
| 132 | + private Long id; |
| 133 | + |
| 134 | + private String uniqueKey; |
| 135 | + |
| 136 | + @ManyToMany( fetch = FetchType.LAZY ) |
| 137 | + private List<Property> properties; |
| 138 | + |
| 139 | + public Entity2() { |
| 140 | + } |
| 141 | + |
| 142 | + public Entity2(String uniqueKey, List<Property> properties) { |
| 143 | + this.uniqueKey = uniqueKey; |
| 144 | + this.properties = properties; |
| 145 | + } |
| 146 | + |
| 147 | + public List<Property> getProperties() { |
| 148 | + return properties; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments