|
| 1 | +package org.hibernate.orm.test.annotations.naturalid; |
| 2 | + |
| 3 | +import java.math.BigDecimal; |
| 4 | + |
| 5 | +import org.hibernate.annotations.NaturalId; |
| 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.Test; |
| 12 | + |
| 13 | +import jakarta.persistence.Column; |
| 14 | +import jakarta.persistence.Embeddable; |
| 15 | +import jakarta.persistence.Embedded; |
| 16 | +import jakarta.persistence.Entity; |
| 17 | +import jakarta.persistence.FetchType; |
| 18 | +import jakarta.persistence.Id; |
| 19 | +import jakarta.persistence.JoinColumn; |
| 20 | +import jakarta.persistence.ManyToOne; |
| 21 | +import jakarta.persistence.Table; |
| 22 | + |
| 23 | +@DomainModel( |
| 24 | + annotatedClasses = { |
| 25 | + NaturalIdAndAssociationTest.PositionEntity.class, |
| 26 | + NaturalIdAndAssociationTest.ZCurrencyEntity1.class |
| 27 | + }) |
| 28 | +@SessionFactory |
| 29 | +@Jira("HHH-18338") |
| 30 | +public class NaturalIdAndAssociationTest { |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testPersist(SessionFactoryScope scope) { |
| 34 | + scope.inTransaction( |
| 35 | + session -> { |
| 36 | + ZCurrencyEntity1 currency = new ZCurrencyEntity1( 1l, "USD" ); |
| 37 | + Amount amount = new Amount( new BigDecimal( "100.00" ), currency ); |
| 38 | + Holding holding = new Holding( amount ); |
| 39 | + PositionEntity positionEntity = new PositionEntity( 1l, "1", holding ); |
| 40 | + |
| 41 | + session.persist( currency ); |
| 42 | + session.persist( positionEntity ); |
| 43 | + } |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + @Entity(name = "PositionEntity") |
| 48 | + @Table(name = "POSITION_TABLE") |
| 49 | + public static class PositionEntity { |
| 50 | + @Id |
| 51 | + private Long id; |
| 52 | + |
| 53 | + private String name; |
| 54 | + |
| 55 | + @Embedded |
| 56 | + private Holding holding; |
| 57 | + |
| 58 | + public PositionEntity() { |
| 59 | + } |
| 60 | + |
| 61 | + public PositionEntity(Long id, String name, Holding holding) { |
| 62 | + this.id = id; |
| 63 | + this.name = name; |
| 64 | + this.holding = holding; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Embeddable |
| 69 | + public static class Holding { |
| 70 | + @Embedded |
| 71 | + private Amount amount; |
| 72 | + |
| 73 | + public Holding() { |
| 74 | + } |
| 75 | + |
| 76 | + public Holding(Amount amount) { |
| 77 | + this.amount = amount; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Embeddable |
| 82 | + public static class Amount { |
| 83 | + |
| 84 | + private BigDecimal quantity; |
| 85 | + |
| 86 | + @ManyToOne(fetch = FetchType.LAZY) |
| 87 | + @JoinColumn(referencedColumnName = "isoCode", nullable = false) |
| 88 | + private ZCurrencyEntity1 currency; |
| 89 | + |
| 90 | + public Amount() { |
| 91 | + } |
| 92 | + |
| 93 | + public Amount(BigDecimal quantity, ZCurrencyEntity1 currency) { |
| 94 | + this.quantity = quantity; |
| 95 | + this.currency = currency; |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + @Entity(name = "ZCurrencyEntity") |
| 101 | + @Table(name = "CURRENCY") |
| 102 | + public static class ZCurrencyEntity1 { |
| 103 | + @Id |
| 104 | + private Long id; |
| 105 | + |
| 106 | + @NaturalId |
| 107 | + private String isoCode; |
| 108 | + |
| 109 | + public ZCurrencyEntity1() { |
| 110 | + } |
| 111 | + |
| 112 | + public ZCurrencyEntity1(Long id, String isoCode) { |
| 113 | + this.id = id; |
| 114 | + this.isoCode = isoCode; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments