|
| 1 | +package org.hibernate.orm.test.hql; |
| 2 | + |
| 3 | +import java.time.LocalDate; |
| 4 | + |
| 5 | +import org.hibernate.dialect.DerbyDialect; |
| 6 | + |
| 7 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 8 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 9 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 10 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 11 | +import org.hibernate.testing.orm.junit.SkipForDialect; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +import jakarta.persistence.Column; |
| 15 | +import jakarta.persistence.Entity; |
| 16 | +import jakarta.persistence.Id; |
| 17 | +import jakarta.persistence.ManyToOne; |
| 18 | +import jakarta.persistence.Query; |
| 19 | + |
| 20 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 21 | + |
| 22 | +@DomainModel( |
| 23 | + annotatedClasses = { |
| 24 | + QueryComparingAssociationToNullTest.Parent.class, |
| 25 | + QueryComparingAssociationToNullTest.Child.class, |
| 26 | + } |
| 27 | +) |
| 28 | +@SessionFactory |
| 29 | +@JiraKey("HHH-16974") |
| 30 | +@SkipForDialect( dialectClass = DerbyDialect.class, reason = "it does not like `= null`") |
| 31 | +public class QueryComparingAssociationToNullTest { |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testQuery(SessionFactoryScope scope) { |
| 35 | + LocalDate date = LocalDate.now(); |
| 36 | + scope.inTransaction( |
| 37 | + session -> { |
| 38 | + Child child = new Child( 1, "first" ); |
| 39 | + Parent parent = new Parent( 1, date, child ); |
| 40 | + session.persist( child ); |
| 41 | + session.persist( parent ); |
| 42 | + } |
| 43 | + ); |
| 44 | + |
| 45 | + scope.inTransaction( |
| 46 | + session -> { |
| 47 | + Query query = session.createQuery( |
| 48 | + "SELECT p FROM Parent p WHERE p.child = NULL ", |
| 49 | + Parent.class |
| 50 | + ); |
| 51 | + query.getResultList(); |
| 52 | + } |
| 53 | + ); |
| 54 | + |
| 55 | + scope.inTransaction( |
| 56 | + session -> { |
| 57 | + Query query = session.createQuery( |
| 58 | + "SELECT p FROM Parent p WHERE p.child = NULL OR p.date = :date ", |
| 59 | + Parent.class |
| 60 | + ).setParameter( "date", date ); |
| 61 | + assertThat(query.getResultList().size()).isEqualTo( 1 ); |
| 62 | + } |
| 63 | + ); |
| 64 | + |
| 65 | + scope.inTransaction( |
| 66 | + session -> { |
| 67 | + Query query = session.createQuery( |
| 68 | + "SELECT p FROM Parent p WHERE p.child = NULL OR p.date = NULL ", |
| 69 | + Parent.class |
| 70 | + ); |
| 71 | + query.getResultList(); |
| 72 | + } |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + @Entity(name = "Parent") |
| 77 | + public static class Parent { |
| 78 | + |
| 79 | + @Id |
| 80 | + private Integer id; |
| 81 | + |
| 82 | + @Column(name = "COLUMN_DATE") |
| 83 | + private LocalDate date; |
| 84 | + |
| 85 | + @ManyToOne |
| 86 | + private Child child; |
| 87 | + |
| 88 | + |
| 89 | + public Parent() { |
| 90 | + } |
| 91 | + |
| 92 | + public Parent(Integer id, LocalDate date, Child child) { |
| 93 | + this.id = id; |
| 94 | + this.date = date; |
| 95 | + this.child = child; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Entity(name = "Child") |
| 100 | + public static class Child { |
| 101 | + @Id |
| 102 | + private Integer id; |
| 103 | + |
| 104 | + private String name; |
| 105 | + |
| 106 | + public Child() { |
| 107 | + } |
| 108 | + |
| 109 | + public Child(Integer id, String name) { |
| 110 | + this.id = id; |
| 111 | + this.name = name; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments