|
| 1 | +package org.hibernate.orm.test.merge; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 7 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 8 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 9 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +import jakarta.persistence.CascadeType; |
| 13 | +import jakarta.persistence.Column; |
| 14 | +import jakarta.persistence.Embeddable; |
| 15 | +import jakarta.persistence.EmbeddedId; |
| 16 | +import jakarta.persistence.Entity; |
| 17 | +import jakarta.persistence.GeneratedValue; |
| 18 | +import jakarta.persistence.Id; |
| 19 | +import jakarta.persistence.JoinColumn; |
| 20 | +import jakarta.persistence.ManyToOne; |
| 21 | +import jakarta.persistence.OneToMany; |
| 22 | +import jakarta.persistence.Table; |
| 23 | + |
| 24 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 25 | + |
| 26 | +@DomainModel( |
| 27 | + annotatedClasses = { |
| 28 | + CompositeIdWithAssociationsAndGeneratedValuesMerge2Test.Middle.class, |
| 29 | + CompositeIdWithAssociationsAndGeneratedValuesMerge2Test.Bottom.class |
| 30 | + } |
| 31 | +) |
| 32 | +@SessionFactory |
| 33 | +@JiraKey( "HHH-17634" ) |
| 34 | +public class CompositeIdWithAssociationsAndGeneratedValuesMerge2Test { |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testMerge(SessionFactoryScope scope) { |
| 38 | + |
| 39 | + scope.inTransaction( |
| 40 | + session -> { |
| 41 | + Middle m1 = new Middle( "Middle" ); |
| 42 | + Bottom bottom = new Bottom( m1, 0, "Bottom" ); |
| 43 | + Middle merge = session.merge( m1 ); |
| 44 | + assertThat( merge.getId() ).isNotNull(); |
| 45 | + assertThat( m1.getId() ).isNull(); |
| 46 | + } |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + @Entity(name = "Middle") |
| 51 | + @Table(name = "middle_table") |
| 52 | + public static class Middle { |
| 53 | + @Id |
| 54 | + @GeneratedValue |
| 55 | + private Long id; |
| 56 | + |
| 57 | + private String name; |
| 58 | + |
| 59 | + @OneToMany(mappedBy = "middle", cascade = { CascadeType.MERGE, CascadeType.PERSIST }) |
| 60 | + private List<Bottom> bottoms; |
| 61 | + |
| 62 | + public Middle() { |
| 63 | + } |
| 64 | + |
| 65 | + public Middle(String name) { |
| 66 | + this.name = name; |
| 67 | + } |
| 68 | + |
| 69 | + public Long getId() { |
| 70 | + return id; |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + public String getName() { |
| 75 | + return name; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + public List<Bottom> getBottoms() { |
| 80 | + return bottoms; |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + public void addBottom(Bottom bottom) { |
| 85 | + if ( bottoms == null ) { |
| 86 | + bottoms = new ArrayList<>(); |
| 87 | + } |
| 88 | + bottoms.add( bottom ); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Entity(name = "Bottom") |
| 93 | + @Table(name = "bottom_table") |
| 94 | + public static class Bottom { |
| 95 | + @Id |
| 96 | + @ManyToOne(optional = false, cascade = CascadeType.MERGE) |
| 97 | + @JoinColumn(name = "middle_id", nullable = false) |
| 98 | + private Middle middle; |
| 99 | + |
| 100 | + @Id |
| 101 | + @Column(name = "type_column") |
| 102 | + private Integer type; |
| 103 | + |
| 104 | + private String note; |
| 105 | + |
| 106 | + public Bottom() { |
| 107 | + } |
| 108 | + |
| 109 | + public Bottom(Middle middle, Integer type,String note) { |
| 110 | + this.middle = middle; |
| 111 | + this.middle.addBottom( this ); |
| 112 | + this.type = type; |
| 113 | + this.note = note; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments