|
| 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.cid; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 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.CascadeType; |
| 17 | +import jakarta.persistence.Column; |
| 18 | +import jakarta.persistence.Entity; |
| 19 | +import jakarta.persistence.GeneratedValue; |
| 20 | +import jakarta.persistence.GenerationType; |
| 21 | +import jakarta.persistence.Id; |
| 22 | +import jakarta.persistence.IdClass; |
| 23 | +import jakarta.persistence.ManyToOne; |
| 24 | +import jakarta.persistence.OneToMany; |
| 25 | +import jakarta.persistence.Table; |
| 26 | + |
| 27 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 28 | + |
| 29 | +@DomainModel( |
| 30 | + annotatedClasses = { |
| 31 | + CompositeIdAndMergeTest.Order.class, |
| 32 | + CompositeIdAndMergeTest.Invoice.class, |
| 33 | + CompositeIdAndMergeTest.LineItem.class |
| 34 | + } |
| 35 | +) |
| 36 | +@SessionFactory |
| 37 | +@JiraKey("HHH-18131") |
| 38 | +public class CompositeIdAndMergeTest { |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testMerge(SessionFactoryScope scope) { |
| 42 | + Integer lineItemIndex = 2; |
| 43 | + Order persistedOrder = scope.fromTransaction( |
| 44 | + session -> { |
| 45 | + Order order = new Order( "order" ); |
| 46 | + session.persist( order ); |
| 47 | + |
| 48 | + Invoice invoice = new Invoice( "invoice" ); |
| 49 | + LineItem lineItem = new LineItem( lineItemIndex ); |
| 50 | + invoice.addLine( lineItem ); |
| 51 | + order.setInvoice( invoice ); |
| 52 | + |
| 53 | + session.merge( order ); |
| 54 | + return order; |
| 55 | + } |
| 56 | + ); |
| 57 | + |
| 58 | + scope.inTransaction( |
| 59 | + session -> { |
| 60 | + Order order = session.find( Order.class, persistedOrder.getId() ); |
| 61 | + Invoice invoice = order.getInvoice(); |
| 62 | + assertThat( invoice ).isNotNull(); |
| 63 | + List<LineItem> lines = invoice.getLines(); |
| 64 | + assertThat( lines.size() ).isEqualTo( 1 ); |
| 65 | + assertThat( lines.get( 0 ).getIndex() ).isEqualTo( lineItemIndex ); |
| 66 | + } |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + @Entity(name = "Order") |
| 71 | + @Table(name = "order_table") |
| 72 | + public static class Order { |
| 73 | + @Id |
| 74 | + @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 75 | + private Long id; |
| 76 | + |
| 77 | + @Column(nullable = false) |
| 78 | + private String description; |
| 79 | + |
| 80 | + @ManyToOne(cascade = { CascadeType.ALL }) |
| 81 | + private Invoice invoice; |
| 82 | + |
| 83 | + public Order() { |
| 84 | + } |
| 85 | + |
| 86 | + public Order(String description) { |
| 87 | + this.description = description; |
| 88 | + } |
| 89 | + |
| 90 | + public Long getId() { |
| 91 | + return id; |
| 92 | + } |
| 93 | + |
| 94 | + public Invoice getInvoice() { |
| 95 | + return invoice; |
| 96 | + } |
| 97 | + |
| 98 | + public void setInvoice(Invoice invoice) { |
| 99 | + this.invoice = invoice; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Entity(name = "Invoice") |
| 104 | + public static class Invoice { |
| 105 | + @Id |
| 106 | + @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 107 | + private Long id; |
| 108 | + |
| 109 | + @Column(name = "number_column") |
| 110 | + private String number; |
| 111 | + |
| 112 | + @OneToMany(mappedBy = "invoice", cascade = { CascadeType.ALL }, orphanRemoval = true) |
| 113 | + private List<LineItem> lines = new ArrayList<>(); |
| 114 | + |
| 115 | + public Invoice() { |
| 116 | + } |
| 117 | + |
| 118 | + public Invoice(String number) { |
| 119 | + this.number = number; |
| 120 | + } |
| 121 | + |
| 122 | + public void addLine(LineItem line) { |
| 123 | + lines.add( line ); |
| 124 | + line.invoice = this; |
| 125 | + } |
| 126 | + |
| 127 | + public List<LineItem> getLines() { |
| 128 | + return lines; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @Entity |
| 133 | + @Table(name = "invoice_lines") |
| 134 | + @IdClass(LineItemId.class) |
| 135 | + public static class LineItem { |
| 136 | + @Id |
| 137 | + @ManyToOne |
| 138 | + private Invoice invoice; |
| 139 | + |
| 140 | + @Id |
| 141 | + @Column(name = "index_column") |
| 142 | + private Integer index; |
| 143 | + |
| 144 | + public LineItem() { |
| 145 | + } |
| 146 | + |
| 147 | + public LineItem(Integer index) { |
| 148 | + this.index = index; |
| 149 | + } |
| 150 | + |
| 151 | + public Integer getIndex() { |
| 152 | + return index; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + public static class LineItemId { |
| 157 | + private Long invoice; |
| 158 | + private Integer index; |
| 159 | + |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments