|
| 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.jpa.criteria; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 10 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 11 | +import org.hibernate.testing.orm.junit.Jpa; |
| 12 | +import org.junit.jupiter.api.AfterEach; |
| 13 | +import org.junit.jupiter.api.BeforeEach; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import jakarta.persistence.CascadeType; |
| 17 | +import jakarta.persistence.Entity; |
| 18 | +import jakarta.persistence.FetchType; |
| 19 | +import jakarta.persistence.Id; |
| 20 | +import jakarta.persistence.OneToOne; |
| 21 | +import jakarta.persistence.criteria.CriteriaBuilder; |
| 22 | +import jakarta.persistence.criteria.CriteriaQuery; |
| 23 | +import jakarta.persistence.criteria.Join; |
| 24 | +import jakarta.persistence.criteria.JoinType; |
| 25 | +import jakarta.persistence.criteria.Root; |
| 26 | + |
| 27 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 28 | + |
| 29 | +@Jpa( |
| 30 | + annotatedClasses = { |
| 31 | + CriteriaSelectOneToOneUnownedTest.Parent.class, |
| 32 | + CriteriaSelectOneToOneUnownedTest.Child.class, |
| 33 | + } |
| 34 | +) |
| 35 | +@JiraKey("HHH-18628") |
| 36 | +public class CriteriaSelectOneToOneUnownedTest { |
| 37 | + |
| 38 | + @BeforeEach |
| 39 | + public void setUp(EntityManagerFactoryScope scope) { |
| 40 | + scope.inTransaction( |
| 41 | + entityManager -> { |
| 42 | + Child child = new Child( 1l, "child" ); |
| 43 | + Parent parent = new Parent( 1l, "parent", child ); |
| 44 | + entityManager.persist( parent ); |
| 45 | + } |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + @AfterEach |
| 50 | + public void tearDown(EntityManagerFactoryScope scope) { |
| 51 | + scope.inTransaction( |
| 52 | + entityManager -> { |
| 53 | + entityManager.createQuery( "delete from Child" ).executeUpdate(); |
| 54 | + entityManager.createQuery( "delete from Parent" ).executeUpdate(); |
| 55 | + } |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testCriteriaInnerJoin(EntityManagerFactoryScope scope) { |
| 61 | + scope.inTransaction( |
| 62 | + entityManager -> { |
| 63 | + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); |
| 64 | + |
| 65 | + CriteriaQuery<Child> query = cb.createQuery( Child.class ); |
| 66 | + Root<Parent> parent = query.from( Parent.class ); |
| 67 | + Join<Parent, Child> child = parent.join( "child", JoinType.INNER ); |
| 68 | + query.select( child ); |
| 69 | + |
| 70 | + List<Child> children = entityManager.createQuery( query ).getResultList(); |
| 71 | + assertThat( children ).isNotNull(); |
| 72 | + assertThat( children.size() ).isEqualTo( 1 ); |
| 73 | + Child c = children.get( 0 ); |
| 74 | + assertThat( c ).isNotNull(); |
| 75 | + } ); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testCriteriaLeftJoin(EntityManagerFactoryScope scope) { |
| 80 | + scope.inTransaction( |
| 81 | + entityManager -> { |
| 82 | + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); |
| 83 | + |
| 84 | + CriteriaQuery<Child> query = cb.createQuery( Child.class ); |
| 85 | + Root<Parent> parent = query.from( Parent.class ); |
| 86 | + Join<Parent, Child> child = parent.join( "child", JoinType.LEFT ); |
| 87 | + query.select( child ); |
| 88 | + |
| 89 | + List<Child> children = entityManager.createQuery( query ).getResultList(); |
| 90 | + assertThat( children ).isNotNull(); |
| 91 | + assertThat( children.size() ).isEqualTo( 1 ); |
| 92 | + Child c = children.get( 0 ); |
| 93 | + assertThat( c ).isNotNull(); |
| 94 | + } ); |
| 95 | + } |
| 96 | + |
| 97 | + @Entity(name = "Parent") |
| 98 | + public static class Parent { |
| 99 | + @Id |
| 100 | + private Long id; |
| 101 | + |
| 102 | + private String name; |
| 103 | + |
| 104 | + @OneToOne(mappedBy = "parent", cascade = CascadeType.PERSIST) |
| 105 | + private Child child; |
| 106 | + |
| 107 | + public Parent() { |
| 108 | + } |
| 109 | + |
| 110 | + public Parent(Long id, String name, Child child) { |
| 111 | + this.id = id; |
| 112 | + this.name = name; |
| 113 | + this.child = child; |
| 114 | + child.parent = this; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Entity(name = "Child") |
| 119 | + public static class Child { |
| 120 | + |
| 121 | + @Id |
| 122 | + private Long id; |
| 123 | + |
| 124 | + private String name; |
| 125 | + |
| 126 | + @OneToOne(optional = false, fetch = FetchType.LAZY) |
| 127 | + private Parent parent; |
| 128 | + |
| 129 | + public Child() { |
| 130 | + } |
| 131 | + |
| 132 | + public Child(Long id, String name) { |
| 133 | + this.id = id; |
| 134 | + this.name = name; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments