|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.query.onetoone; |
| 6 | + |
| 7 | +import jakarta.persistence.CascadeType; |
| 8 | +import jakarta.persistence.Entity; |
| 9 | +import jakarta.persistence.Id; |
| 10 | +import jakarta.persistence.OneToOne; |
| 11 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 12 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 13 | +import org.hibernate.testing.orm.junit.Jpa; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | + |
| 18 | +@Jpa(annotatedClasses = {QueryOneToOneIsNullTest.Parent.class, |
| 19 | + QueryOneToOneIsNullTest.Child.class}) |
| 20 | +@JiraKey("HHH-12712") |
| 21 | +public class QueryOneToOneIsNullTest { |
| 22 | + @Test |
| 23 | + public void test(EntityManagerFactoryScope scope) { |
| 24 | + scope.inTransaction(entityManager -> { |
| 25 | + var parent1 = new Parent(1); |
| 26 | + new Child(3, parent1); |
| 27 | + entityManager.persist(parent1); |
| 28 | + var parent2 = new Parent(2); |
| 29 | + entityManager.persist(parent2); |
| 30 | + }); |
| 31 | + |
| 32 | + scope.inTransaction(entityManager -> { |
| 33 | + var query = entityManager.createQuery("from Parent p where p.child is null", Parent.class); |
| 34 | + var parents = query.getResultList(); |
| 35 | + assertThat(parents).hasSize(1); |
| 36 | + assertThat(parents).element(0).extracting(Parent::getId).isEqualTo(2L); |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + @Entity(name = "Parent") |
| 41 | + static class Parent { |
| 42 | + @Id |
| 43 | + private long id; |
| 44 | + |
| 45 | + @OneToOne(mappedBy = "parent", cascade = CascadeType.ALL) |
| 46 | + private Child child; |
| 47 | + |
| 48 | + Parent() {} |
| 49 | + |
| 50 | + public Parent(long id) { |
| 51 | + this.id = id; |
| 52 | + } |
| 53 | + |
| 54 | + public long getId() { |
| 55 | + return id; |
| 56 | + } |
| 57 | + |
| 58 | + public Child getChild() { |
| 59 | + return child; |
| 60 | + } |
| 61 | + |
| 62 | + void setChild(Child child) { |
| 63 | + this.child = child; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public String toString() { |
| 68 | + return "Parent [id=" + id + ", child=" + child + "]"; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + @Entity(name = "Child") |
| 74 | + static class Child { |
| 75 | + @Id |
| 76 | + private long id; |
| 77 | + |
| 78 | + @OneToOne |
| 79 | + private Parent parent; |
| 80 | + |
| 81 | + Child() {} |
| 82 | + |
| 83 | + public Child(long id, Parent parent) { |
| 84 | + this.id = id; |
| 85 | + setParent(parent); |
| 86 | + } |
| 87 | + |
| 88 | + public Parent getParent() { |
| 89 | + return parent; |
| 90 | + } |
| 91 | + |
| 92 | + public void setParent(Parent parent) { |
| 93 | + this.parent = parent; |
| 94 | + parent.setChild(this); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public String toString() { |
| 99 | + return "Child [id=" + id + "]"; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments