|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.dialect.functional; |
| 6 | + |
| 7 | +import org.hibernate.cfg.AvailableSettings; |
| 8 | +import org.hibernate.dialect.SQLServerDialect; |
| 9 | +import org.hibernate.orm.test.jpa.CompositeId; |
| 10 | +import org.hibernate.orm.test.jpa.EntityWithCompositeId; |
| 11 | +import org.hibernate.testing.jdbc.SQLStatementInspector; |
| 12 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 13 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 14 | +import org.hibernate.testing.orm.junit.Jpa; |
| 15 | +import org.hibernate.testing.orm.junit.RequiresDialect; |
| 16 | +import org.hibernate.testing.orm.junit.Setting; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Rob Green |
| 27 | + */ |
| 28 | +@JiraKey( value = "HHH-8370" ) |
| 29 | +@RequiresDialect( value = SQLServerDialect.class, majorVersion = 10 ) |
| 30 | +@Jpa( |
| 31 | + annotatedClasses = { EntityWithCompositeId.class }, |
| 32 | + integrationSettings = { |
| 33 | + @Setting( name = AvailableSettings.USE_SQL_COMMENTS, value = "true" ), |
| 34 | + }, |
| 35 | + useCollectingStatementInspector = true |
| 36 | +) |
| 37 | +public class SQLServerDialectCompositeTest { |
| 38 | + @Test |
| 39 | + public void testCompositeQueryWithInPredicate(EntityManagerFactoryScope scope) { |
| 40 | + final SQLStatementInspector sqlStatementInterceptor = scope.getCollectingStatementInspector(); |
| 41 | + sqlStatementInterceptor.clear(); |
| 42 | + |
| 43 | + List<CompositeId> compositeIds = new ArrayList<>(); |
| 44 | + compositeIds.add( new CompositeId( 1,2 ) ); |
| 45 | + compositeIds.add( new CompositeId( 3,4 ) ); |
| 46 | + |
| 47 | + scope.inTransaction( entityManager -> { |
| 48 | + entityManager.createQuery( "SELECT e FROM EntityWithCompositeId e WHERE e.id IN (:ids)" ) |
| 49 | + .setParameter( "ids", compositeIds ) |
| 50 | + .getResultList(); |
| 51 | + } |
| 52 | + ); |
| 53 | + |
| 54 | + var query = sqlStatementInterceptor.getSqlQueries().get( 0 ); |
| 55 | + assertTrue(query.endsWith( "where exists (select 1 from (values (?,?), (?,?)) as v(id1, id2) where ewci1_0.id1 = v.id1 and ewci1_0.id2 = v.id2)" )); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testCompositeQueryWithNotInPredicate(EntityManagerFactoryScope scope) { |
| 60 | + final SQLStatementInspector sqlStatementInterceptor = scope.getCollectingStatementInspector(); |
| 61 | + sqlStatementInterceptor.clear(); |
| 62 | + |
| 63 | + List<CompositeId> compositeIds = new ArrayList<>(); |
| 64 | + compositeIds.add( new CompositeId( 1,2 ) ); |
| 65 | + compositeIds.add( new CompositeId( 3,4 ) ); |
| 66 | + |
| 67 | + scope.inTransaction( entityManager -> { |
| 68 | + entityManager.createQuery( "SELECT e FROM EntityWithCompositeId e WHERE e.id NOT IN (:ids)" ) |
| 69 | + .setParameter( "ids", compositeIds ) |
| 70 | + .getResultList(); |
| 71 | + } |
| 72 | + ); |
| 73 | + |
| 74 | + var query = sqlStatementInterceptor.getSqlQueries().get( 0 ); |
| 75 | + assertTrue(query.endsWith( "where not exists (select 1 from (values (?,?), (?,?)) as v(id1, id2) where ewci1_0.id1 = v.id1 and ewci1_0.id2 = v.id2)" )); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testCompositeQueryWithMultiplePredicatesIncludingIn(EntityManagerFactoryScope scope) { |
| 80 | + final SQLStatementInspector sqlStatementInterceptor = scope.getCollectingStatementInspector(); |
| 81 | + sqlStatementInterceptor.clear(); |
| 82 | + |
| 83 | + List<CompositeId> compositeIds = new ArrayList<>(); |
| 84 | + compositeIds.add( new CompositeId( 1,2 ) ); |
| 85 | + compositeIds.add( new CompositeId( 3,4 ) ); |
| 86 | + |
| 87 | + scope.inTransaction( entityManager -> { |
| 88 | + entityManager.createQuery("SELECT e FROM EntityWithCompositeId e WHERE e.description = :description AND e.id IN (:ids)") |
| 89 | + .setParameter( "ids", compositeIds ) |
| 90 | + .setParameter( "description", "test" ) |
| 91 | + .getResultList(); |
| 92 | + } |
| 93 | + ); |
| 94 | + |
| 95 | + var query = sqlStatementInterceptor.getSqlQueries().get( 0 ); |
| 96 | + assertTrue(query.endsWith( "where ewci1_0.description=? and exists (select 1 from (values (?,?), (?,?)) as v(id1, id2) where ewci1_0.id1 = v.id1 and ewci1_0.id2 = v.id2)" )); |
| 97 | + } |
| 98 | +} |
0 commit comments