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