|
| 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.annotations.cid; |
| 6 | + |
| 7 | + |
| 8 | +import jakarta.persistence.*; |
| 9 | +import org.hibernate.cfg.BatchSettings; |
| 10 | +import org.hibernate.testing.orm.junit.*; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.fail; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Torsten Landmann |
| 19 | + * @author Jan Schatteman |
| 20 | + */ |
| 21 | +@ServiceRegistry( |
| 22 | + settings = { |
| 23 | + @Setting(name = BatchSettings.ORDER_UPDATES, value = "true") |
| 24 | + } |
| 25 | +) |
| 26 | +@DomainModel( |
| 27 | + annotatedClasses = { |
| 28 | + NestedCompositeIdWithOrderedUpdatesTest.A.class, |
| 29 | + NestedCompositeIdWithOrderedUpdatesTest.AId.class, |
| 30 | + NestedCompositeIdWithOrderedUpdatesTest.B.class, |
| 31 | + NestedCompositeIdWithOrderedUpdatesTest.BId.class, |
| 32 | + NestedCompositeIdWithOrderedUpdatesTest.C.class |
| 33 | + } |
| 34 | +) |
| 35 | +@SessionFactory |
| 36 | +public class NestedCompositeIdWithOrderedUpdatesTest { |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testUpdateOrderingWithNestedCompositeIds(SessionFactoryScope scope) { |
| 40 | + scope.inTransaction( |
| 41 | + session -> { |
| 42 | + // set up entities |
| 43 | + C c1 = new C(); |
| 44 | + c1.setCvalue("sample_c1"); |
| 45 | + session.persist(c1); |
| 46 | + C c2 = new C(); |
| 47 | + c2.setCvalue("sample_c2"); |
| 48 | + session.persist(c2); |
| 49 | + |
| 50 | + B b1 = new B(); |
| 51 | + b1.setId(new BId(c1, "b1_key")); |
| 52 | + b1.setBvalue("sample_b1"); |
| 53 | + session.persist(b1); |
| 54 | + B b2 = new B(); |
| 55 | + b2.setId(new BId(c2, "b2_key")); |
| 56 | + b2.setBvalue("sample_b2"); |
| 57 | + session.persist(b2); |
| 58 | + |
| 59 | + A a1 = new A(); |
| 60 | + a1.setId(new AId(b1, "a1")); |
| 61 | + a1.setAvalue("sample_a1"); |
| 62 | + session.persist(a1); |
| 63 | + A a2 = new A(); |
| 64 | + a2.setId(new AId(b2, "a2")); |
| 65 | + a2.setAvalue("sample_a2"); |
| 66 | + session.persist(a2); |
| 67 | + } |
| 68 | + ); |
| 69 | + |
| 70 | + try { |
| 71 | + scope.inTransaction( |
| 72 | + session -> { |
| 73 | + TypedQuery<A> query = session.createQuery("select a from A a", A.class); |
| 74 | + List<A> aList = query.getResultList(); |
| 75 | + for (A curA : aList) { |
| 76 | + curA.setAvalue(curA.getAvalue() + "_modified"); |
| 77 | + session.persist(curA); |
| 78 | + } |
| 79 | + } |
| 80 | + ); |
| 81 | + } catch (UnsupportedOperationException uoe) { |
| 82 | + fail("Shouldn't throw an UnsupportedOperationException!"); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Entity(name = "A") |
| 87 | + public static class A |
| 88 | + { |
| 89 | + @EmbeddedId |
| 90 | + private AId id; |
| 91 | + |
| 92 | + private String avalue; |
| 93 | + |
| 94 | + public AId getId() |
| 95 | + { |
| 96 | + return id; |
| 97 | + } |
| 98 | + |
| 99 | + public void setId(AId id) |
| 100 | + { |
| 101 | + this.id=id; |
| 102 | + } |
| 103 | + |
| 104 | + public String getAvalue() |
| 105 | + { |
| 106 | + return avalue; |
| 107 | + } |
| 108 | + |
| 109 | + public void setAvalue(String avalue) |
| 110 | + { |
| 111 | + this.avalue=avalue; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Embeddable |
| 116 | + public static class AId |
| 117 | + { |
| 118 | + @ManyToOne(cascade={}, // cascade nothing |
| 119 | + fetch=FetchType.LAZY, |
| 120 | + optional=false) |
| 121 | + private B b; |
| 122 | + |
| 123 | + /** |
| 124 | + * "key" won't work because h2 database considers it a reserved word, and hibernate doesn't escape it. |
| 125 | + * furthermore, the variable name must be after {@link #b}, alphabetically, to account for hibernate's internal sorting. |
| 126 | + */ |
| 127 | + private String zkey; |
| 128 | + |
| 129 | + public AId() |
| 130 | + { |
| 131 | + } |
| 132 | + |
| 133 | + public AId(B b, String key) |
| 134 | + { |
| 135 | + this.b=b; |
| 136 | + this.zkey=key; |
| 137 | + } |
| 138 | + |
| 139 | + public B getB() |
| 140 | + { |
| 141 | + return b; |
| 142 | + } |
| 143 | + |
| 144 | + public void setB(B b) |
| 145 | + { |
| 146 | + this.b=b; |
| 147 | + } |
| 148 | + |
| 149 | + public String getZkey() |
| 150 | + { |
| 151 | + return zkey; |
| 152 | + } |
| 153 | + |
| 154 | + public void setZkey(String zkey) |
| 155 | + { |
| 156 | + this.zkey=zkey; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + @Entity(name = "B") |
| 161 | + public static class B |
| 162 | + { |
| 163 | + @EmbeddedId |
| 164 | + private BId id; |
| 165 | + |
| 166 | + private String bvalue; |
| 167 | + |
| 168 | + public BId getId() |
| 169 | + { |
| 170 | + return id; |
| 171 | + } |
| 172 | + |
| 173 | + public void setId(BId id) |
| 174 | + { |
| 175 | + this.id=id; |
| 176 | + } |
| 177 | + |
| 178 | + public String getBvalue() |
| 179 | + { |
| 180 | + return bvalue; |
| 181 | + } |
| 182 | + |
| 183 | + public void setBvalue(String bvalue) |
| 184 | + { |
| 185 | + this.bvalue=bvalue; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + @Embeddable |
| 190 | + public static class BId |
| 191 | + { |
| 192 | + @ManyToOne(cascade={}, // cascade nothing |
| 193 | + fetch=FetchType.LAZY, |
| 194 | + optional=false) |
| 195 | + private C c; |
| 196 | + |
| 197 | + /** |
| 198 | + * "key" won't work because h2 database considers it a reserved word, and hibernate doesn't escape it. |
| 199 | + * furthermore, the variable name must be after {@link #c}, alphabetically, to account for hibernate's internal sorting. |
| 200 | + */ |
| 201 | + private String zkey; |
| 202 | + |
| 203 | + public BId() |
| 204 | + { |
| 205 | + } |
| 206 | + |
| 207 | + public BId(C c, String key) |
| 208 | + { |
| 209 | + this.c=c; |
| 210 | + this.zkey=key; |
| 211 | + } |
| 212 | + |
| 213 | + public C getC() |
| 214 | + { |
| 215 | + return c; |
| 216 | + } |
| 217 | + |
| 218 | + public void setC(C c) |
| 219 | + { |
| 220 | + this.c=c; |
| 221 | + } |
| 222 | + |
| 223 | + public String getZkey() |
| 224 | + { |
| 225 | + return zkey; |
| 226 | + } |
| 227 | + |
| 228 | + public void setZkey(String zkey) |
| 229 | + { |
| 230 | + this.zkey=zkey; |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + @Entity(name = "C") |
| 235 | + public static class C |
| 236 | + { |
| 237 | + @Id |
| 238 | + private String cvalue; |
| 239 | + |
| 240 | + public String getCvalue() |
| 241 | + { |
| 242 | + return cvalue; |
| 243 | + } |
| 244 | + |
| 245 | + public void setCvalue(String cvalue) |
| 246 | + { |
| 247 | + this.cvalue=cvalue; |
| 248 | + } |
| 249 | + } |
| 250 | +} |
0 commit comments