|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.jpa.criteria; |
| 6 | + |
| 7 | +import jakarta.persistence.Basic; |
| 8 | +import jakarta.persistence.CascadeType; |
| 9 | +import jakarta.persistence.Entity; |
| 10 | +import jakarta.persistence.Id; |
| 11 | +import jakarta.persistence.ManyToOne; |
| 12 | +import jakarta.persistence.criteria.CriteriaBuilder; |
| 13 | +import jakarta.persistence.criteria.CriteriaUpdate; |
| 14 | +import jakarta.persistence.criteria.Root; |
| 15 | +import org.hibernate.SessionFactory; |
| 16 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 17 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 18 | +import org.hibernate.testing.orm.junit.Jpa; |
| 19 | +import org.junit.jupiter.api.AfterEach; |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 24 | + |
| 25 | + |
| 26 | +@Jpa( |
| 27 | + annotatedClasses = { |
| 28 | + CriteriaUpdateAssociationSetNullValueTest.Parent.class, |
| 29 | + CriteriaUpdateAssociationSetNullValueTest.Child.class} |
| 30 | +) |
| 31 | +@JiraKey("HHH-19085") |
| 32 | +public class CriteriaUpdateAssociationSetNullValueTest { |
| 33 | + |
| 34 | + private static final Long PARENT_ID = 1L; |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + public void setUp(EntityManagerFactoryScope scope) { |
| 38 | + scope.inTransaction( |
| 39 | + em -> { |
| 40 | + em.persist( new Parent( PARENT_ID, "Lionello", new Child( 2L, "Andrea" ) ) ); |
| 41 | + } |
| 42 | + |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + @AfterEach |
| 47 | + public void tearDown(EntityManagerFactoryScope scope) { |
| 48 | + scope.getEntityManagerFactory().unwrap( SessionFactory.class ).getSchemaManager().truncateMappedObjects(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void testUpdateSetAssociationToNullValue(EntityManagerFactoryScope scope) { |
| 53 | + scope.inTransaction( |
| 54 | + em -> { |
| 55 | + CriteriaBuilder cb = em.getCriteriaBuilder(); |
| 56 | + CriteriaUpdate<Parent> update = cb.createCriteriaUpdate( Parent.class ); |
| 57 | + Root<Parent> msg = update.from( Parent.class ); |
| 58 | + update.set( msg.get( "child" ), (Child) null ); |
| 59 | + em.createQuery( update ).executeUpdate(); |
| 60 | + } |
| 61 | + ); |
| 62 | + |
| 63 | + scope.inTransaction( |
| 64 | + em -> { |
| 65 | + Parent parent = em.find( Parent.class, PARENT_ID ); |
| 66 | + assertThat( parent ).isNotNull(); |
| 67 | + assertThat( parent.getName() ).isNotNull(); |
| 68 | + assertThat( parent.getChild() ).isNull(); |
| 69 | + } |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + @Entity(name = "Parent") |
| 74 | + public static class Parent { |
| 75 | + |
| 76 | + @Id |
| 77 | + private Long id; |
| 78 | + |
| 79 | + @Basic |
| 80 | + private String name; |
| 81 | + |
| 82 | + @ManyToOne(cascade = CascadeType.PERSIST) |
| 83 | + private Child child; |
| 84 | + |
| 85 | + public Parent() { |
| 86 | + } |
| 87 | + |
| 88 | + public Parent(Long id, String name, Child child) { |
| 89 | + this.id = id; |
| 90 | + this.name = name; |
| 91 | + this.child = child; |
| 92 | + } |
| 93 | + |
| 94 | + public Long getId() { |
| 95 | + return id; |
| 96 | + } |
| 97 | + |
| 98 | + public String getName() { |
| 99 | + return name; |
| 100 | + } |
| 101 | + |
| 102 | + public Child getChild() { |
| 103 | + return child; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Entity(name = "Child") |
| 108 | + public static class Child { |
| 109 | + |
| 110 | + @Id |
| 111 | + private Long id; |
| 112 | + |
| 113 | + private String name; |
| 114 | + |
| 115 | + public Child() { |
| 116 | + } |
| 117 | + |
| 118 | + public Child(Long id, String name) { |
| 119 | + this.id = id; |
| 120 | + this.name = name; |
| 121 | + } |
| 122 | + |
| 123 | + public Long getId() { |
| 124 | + return id; |
| 125 | + } |
| 126 | + |
| 127 | + public String getName() { |
| 128 | + return name; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments