|
| 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.formulajoin; |
| 6 | + |
| 7 | +import jakarta.persistence.CascadeType; |
| 8 | +import jakarta.persistence.Embeddable; |
| 9 | +import jakarta.persistence.EmbeddedId; |
| 10 | +import jakarta.persistence.Entity; |
| 11 | +import jakarta.persistence.FetchType; |
| 12 | +import jakarta.persistence.JoinColumn; |
| 13 | +import jakarta.persistence.ManyToOne; |
| 14 | +import org.hibernate.Hibernate; |
| 15 | +import org.hibernate.annotations.JoinColumnOrFormula; |
| 16 | +import org.hibernate.annotations.JoinFormula; |
| 17 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 18 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 19 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 20 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 21 | +import org.junit.jupiter.api.AfterEach; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 26 | + |
| 27 | +@JiraKey("HHH-9952") |
| 28 | +@DomainModel( |
| 29 | + annotatedClasses = { |
| 30 | + AssociationFormulaTest.Parent.class, |
| 31 | + AssociationFormulaTest.Child.class |
| 32 | + } |
| 33 | +) |
| 34 | +@SessionFactory |
| 35 | +public class AssociationFormulaTest { |
| 36 | + |
| 37 | + @BeforeEach |
| 38 | + public void setUp(SessionFactoryScope scope) { |
| 39 | + Child child = new Child( new EmbeddableId( "test", 2 ), "c1" ); |
| 40 | + Parent parent = new Parent( new EmbeddableId( "test", 1 ), "p1", child ); |
| 41 | + |
| 42 | + Parent parent2 = new Parent( new EmbeddableId( "null", 3 ), "p2" ); |
| 43 | + |
| 44 | + scope.inTransaction( |
| 45 | + session -> { |
| 46 | + session.persist( parent ); |
| 47 | + session.persist( parent2 ); |
| 48 | + } |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + @AfterEach |
| 53 | + public void tearDown(SessionFactoryScope scope) { |
| 54 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testJoin(SessionFactoryScope scope) { |
| 59 | + scope.inTransaction( |
| 60 | + session -> { |
| 61 | + Parent loaded = session.createQuery( |
| 62 | + "select e from Parent e inner join e.child o", |
| 63 | + Parent.class |
| 64 | + ).uniqueResult(); |
| 65 | + assertThat( loaded ).isNotNull(); |
| 66 | + assertThat( loaded.getId().getId2() ).isEqualTo( 1 ); |
| 67 | + assertThat( loaded.getChild().getId().getId2() ).isEqualTo( 2 ); |
| 68 | + assertThat( Hibernate.isInitialized( loaded.getChild() ) ).isFalse(); |
| 69 | + Hibernate.initialize( loaded.getChild() ); |
| 70 | + } |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testJoinFetch(SessionFactoryScope scope) { |
| 76 | + scope.inTransaction( |
| 77 | + session -> { |
| 78 | + Parent loaded = session.createQuery( |
| 79 | + "select e from Parent e inner join fetch e.child o", |
| 80 | + Parent.class |
| 81 | + ).uniqueResult(); |
| 82 | + assertThat( loaded ).isNotNull(); |
| 83 | + assertThat( loaded.getId().getId2() ).isEqualTo( 1 ); |
| 84 | + assertThat( Hibernate.isInitialized( loaded.getChild() ) ).isTrue(); |
| 85 | + assertThat( loaded.getChild().getId().getId2() ).isEqualTo( 2 ); |
| 86 | + } |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testSelect(SessionFactoryScope scope) { |
| 92 | + scope.inTransaction( |
| 93 | + session -> { |
| 94 | + Parent loaded = session.createQuery( "from Parent e where e.child is null", Parent.class ) |
| 95 | + .uniqueResult(); |
| 96 | + assertThat( loaded ).isNotNull(); |
| 97 | + assertThat( loaded.getId().getId2() ).isEqualTo( 3 ); |
| 98 | + assertThat( loaded.getChild() ).isNull(); |
| 99 | + } |
| 100 | + ); |
| 101 | + |
| 102 | + scope.inTransaction( |
| 103 | + session -> { |
| 104 | + Parent loaded = session.createQuery( "from Parent e where e.child.id.id2 is null", Parent.class ) |
| 105 | + .uniqueResult(); |
| 106 | + assertThat( loaded ).isNotNull(); |
| 107 | + assertThat( loaded.getId().getId2() ).isEqualTo( 3 ); |
| 108 | + assertThat( loaded.getChild() ).isNull(); |
| 109 | + } |
| 110 | + ); |
| 111 | + |
| 112 | + scope.inTransaction( |
| 113 | + session -> { |
| 114 | + Child child = new Child( new EmbeddableId( "test", 2 ), "c2" ); |
| 115 | + Parent loaded = session.createQuery( "from Parent e where e.child = :child", Parent.class ) |
| 116 | + .setParameter( "child", child ) |
| 117 | + .uniqueResult(); |
| 118 | + assertThat( loaded ).isNotNull(); |
| 119 | + assertThat( loaded.getId().getId2() ).isEqualTo( 1 ); |
| 120 | + assertThat( loaded.getChild() ).isNotNull(); |
| 121 | + assertThat( loaded.getChild().getId().getId2() ).isEqualTo( 2 ); |
| 122 | + } |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testUpdate(SessionFactoryScope scope) { |
| 128 | + scope.inTransaction( |
| 129 | + session -> { |
| 130 | + Parent loaded = session.createQuery( "from Parent e where e.id.id2 = 1", Parent.class ) |
| 131 | + .uniqueResult(); |
| 132 | + assertThat( loaded ).isNotNull(); |
| 133 | + assertThat( loaded.getChild() ).isNotNull(); |
| 134 | + Child child = new Child( new EmbeddableId( "test", 3 ), "c3" ); |
| 135 | + loaded.setChild( child ); |
| 136 | + } |
| 137 | + ); |
| 138 | + |
| 139 | + scope.inTransaction( |
| 140 | + session -> { |
| 141 | + Parent loaded = session.createQuery( "from Parent e where e.id.id2 = 3", Parent.class ) |
| 142 | + .uniqueResult(); |
| 143 | + assertThat( loaded ).isNotNull(); |
| 144 | + assertThat( loaded.getChild() ).isNull(); |
| 145 | + Child child = new Child( new EmbeddableId( "test", 4 ), "c4" ); |
| 146 | + loaded.setChild( child ); |
| 147 | + } |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testDelete(SessionFactoryScope scope) { |
| 153 | + scope.inTransaction( |
| 154 | + session -> { |
| 155 | + Child child = new Child( new EmbeddableId( "test", 2 ), "c2" ); |
| 156 | + assertThat( |
| 157 | + session.createMutationQuery( "delete Parent e where e.child = :child" ) |
| 158 | + .setParameter( "child", child ) |
| 159 | + .executeUpdate() |
| 160 | + ).isEqualTo( 1 ); |
| 161 | + Parent loaded = session.createQuery( "from Parent e where e.id.id2 = 1", Parent.class ) |
| 162 | + .uniqueResult(); |
| 163 | + assertThat( loaded ).isNull(); |
| 164 | + } |
| 165 | + ); |
| 166 | + } |
| 167 | + |
| 168 | +// @Test |
| 169 | +// public void testUpdateHql(SessionFactoryScope scope) { |
| 170 | +// scope.inTransaction( |
| 171 | +// session -> { |
| 172 | +// Child child = new Child( new EmbeddableId( "null", 4 ), "c4" ); |
| 173 | +// assertThat( |
| 174 | +// session.createQuery( "update Parent e set e.child = :child where e.id.id2 = 3" ) |
| 175 | +// .setParameter( "child", child ) |
| 176 | +// .executeUpdate() |
| 177 | +// ).isEqualTo( 1 ); |
| 178 | +// Parent loaded = session.createQuery( "from Parent e where e.id.id2 = 3", Parent.class ) |
| 179 | +// .uniqueResult(); |
| 180 | +// assertThat( loaded ).isNotNull(); |
| 181 | +// assertThat( loaded.getChild().getId().getId2() ).isEqualTo( 4 ); |
| 182 | +// } |
| 183 | +// ); |
| 184 | +// } |
| 185 | +// |
| 186 | +// @Test |
| 187 | +// public void testUpdateHqlNull(SessionFactoryScope scope) { |
| 188 | +// scope.inTransaction( |
| 189 | +// session -> { |
| 190 | +// assertThat( |
| 191 | +// session.createQuery( "update Parent e set e.child = null where e.id.id2 = 1" ) |
| 192 | +// .executeUpdate() |
| 193 | +// ).isEqualTo( 1 ); |
| 194 | +// Parent loaded = session.createQuery( "from Parent e where e.id.id2 = 1", Parent.class ) |
| 195 | +// .uniqueResult(); |
| 196 | +// assertThat( loaded ).isNotNull(); |
| 197 | +// assertThat( loaded.getChild().getId().getId2() ).isEqualTo( 4 ); |
| 198 | +// } |
| 199 | +// ); |
| 200 | +// } |
| 201 | + |
| 202 | + @Embeddable |
| 203 | + public static class EmbeddableId { |
| 204 | + private String id1; |
| 205 | + |
| 206 | + private int id2; |
| 207 | + |
| 208 | + public EmbeddableId() { |
| 209 | + } |
| 210 | + |
| 211 | + public EmbeddableId(String id1, int id2) { |
| 212 | + this.id1 = id1; |
| 213 | + this.id2 = id2; |
| 214 | + } |
| 215 | + |
| 216 | + public String getId1() { |
| 217 | + return id1; |
| 218 | + } |
| 219 | + |
| 220 | + public int getId2() { |
| 221 | + return id2; |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + @Entity(name = "Parent") |
| 226 | + public static class Parent { |
| 227 | + @EmbeddedId |
| 228 | + private EmbeddableId id; |
| 229 | + |
| 230 | + private String name; |
| 231 | + |
| 232 | + @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) |
| 233 | + @JoinColumnOrFormula(formula = @JoinFormula(referencedColumnName = "id1", value = "case when child is null then null else id1 end")) |
| 234 | + @JoinColumnOrFormula(column = @JoinColumn(referencedColumnName = "id2", name = "child")) |
| 235 | + private Child child; |
| 236 | + |
| 237 | + public Parent() { |
| 238 | + } |
| 239 | + |
| 240 | + public Parent(EmbeddableId id, String name) { |
| 241 | + this.id = id; |
| 242 | + this.name = name; |
| 243 | + } |
| 244 | + |
| 245 | + public Parent(EmbeddableId id, String name, Child child) { |
| 246 | + this.id = id; |
| 247 | + this.name = name; |
| 248 | + this.child = child; |
| 249 | + } |
| 250 | + |
| 251 | + public EmbeddableId getId() { |
| 252 | + return id; |
| 253 | + } |
| 254 | + |
| 255 | + public String getName() { |
| 256 | + return name; |
| 257 | + } |
| 258 | + |
| 259 | + public Child getChild() { |
| 260 | + return child; |
| 261 | + } |
| 262 | + |
| 263 | + public void setChild(Child partial) { |
| 264 | + this.child = partial; |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + @Entity(name = "Child") |
| 269 | + public static class Child { |
| 270 | + |
| 271 | + @EmbeddedId |
| 272 | + private EmbeddableId id; |
| 273 | + |
| 274 | + private String name; |
| 275 | + |
| 276 | + public Child() { |
| 277 | + } |
| 278 | + |
| 279 | + public Child(EmbeddableId id, String name) { |
| 280 | + this.id = id; |
| 281 | + this.name = name; |
| 282 | + } |
| 283 | + |
| 284 | + public String getName() { |
| 285 | + return name; |
| 286 | + } |
| 287 | + |
| 288 | + public EmbeddableId getId() { |
| 289 | + return id; |
| 290 | + } |
| 291 | + } |
| 292 | + |
| 293 | +} |
0 commit comments