|
| 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.idclass; |
| 6 | + |
| 7 | +import java.io.Serializable; |
| 8 | +import java.util.HashSet; |
| 9 | +import java.util.Set; |
| 10 | + |
| 11 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 12 | +import org.hibernate.testing.orm.junit.FailureExpected; |
| 13 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 16 | + |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import jakarta.persistence.CascadeType; |
| 20 | +import jakarta.persistence.Column; |
| 21 | +import jakarta.persistence.Entity; |
| 22 | +import jakarta.persistence.FetchType; |
| 23 | +import jakarta.persistence.GeneratedValue; |
| 24 | +import jakarta.persistence.GenerationType; |
| 25 | +import jakarta.persistence.Id; |
| 26 | +import jakarta.persistence.IdClass; |
| 27 | +import jakarta.persistence.JoinColumn; |
| 28 | +import jakarta.persistence.JoinColumns; |
| 29 | +import jakarta.persistence.ManyToOne; |
| 30 | +import jakarta.persistence.OneToMany; |
| 31 | +import jakarta.persistence.Table; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Jan Schatteman |
| 35 | + */ |
| 36 | +@DomainModel( |
| 37 | + annotatedClasses = { |
| 38 | + NestedIdClassTest.Asset.class, NestedIdClassTest.AssetAssetTypeAttribute.class, NestedIdClassTest.AssetTypeAttribute.class |
| 39 | + } |
| 40 | +) |
| 41 | +@SessionFactory |
| 42 | +public class NestedIdClassTest { |
| 43 | + |
| 44 | + @Test |
| 45 | + @JiraKey("HHH-14340") |
| 46 | + @FailureExpected(reason = "duplicate column in the generated SQL") |
| 47 | + public void testIdClass(SessionFactoryScope scope) { |
| 48 | + scope.inTransaction( |
| 49 | + session -> { |
| 50 | + Asset asset = new Asset(); |
| 51 | + asset.setId(1L); |
| 52 | + asset.setTenantId(2L); |
| 53 | + AssetTypeAttribute assetTypeAttribute = new AssetTypeAttribute(); |
| 54 | + assetTypeAttribute.setId(3L); |
| 55 | + assetTypeAttribute.setName("TestAttribute"); |
| 56 | + |
| 57 | + AssetAssetTypeAttribute assetAssetTypeAttribute = new AssetAssetTypeAttribute(); |
| 58 | + |
| 59 | + assetAssetTypeAttribute.setAssetTypeAttributeId(assetTypeAttribute.getId()); |
| 60 | + assetAssetTypeAttribute.setAsset(asset); |
| 61 | + asset.setAssetAssetTypeAttributes(new HashSet<>()); |
| 62 | + asset.getAssetAssetTypeAttributes().add(assetAssetTypeAttribute); |
| 63 | + |
| 64 | + session.persist(asset); |
| 65 | + |
| 66 | + for (AssetAssetTypeAttribute assetAssetTypeAttribute1 : asset.getAssetAssetTypeAttributes()) { |
| 67 | + session.persist(assetAssetTypeAttribute1); |
| 68 | + } |
| 69 | + } |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + @Entity(name = "Asset") |
| 74 | + @Table(name = "asset") |
| 75 | + @IdClass(AssetId.class) |
| 76 | + public static class Asset { |
| 77 | + @Id |
| 78 | + private Long id; |
| 79 | + |
| 80 | + @Id |
| 81 | + @Column(name = "tenant_id") |
| 82 | + private Long tenantId; |
| 83 | + |
| 84 | + @OneToMany(fetch = FetchType.LAZY, mappedBy = "asset", cascade = CascadeType.ALL, orphanRemoval = true) |
| 85 | + private Set<AssetAssetTypeAttribute> assetAssetTypeAttributes; |
| 86 | + |
| 87 | + public void setTenantId(Long tenantId) { |
| 88 | + this.tenantId = tenantId; |
| 89 | + } |
| 90 | + |
| 91 | + public Long getTenantId() { |
| 92 | + return tenantId; |
| 93 | + } |
| 94 | + |
| 95 | + public void setId(Long id) { |
| 96 | + this.id = id; |
| 97 | + } |
| 98 | + |
| 99 | + public Long getId() { |
| 100 | + return id; |
| 101 | + } |
| 102 | + |
| 103 | + public Set<AssetAssetTypeAttribute> getAssetAssetTypeAttributes() { |
| 104 | + return assetAssetTypeAttributes; |
| 105 | + } |
| 106 | + |
| 107 | + public void setAssetAssetTypeAttributes(Set<AssetAssetTypeAttribute> assetAssetTypeAttributes) { |
| 108 | + this.assetAssetTypeAttributes = assetAssetTypeAttributes; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Entity(name = "AssetAssetTypeAttribute") |
| 113 | + @Table(name = "asset_asset_type_attribute") |
| 114 | + @IdClass(AssetAttributeId.class) |
| 115 | + public static class AssetAssetTypeAttribute { |
| 116 | + |
| 117 | + @Id |
| 118 | + @Column(name = "tenant_id", insertable = false, updatable = false) |
| 119 | + private Long tenantId; |
| 120 | + |
| 121 | + @Id |
| 122 | + @Column(name = "asset_id", insertable = false, updatable = false) |
| 123 | + private Long assetId; |
| 124 | + |
| 125 | + @Id |
| 126 | + @Column(name = "asset_type_attribute_id") |
| 127 | + private Long assetTypeAttributeId; |
| 128 | + |
| 129 | + @ManyToOne(fetch = FetchType.LAZY) |
| 130 | + @JoinColumns({ |
| 131 | + @JoinColumn(name = "asset_id", referencedColumnName = "id"), |
| 132 | + @JoinColumn(name = "tenant_id", referencedColumnName = "tenant_id"), |
| 133 | + }) |
| 134 | + private Asset asset; |
| 135 | + |
| 136 | + @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.DETACH) |
| 137 | + @JoinColumn(name = "asset_type_attribute_id", referencedColumnName = "id", insertable = false, updatable = false) |
| 138 | + private AssetTypeAttribute assetTypeAttribute; |
| 139 | + |
| 140 | + private String sValue; |
| 141 | + |
| 142 | + public Long getTenantId() { |
| 143 | + return tenantId; |
| 144 | + } |
| 145 | + |
| 146 | + public void setTenantId(Long tenantId) { |
| 147 | + this.tenantId = tenantId; |
| 148 | + } |
| 149 | + |
| 150 | + public Long getAssetId() { |
| 151 | + return assetId; |
| 152 | + } |
| 153 | + |
| 154 | + public void setAssetId(Long assetId) { |
| 155 | + this.assetId = assetId; |
| 156 | + } |
| 157 | + |
| 158 | + public Long getAssetTypeAttributeId() { |
| 159 | + return assetTypeAttributeId; |
| 160 | + } |
| 161 | + |
| 162 | + public void setAssetTypeAttributeId(Long assetTypeAttributeId) { |
| 163 | + this.assetTypeAttributeId = assetTypeAttributeId; |
| 164 | + } |
| 165 | + |
| 166 | + public Asset getAsset() { |
| 167 | + return asset; |
| 168 | + } |
| 169 | + |
| 170 | + public void setAsset(Asset asset) { |
| 171 | + this.asset = asset; |
| 172 | + } |
| 173 | + |
| 174 | + public AssetTypeAttribute getAssetTypeAttribute() { |
| 175 | + return assetTypeAttribute; |
| 176 | + } |
| 177 | + |
| 178 | + public void setAssetTypeAttribute(AssetTypeAttribute assetTypeAttribute) { |
| 179 | + this.assetTypeAttribute = assetTypeAttribute; |
| 180 | + } |
| 181 | + |
| 182 | + public String getsValue() { |
| 183 | + return sValue; |
| 184 | + } |
| 185 | + |
| 186 | + public void setsValue(String sValue) { |
| 187 | + this.sValue = sValue; |
| 188 | + } |
| 189 | + } |
| 190 | + @Entity(name = "AssetTypeAttribute") |
| 191 | + @Table(name = "asset_type_attribute") |
| 192 | + public static class AssetTypeAttribute { |
| 193 | + @Id |
| 194 | + @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 195 | + private Long id; |
| 196 | + private String name; |
| 197 | + |
| 198 | + @OneToMany(fetch = FetchType.LAZY, mappedBy = "assetTypeAttribute", cascade = CascadeType.ALL, orphanRemoval = true) |
| 199 | + private Set<AssetAssetTypeAttribute> assetAssetTypeAttributes; |
| 200 | + |
| 201 | + public Long getId() { |
| 202 | + return id; |
| 203 | + } |
| 204 | + |
| 205 | + public void setId(Long id) { |
| 206 | + this.id = id; |
| 207 | + } |
| 208 | + |
| 209 | + public String getName() { |
| 210 | + return name; |
| 211 | + } |
| 212 | + |
| 213 | + public void setName(String name) { |
| 214 | + this.name = name; |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + public static class AssetAttributeId implements Serializable { |
| 219 | + private Long assetId; |
| 220 | + private Long assetTypeAttributeId; |
| 221 | + private Long tenantId; |
| 222 | + |
| 223 | + public AssetAttributeId() {} |
| 224 | + |
| 225 | + public AssetAttributeId(Long assetId, Long assetTypeAttributeId, Long tenantId) { |
| 226 | + this.assetId = assetId; |
| 227 | + this.assetTypeAttributeId = assetTypeAttributeId; |
| 228 | + this.tenantId = tenantId; |
| 229 | + } |
| 230 | + |
| 231 | + public Long getAssetId() { |
| 232 | + return assetId; |
| 233 | + } |
| 234 | + |
| 235 | + public void setAssetId(Long assetId) { |
| 236 | + this.assetId = assetId; |
| 237 | + } |
| 238 | + |
| 239 | + public Long getAssetTypeAttributeId() { |
| 240 | + return assetTypeAttributeId; |
| 241 | + } |
| 242 | + |
| 243 | + public void setAssetTypeAttributeId(Long assetTypeAttributeId) { |
| 244 | + this.assetTypeAttributeId = assetTypeAttributeId; |
| 245 | + } |
| 246 | + |
| 247 | + public Long getTenantId() { |
| 248 | + return tenantId; |
| 249 | + } |
| 250 | + |
| 251 | + public void setTenantId(Long tenantId) { |
| 252 | + this.tenantId = tenantId; |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + public static class AssetId implements Serializable { |
| 257 | + private Long id; |
| 258 | + private Long tenantId; |
| 259 | + |
| 260 | + public AssetId() {} |
| 261 | + |
| 262 | + public AssetId(Long id, Long tenantId) { |
| 263 | + this.id = id; |
| 264 | + this.tenantId = tenantId; |
| 265 | + } |
| 266 | + |
| 267 | + public Long getId() { |
| 268 | + return id; |
| 269 | + } |
| 270 | + |
| 271 | + public void setId(Long id) { |
| 272 | + this.id = id; |
| 273 | + } |
| 274 | + |
| 275 | + public Long getTenantId() { |
| 276 | + return tenantId; |
| 277 | + } |
| 278 | + |
| 279 | + public void setTenantId(Long tenantId) { |
| 280 | + this.tenantId = tenantId; |
| 281 | + } |
| 282 | + } |
| 283 | + |
| 284 | +} |
0 commit comments