|
| 1 | +package org.hibernate.orm.test.any.annotations; |
| 2 | + |
| 3 | +import java.math.BigDecimal; |
| 4 | + |
| 5 | +import org.hibernate.annotations.Any; |
| 6 | +import org.hibernate.annotations.AnyDiscriminator; |
| 7 | +import org.hibernate.annotations.AnyDiscriminatorValue; |
| 8 | +import org.hibernate.annotations.AnyKeyJavaClass; |
| 9 | + |
| 10 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 11 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 12 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 13 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import jakarta.persistence.Column; |
| 17 | +import jakarta.persistence.DiscriminatorType; |
| 18 | +import jakarta.persistence.Entity; |
| 19 | +import jakarta.persistence.GeneratedValue; |
| 20 | +import jakarta.persistence.Id; |
| 21 | +import jakarta.persistence.JoinColumn; |
| 22 | +import jakarta.persistence.OneToOne; |
| 23 | + |
| 24 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 25 | + |
| 26 | +@DomainModel( |
| 27 | + annotatedClasses = { |
| 28 | + AnyMergeTest.InvoicePosition.class, |
| 29 | + AnyMergeTest.Bonus.class, |
| 30 | + AnyMergeTest.Fee.class, |
| 31 | + AnyMergeTest.Amount.class |
| 32 | + } |
| 33 | +) |
| 34 | +@SessionFactory |
| 35 | +@JiraKey("HHH-17621") |
| 36 | +public class AnyMergeTest { |
| 37 | + |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testMerge(SessionFactoryScope scope) { |
| 41 | + Amount amount = new Amount( new BigDecimal( 10 ) ); |
| 42 | + Bonus bonus = new Bonus( "that's a bonus", amount ); |
| 43 | + scope.inTransaction( |
| 44 | + session -> { |
| 45 | + session.persist( amount ); |
| 46 | + session.persist( bonus ); |
| 47 | + } |
| 48 | + ); |
| 49 | + |
| 50 | + scope.inTransaction( |
| 51 | + session -> { |
| 52 | + InvoicePosition invoicePosition = new InvoicePosition(); |
| 53 | + invoicePosition.setReference( bonus ); |
| 54 | + InvoicePosition merged = session.merge( invoicePosition ); |
| 55 | + |
| 56 | + Reference mergedReference = merged.getReference(); |
| 57 | + assertThat( mergedReference ).isExactlyInstanceOf( Bonus.class ); |
| 58 | + Bonus mergedBonus = (Bonus) mergedReference; |
| 59 | + // check the merged values are copies of the original ones |
| 60 | + assertThat( mergedBonus ).isNotEqualTo( bonus ); |
| 61 | + assertThat( mergedBonus.amount ).isNotEqualTo( amount ); |
| 62 | + |
| 63 | + assertThat( mergedBonus.amount.quantity.compareTo( new BigDecimal( 10 ) ) ).isEqualTo( 0 ); |
| 64 | + } |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + public interface Reference { |
| 69 | + } |
| 70 | + |
| 71 | + @Entity(name = "Bonus") |
| 72 | + public static class Bonus implements Reference { |
| 73 | + |
| 74 | + @Id |
| 75 | + @GeneratedValue |
| 76 | + private Long id; |
| 77 | + |
| 78 | + private String name; |
| 79 | + |
| 80 | + @OneToOne |
| 81 | + private Amount amount; |
| 82 | + |
| 83 | + public Bonus() { |
| 84 | + } |
| 85 | + |
| 86 | + public Bonus(String name, Amount amount) { |
| 87 | + this.name = name; |
| 88 | + this.amount = amount; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Entity(name = "Amount") |
| 93 | + public static class Amount { |
| 94 | + |
| 95 | + @Id |
| 96 | + @GeneratedValue |
| 97 | + private Long id; |
| 98 | + |
| 99 | + private BigDecimal quantity; |
| 100 | + |
| 101 | + public Amount() { |
| 102 | + } |
| 103 | + |
| 104 | + public Amount(BigDecimal quantity) { |
| 105 | + this.quantity = quantity; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Entity(name = "Fee") |
| 110 | + public static class Fee implements Reference { |
| 111 | + |
| 112 | + @Id |
| 113 | + @GeneratedValue |
| 114 | + private Long id; |
| 115 | + |
| 116 | + private String name; |
| 117 | + } |
| 118 | + |
| 119 | + @Entity(name = "InvoicePosition") |
| 120 | + public static class InvoicePosition { |
| 121 | + |
| 122 | + @Id |
| 123 | + @GeneratedValue |
| 124 | + private Long id; |
| 125 | + |
| 126 | + @Any |
| 127 | + @AnyDiscriminator(DiscriminatorType.STRING) |
| 128 | + @AnyDiscriminatorValue(discriminator = "BONUS", entity = Bonus.class) |
| 129 | + @AnyDiscriminatorValue(discriminator = "FEE", entity = Fee.class) |
| 130 | + @AnyKeyJavaClass(Long.class) |
| 131 | + @Column(name = "Type") |
| 132 | + @JoinColumn(name = "ReferenceId") |
| 133 | + private Reference reference; |
| 134 | + |
| 135 | + public Reference getReference() { |
| 136 | + return reference; |
| 137 | + } |
| 138 | + |
| 139 | + public void setReference(Reference reference) { |
| 140 | + this.reference = reference; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | +} |
0 commit comments