|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.dialect; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.Id; |
| 9 | +import jakarta.persistence.NoResultException; |
| 10 | +import org.hibernate.dialect.SybaseASEDialect; |
| 11 | +import org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl; |
| 12 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 13 | +import org.hibernate.testing.orm.junit.Jira; |
| 14 | +import org.hibernate.testing.orm.junit.RequiresDialect; |
| 15 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 18 | +import org.hibernate.testing.orm.junit.Setting; |
| 19 | +import org.junit.jupiter.api.AfterEach; |
| 20 | +import org.junit.jupiter.api.Assertions; |
| 21 | +import org.junit.jupiter.api.BeforeEach; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Jan Schatteman |
| 28 | + */ |
| 29 | +@DomainModel(annotatedClasses = { SybaseASENullParamTest.Book.class }) |
| 30 | +@SessionFactory |
| 31 | +@Jira( value = "https://hibernate.atlassian.net/browse/HHH-16216" ) |
| 32 | +public class SybaseASENullParamTest { |
| 33 | + |
| 34 | + @BeforeEach |
| 35 | + void setUp(SessionFactoryScope scope) { |
| 36 | + scope.inTransaction( session -> { |
| 37 | + session.persist( new Book(1L, "LoremIpsum") ); |
| 38 | + session.persist( new Book(2L, null) ); |
| 39 | + } ); |
| 40 | + } |
| 41 | + |
| 42 | + @AfterEach |
| 43 | + void tearDown(SessionFactoryScope scope) { |
| 44 | + scope.inTransaction( |
| 45 | + session -> session.createMutationQuery( "delete from Book" ).executeUpdate() |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + @RequiresDialect(value = SybaseASEDialect.class) |
| 51 | + public void testSybaseNullWithAnsiNullOn(SessionFactoryScope scope) { |
| 52 | + scope.inTransaction( |
| 53 | + session -> { |
| 54 | + Book b = session.createQuery( "SELECT b FROM Book b WHERE b.title is distinct from null", Book.class ).getSingleResult(); |
| 55 | + Assertions.assertEquals( 1L, b.id); |
| 56 | + |
| 57 | + b = session.createQuery( "SELECT b FROM Book b WHERE b.title is not distinct from null", Book.class ).getSingleResult(); |
| 58 | + Assertions.assertEquals( 2L, b.id); |
| 59 | + |
| 60 | + // Neither of these should return anything given the fact that with ansinull on, these comparisons evaluate to 'unknown' |
| 61 | + Assertions.assertThrows( NoResultException.class, () -> session.createQuery( "SELECT b FROM Book b WHERE b.title = null", Book.class ).getSingleResult()); |
| 62 | + Assertions.assertThrows( NoResultException.class, () -> session.createQuery( "SELECT b FROM Book b WHERE b.title != null", Book.class ).getSingleResult()); |
| 63 | + |
| 64 | + List<Book> books = session.createQuery( "SELECT b FROM Book b WHERE 1 = 1", Book.class ).list(); |
| 65 | + Assertions.assertEquals( 2, books.size()); |
| 66 | + } |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + @RequiresDialect(value = SybaseASEDialect.class) |
| 72 | + @DomainModel(annotatedClasses = { SybaseASENullParamTest.Book.class }) |
| 73 | + @SessionFactory |
| 74 | + @ServiceRegistry( settings = {@Setting(name = DriverManagerConnectionProviderImpl.INIT_SQL, value = "set ansinull off")} ) |
| 75 | + public void testSybaseNullWithAnsiNullOff(SessionFactoryScope scope) { |
| 76 | + scope.inTransaction( |
| 77 | + session -> { |
| 78 | + Book b = session.createQuery( "SELECT b FROM Book b WHERE b.title is distinct from null", Book.class ).getSingleResult(); |
| 79 | + Assertions.assertEquals( 1L, b.id); |
| 80 | + |
| 81 | +//fails because of the added and b1_0.title is not null in the where clause --> bug? |
| 82 | +// b = session.createQuery( "SELECT b FROM Book b WHERE b.title is not distinct from null", Book.class ).getSingleResult(); |
| 83 | +// Assertions.assertEquals( 2L, b.id); |
| 84 | + |
| 85 | +//fails because of the added and b1_0.title is not null in the where clause --> bug? |
| 86 | +// b = session.createQuery( "SELECT b FROM Book b WHERE b.title = null", Book.class ).getSingleResult(); |
| 87 | +// Assertions.assertEquals( 2L, b.id); |
| 88 | + |
| 89 | + b = session.createQuery( "SELECT b FROM Book b WHERE b.title != null", Book.class ).getSingleResult(); |
| 90 | + Assertions.assertEquals( 1L, b.id); |
| 91 | + |
| 92 | +// Doesn't fail but has 2 unnecessary 'and 1 is not null' conditions in the where clause (doesn't happen when ansinull is on) |
| 93 | + List<Book> books = session.createQuery( "SELECT b FROM Book b WHERE 1 = 1", Book.class ).list(); |
| 94 | + Assertions.assertEquals( 2, books.size()); |
| 95 | + } |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + @Entity(name = "Book") |
| 100 | + static class Book { |
| 101 | + @Id |
| 102 | + Long id; |
| 103 | + String title; |
| 104 | + |
| 105 | + public Book() { |
| 106 | + } |
| 107 | + |
| 108 | + public Book(Long id, String title) { |
| 109 | + this.id = id; |
| 110 | + this.title = title; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments