|
| 1 | +package org.hibernate.orm.test.embeddable; |
| 2 | + |
| 3 | +import java.io.Serializable; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.Collection; |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.Objects; |
| 8 | +import java.util.Set; |
| 9 | + |
| 10 | +import org.hibernate.annotations.Fetch; |
| 11 | +import org.hibernate.annotations.FetchMode; |
| 12 | + |
| 13 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 16 | +import org.junit.jupiter.api.BeforeAll; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import jakarta.persistence.CascadeType; |
| 20 | +import jakarta.persistence.Column; |
| 21 | +import jakarta.persistence.ElementCollection; |
| 22 | +import jakarta.persistence.Entity; |
| 23 | +import jakarta.persistence.FetchType; |
| 24 | +import jakarta.persistence.GeneratedValue; |
| 25 | +import jakarta.persistence.Id; |
| 26 | +import jakarta.persistence.JoinColumn; |
| 27 | +import jakarta.persistence.ManyToOne; |
| 28 | +import jakarta.persistence.Table; |
| 29 | +import org.assertj.core.api.Assertions; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | + |
| 33 | +@DomainModel(annotatedClasses = { |
| 34 | + MergeWithReferenceTest.Foo.class, |
| 35 | + MergeWithReferenceTest.Bar.class |
| 36 | +}) |
| 37 | +@SessionFactory |
| 38 | +public class MergeWithReferenceTest { |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testMergeReference(SessionFactoryScope scope) { |
| 42 | + Bar bar = new Bar( "unique3" ); |
| 43 | + scope.inTransaction( session -> { |
| 44 | + session.persist( bar ); |
| 45 | + } ); |
| 46 | + scope.inTransaction( session -> { |
| 47 | + Foo merged = session.merge( new Foo( session.getReference( Bar.class, bar.getId() ) ) ); |
| 48 | + Assertions.assertThat( merged.getBar().getKey() ).isEqualTo( bar.getKey() ); |
| 49 | + } ); |
| 50 | + } |
| 51 | + |
| 52 | + @Entity(name = "Foo") |
| 53 | + static class Foo { |
| 54 | + Foo(Bar bar) { |
| 55 | + this.bar = bar; |
| 56 | + } |
| 57 | + |
| 58 | + Foo() { |
| 59 | + } |
| 60 | + |
| 61 | + @Id |
| 62 | + @GeneratedValue |
| 63 | + long id; |
| 64 | + @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER) |
| 65 | + @Fetch(FetchMode.JOIN) |
| 66 | + @JoinColumn(name = "bar_key", referencedColumnName = "nat_key") |
| 67 | + Bar bar; |
| 68 | + |
| 69 | + public long getId() { |
| 70 | + return id; |
| 71 | + } |
| 72 | + |
| 73 | + public void setId(long id) { |
| 74 | + this.id = id; |
| 75 | + } |
| 76 | + |
| 77 | + public Bar getBar() { |
| 78 | + return bar; |
| 79 | + } |
| 80 | + |
| 81 | + public void setBar(Bar bar) { |
| 82 | + this.bar = bar; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Entity(name = "Bar") |
| 87 | + static class Bar implements Serializable { |
| 88 | + Bar(String key) { |
| 89 | + this.key = key; |
| 90 | + } |
| 91 | + |
| 92 | + Bar() { |
| 93 | + } |
| 94 | + |
| 95 | + @Id |
| 96 | + @GeneratedValue |
| 97 | + long id; |
| 98 | + @Column(name = "nat_key", unique = true) |
| 99 | + String key; |
| 100 | + |
| 101 | + public long getId() { |
| 102 | + return id; |
| 103 | + } |
| 104 | + |
| 105 | + public void setId(long id) { |
| 106 | + this.id = id; |
| 107 | + } |
| 108 | + |
| 109 | + public String getKey() { |
| 110 | + return key; |
| 111 | + } |
| 112 | + |
| 113 | + public void setKey(String key) { |
| 114 | + this.key = key; |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments