|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.secondarytable; |
| 6 | + |
| 7 | +import jakarta.persistence.Column; |
| 8 | +import jakarta.persistence.Entity; |
| 9 | +import jakarta.persistence.GeneratedValue; |
| 10 | +import jakarta.persistence.Id; |
| 11 | +import jakarta.persistence.Inheritance; |
| 12 | +import jakarta.persistence.InheritanceType; |
| 13 | +import jakarta.persistence.SecondaryTable; |
| 14 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 15 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 22 | + |
| 23 | + |
| 24 | +/** |
| 25 | + * Test for a Bugfix described in HHH-18813. |
| 26 | + * The CteUpdateHandler generated an Insert-Query, |
| 27 | + * which contained columns that do not exist in the target table. |
| 28 | + * |
| 29 | + * @author Peter Bambazek |
| 30 | + */ |
| 31 | +@JiraKey(value = "HHH-18813") |
| 32 | +@DomainModel( |
| 33 | + annotatedClasses = {HHH18813Test.SecondaryTableEntitySub.class, HHH18813Test.SecondaryTableEntityBase.class}) |
| 34 | +@SessionFactory |
| 35 | +class HHH18813Test { |
| 36 | + |
| 37 | + @Test |
| 38 | + void hhh18813Test(SessionFactoryScope scope) { |
| 39 | + |
| 40 | + // prepare |
| 41 | + scope.inTransaction( session -> { |
| 42 | + SecondaryTableEntitySub entitySub = new SecondaryTableEntitySub(); |
| 43 | + entitySub.setB( 111L ); |
| 44 | + entitySub.setC( 222L ); |
| 45 | + session.persist( entitySub ); |
| 46 | + } ); |
| 47 | + |
| 48 | + // asset before |
| 49 | + scope.inTransaction( session -> { |
| 50 | + SecondaryTableEntitySub entitySub = session.createQuery( |
| 51 | + "select s from SecondaryTableEntitySub s", |
| 52 | + SecondaryTableEntitySub.class ).getSingleResult(); |
| 53 | + assertNotNull( entitySub ); |
| 54 | + assertEquals( 111L, entitySub.getB() ); |
| 55 | + assertEquals( 222L, entitySub.getC() ); |
| 56 | + } ); |
| 57 | + |
| 58 | + // update |
| 59 | + scope.inTransaction( session -> { |
| 60 | + session.createMutationQuery( "update SecondaryTableEntitySub e set e.b=:b, e.c=:c" ) |
| 61 | + .setParameter( "b", 333L ) |
| 62 | + .setParameter( "c", 444L ) |
| 63 | + .executeUpdate(); |
| 64 | + } ); |
| 65 | + |
| 66 | + // asset after |
| 67 | + scope.inTransaction( session -> { |
| 68 | + SecondaryTableEntitySub entitySub = session.createQuery( "select s from SecondaryTableEntitySub s", |
| 69 | + SecondaryTableEntitySub.class ).getSingleResult(); |
| 70 | + assertNotNull( entitySub ); |
| 71 | + assertEquals( 333L, entitySub.getB() ); |
| 72 | + assertEquals( 444L, entitySub.getC() ); |
| 73 | + } ); |
| 74 | + } |
| 75 | + |
| 76 | + @Entity(name = "SecondaryTableEntitySub") |
| 77 | + @Inheritance(strategy = InheritanceType.JOINED) |
| 78 | + @SecondaryTable(name = "test") |
| 79 | + public static class SecondaryTableEntitySub extends SecondaryTableEntityBase { |
| 80 | + |
| 81 | + @Column |
| 82 | + private Long b; |
| 83 | + |
| 84 | + @Column(table = "test") |
| 85 | + private Long c; |
| 86 | + |
| 87 | + public Long getB() { |
| 88 | + return b; |
| 89 | + } |
| 90 | + |
| 91 | + public void setB(Long b) { |
| 92 | + this.b = b; |
| 93 | + } |
| 94 | + |
| 95 | + public Long getC() { |
| 96 | + return c; |
| 97 | + } |
| 98 | + |
| 99 | + public void setC(Long c) { |
| 100 | + this.c = c; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Entity |
| 105 | + @Inheritance(strategy = InheritanceType.JOINED) |
| 106 | + public static class SecondaryTableEntityBase { |
| 107 | + |
| 108 | + @Id |
| 109 | + @GeneratedValue |
| 110 | + private Long id; |
| 111 | + |
| 112 | + public Long getId() { |
| 113 | + return id; |
| 114 | + } |
| 115 | + |
| 116 | + public void setId(Long id) { |
| 117 | + this.id = id; |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments