|
| 1 | +package org.hibernate.test.annotations.various; |
| 2 | + |
| 3 | +import javax.persistence.Entity; |
| 4 | +import javax.persistence.Id; |
| 5 | +import javax.persistence.OneToOne; |
| 6 | +import javax.persistence.PrimaryKeyJoinColumn; |
| 7 | +import javax.persistence.Table; |
| 8 | + |
| 9 | +import org.hibernate.annotations.Generated; |
| 10 | +import org.hibernate.annotations.GenerationTime; |
| 11 | +import org.hibernate.annotations.Subselect; |
| 12 | + |
| 13 | +import org.hibernate.testing.TestForIssue; |
| 14 | +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | + |
| 19 | +@TestForIssue(jiraKey = "HHH-15520") |
| 20 | +public class OneOneGeneratedValueTest extends BaseCoreFunctionalTestCase { |
| 21 | + |
| 22 | + @Override |
| 23 | + protected Class<?>[] getAnnotatedClasses() { |
| 24 | + return new Class[] { |
| 25 | + EntityA.class, |
| 26 | + EntityB.class |
| 27 | + }; |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void testIt() { |
| 32 | + inTransaction( |
| 33 | + session -> { |
| 34 | + EntityA entityA = new EntityA( 1l ); |
| 35 | + session.persist( entityA ); |
| 36 | + } |
| 37 | + ); |
| 38 | + inTransaction( |
| 39 | + session -> { |
| 40 | + EntityA entityA = session.get( EntityA.class, 1l ); |
| 41 | + assertThat( entityA ).isNotNull(); |
| 42 | + EntityB entityB = entityA.getB(); |
| 43 | + assertThat( entityB ).isNotNull(); |
| 44 | + assertThat( entityB.getB() ).isEqualTo( 5l ); |
| 45 | + } |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + @Entity(name = "EntityA") |
| 50 | + @Table(name = "TABLE_A") |
| 51 | + public static class EntityA { |
| 52 | + |
| 53 | + @Id |
| 54 | + private Long id; |
| 55 | + |
| 56 | + private String name; |
| 57 | + |
| 58 | + @Generated(GenerationTime.INSERT) |
| 59 | + @OneToOne(mappedBy = "a") |
| 60 | + private EntityB b; |
| 61 | + |
| 62 | + public EntityA() { |
| 63 | + } |
| 64 | + |
| 65 | + public EntityA(Long id) { |
| 66 | + this.id = id; |
| 67 | + } |
| 68 | + |
| 69 | + public Long getId() { |
| 70 | + return id; |
| 71 | + } |
| 72 | + |
| 73 | + public EntityB getB() { |
| 74 | + return b; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Entity(name = "EntityB") |
| 79 | + @Subselect("SELECT 5 as b, a.id AS AId FROM TABLE_A a") |
| 80 | + public static class EntityB { |
| 81 | + |
| 82 | + private Long aId; |
| 83 | + |
| 84 | + private EntityA a; |
| 85 | + |
| 86 | + private Long b; |
| 87 | + |
| 88 | + @Id |
| 89 | + public Long getAId() { |
| 90 | + return aId; |
| 91 | + } |
| 92 | + |
| 93 | + public void setAId(Long aId) { |
| 94 | + this.aId = aId; |
| 95 | + } |
| 96 | + |
| 97 | + @OneToOne |
| 98 | + @PrimaryKeyJoinColumn |
| 99 | + public EntityA getA() { |
| 100 | + return a; |
| 101 | + } |
| 102 | + |
| 103 | + public void setA(EntityA a) { |
| 104 | + this.a = a; |
| 105 | + } |
| 106 | + |
| 107 | + public Long getB() { |
| 108 | + return b; |
| 109 | + } |
| 110 | + |
| 111 | + public void setB(Long b) { |
| 112 | + this.b = b; |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + } |
| 117 | +} |
0 commit comments