|
| 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.query.criteria; |
| 6 | + |
| 7 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 8 | +import org.hibernate.testing.orm.junit.Jira; |
| 9 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 10 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 11 | +import org.junit.jupiter.api.AfterAll; |
| 12 | +import org.junit.jupiter.api.BeforeAll; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import jakarta.persistence.CascadeType; |
| 16 | +import jakarta.persistence.Entity; |
| 17 | +import jakarta.persistence.Id; |
| 18 | +import jakarta.persistence.JoinColumn; |
| 19 | +import jakarta.persistence.ManyToOne; |
| 20 | +import org.assertj.core.extractor.Extractors; |
| 21 | + |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author Marco Belladelli |
| 26 | + */ |
| 27 | +@DomainModel( annotatedClasses = { |
| 28 | + CriteriaMutationQueryFkValuesTest.DemoEntity.class, |
| 29 | + CriteriaMutationQueryFkValuesTest.A.class, |
| 30 | + CriteriaMutationQueryFkValuesTest.B.class, |
| 31 | + CriteriaMutationQueryFkValuesTest.C.class, |
| 32 | +} ) |
| 33 | +@SessionFactory( useCollectingStatementInspector = true ) |
| 34 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-18647" ) |
| 35 | +public class CriteriaMutationQueryFkValuesTest { |
| 36 | + @Test |
| 37 | + public void testInsertValuesFkColumns(SessionFactoryScope scope) { |
| 38 | + final var inspector = scope.getCollectingStatementInspector(); |
| 39 | + inspector.clear(); |
| 40 | + |
| 41 | + scope.inTransaction( session -> { |
| 42 | + final var cb = session.getCriteriaBuilder(); |
| 43 | + final var criteriaInsert = cb.createCriteriaInsertValues( DemoEntity.class ); |
| 44 | + criteriaInsert.setInsertionTargetPaths( |
| 45 | + criteriaInsert.getTarget().get( "id" ), |
| 46 | + // insert values into foreign key columns |
| 47 | + criteriaInsert.getTarget().get( "a" ).get( "id" ), // a_id |
| 48 | + criteriaInsert.getTarget().get( "b" ).get( "id" ), // b_id |
| 49 | + criteriaInsert.getTarget().get( "c" ).get( "id" ) // c_id |
| 50 | + ); |
| 51 | + criteriaInsert.values( cb.values( |
| 52 | + cb.value( 2L ), |
| 53 | + cb.value( 1 ), |
| 54 | + cb.value( 2 ), |
| 55 | + cb.value( 3 ) |
| 56 | + ) ); |
| 57 | + final var count = session.createMutationQuery( criteriaInsert ).executeUpdate(); |
| 58 | + assertThat( count ).isEqualTo( 1 ); |
| 59 | + inspector.assertNumberOfOccurrenceInQueryNoSpace( 0, "join", 0 ); |
| 60 | + |
| 61 | + assertThat( session.find( DemoEntity.class, 2L ) ).extracting( "a", "b", "c" ).doesNotContainNull(); |
| 62 | + } ); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testUpdateFkColumns(SessionFactoryScope scope) { |
| 67 | + final var inspector = scope.getCollectingStatementInspector(); |
| 68 | + inspector.clear(); |
| 69 | + |
| 70 | + scope.inTransaction( session -> { |
| 71 | + final var cb = session.getCriteriaBuilder(); |
| 72 | + final var criteriaUpdate = cb.createCriteriaUpdate( DemoEntity.class ); |
| 73 | + criteriaUpdate.set( criteriaUpdate.getTarget().get( "a" ).<Integer>get( "id" ), cb.value( 4 ) ); |
| 74 | + criteriaUpdate.set( |
| 75 | + criteriaUpdate.getTarget().get( "b" ).<Integer>get( "id" ), |
| 76 | + cb.nullLiteral( Integer.class ) |
| 77 | + ); |
| 78 | + criteriaUpdate.where( cb.equal( criteriaUpdate.getTarget().get( "id" ), 1L ) ); |
| 79 | + final var count = session.createMutationQuery( criteriaUpdate ).executeUpdate(); |
| 80 | + assertThat( count ).isEqualTo( 1 ); |
| 81 | + inspector.assertNumberOfOccurrenceInQueryNoSpace( 0, "join", 0 ); |
| 82 | + |
| 83 | + assertThat( session.find( DemoEntity.class, 1L ) ) |
| 84 | + .extracting( "a", "b", "c" ) |
| 85 | + .extracting( o -> o == null ? null : Extractors.byName( "id" ).apply( o ) ) |
| 86 | + .contains( 4, null, 3 ); |
| 87 | + } ); |
| 88 | + } |
| 89 | + |
| 90 | + @BeforeAll |
| 91 | + public void setUp(SessionFactoryScope scope) { |
| 92 | + scope.inTransaction( session -> { |
| 93 | + final var a = new A( 1 ); |
| 94 | + final var b = new B( 2 ); |
| 95 | + final var c = new C( 3 ); |
| 96 | + session.persist( new DemoEntity( 1L, a, b, c ) ); |
| 97 | + session.persist( new A( 4 ) ); |
| 98 | + } ); |
| 99 | + } |
| 100 | + |
| 101 | + @AfterAll |
| 102 | + public void tearDown(SessionFactoryScope scope) { |
| 103 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
| 104 | + } |
| 105 | + |
| 106 | + @Entity( name = "DemoEntity" ) |
| 107 | + static class DemoEntity { |
| 108 | + @Id |
| 109 | + private Long id; |
| 110 | + |
| 111 | + @ManyToOne( cascade = CascadeType.PERSIST ) |
| 112 | + @JoinColumn( name = "a_id" ) |
| 113 | + private A a; |
| 114 | + |
| 115 | + @ManyToOne( cascade = CascadeType.PERSIST ) |
| 116 | + @JoinColumn( name = "b_id" ) |
| 117 | + private B b; |
| 118 | + |
| 119 | + @ManyToOne( cascade = CascadeType.PERSIST ) |
| 120 | + @JoinColumn( name = "c_id" ) |
| 121 | + private C c; |
| 122 | + |
| 123 | + public DemoEntity() { |
| 124 | + } |
| 125 | + |
| 126 | + public DemoEntity(Long id, A a, B b, C c) { |
| 127 | + this.id = id; |
| 128 | + this.a = a; |
| 129 | + this.b = b; |
| 130 | + this.c = c; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + @Entity( name = "AEntity" ) |
| 135 | + static class A { |
| 136 | + @Id |
| 137 | + private Integer id; |
| 138 | + |
| 139 | + public A() { |
| 140 | + } |
| 141 | + |
| 142 | + public A(Integer id) { |
| 143 | + this.id = id; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Entity( name = "BEntity" ) |
| 148 | + static class B { |
| 149 | + @Id |
| 150 | + private Integer id; |
| 151 | + |
| 152 | + public B() { |
| 153 | + } |
| 154 | + |
| 155 | + public B(Integer id) { |
| 156 | + this.id = id; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + @Entity( name = "CEntity" ) |
| 161 | + static class C { |
| 162 | + @Id |
| 163 | + private Integer id; |
| 164 | + |
| 165 | + public C() { |
| 166 | + } |
| 167 | + |
| 168 | + public C(Integer id) { |
| 169 | + this.id = id; |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments