|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.jpa; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.FetchType; |
| 9 | +import jakarta.persistence.GeneratedValue; |
| 10 | +import jakarta.persistence.Id; |
| 11 | +import jakarta.persistence.JoinColumn; |
| 12 | +import jakarta.persistence.ManyToOne; |
| 13 | +import jakarta.persistence.OneToOne; |
| 14 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 15 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 16 | +import org.hibernate.testing.orm.junit.Jpa; |
| 17 | +import org.junit.jupiter.api.BeforeAll; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 23 | + |
| 24 | +@Jpa( |
| 25 | + annotatedClasses = { |
| 26 | + JpaProxyComplianceEnabledTest.Provider.class, |
| 27 | + JpaProxyComplianceEnabledTest.TelephoneNumber.class, |
| 28 | + JpaProxyComplianceEnabledTest.VoiceGroup.class, |
| 29 | + }, |
| 30 | + proxyComplianceEnabled = true |
| 31 | +) |
| 32 | +@JiraKey( "HHH-19476" ) |
| 33 | +public class JpaProxyComplianceEnabledTest { |
| 34 | + |
| 35 | + private static final Integer VOICE_GROUP_ID = 1; |
| 36 | + |
| 37 | + @BeforeAll |
| 38 | + public static void init(EntityManagerFactoryScope scope) { |
| 39 | + scope.inTransaction( |
| 40 | + entityManager -> { |
| 41 | + VoiceGroup voiceGroup = new VoiceGroup(VOICE_GROUP_ID); |
| 42 | + entityManager.persist( voiceGroup ); |
| 43 | + |
| 44 | + Provider provider = new Provider("A Provider"); |
| 45 | + entityManager.persist( provider ); |
| 46 | + |
| 47 | + TelephoneNumber telephoneNumber1 = new TelephoneNumber("123-456-7890",voiceGroup,provider); |
| 48 | + entityManager.persist( telephoneNumber1 ); |
| 49 | + } |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testJoinFetchAfterFind(EntityManagerFactoryScope scope) { |
| 55 | + scope.inTransaction( |
| 56 | + entityManager -> { |
| 57 | + VoiceGroup voiceGroup = entityManager.find(VoiceGroup.class, VOICE_GROUP_ID); |
| 58 | + List<TelephoneNumber> telephoneNumbers = entityManager.createQuery( |
| 59 | + "from TelephoneNumber t join fetch t.provider where t.voiceGroup = :voiceGroup", |
| 60 | + TelephoneNumber.class |
| 61 | + ) |
| 62 | + .setParameter( "voiceGroup", voiceGroup ) |
| 63 | + .getResultList(); |
| 64 | + assertThat(telephoneNumbers.size()).isEqualTo(1); |
| 65 | + } |
| 66 | + |
| 67 | + ); |
| 68 | + scope.inTransaction( |
| 69 | + entityManager -> { |
| 70 | + VoiceGroup voiceGroup = entityManager.find( VoiceGroup.class, VOICE_GROUP_ID); |
| 71 | + voiceGroup.getPrimaryNumber().getNumber(); |
| 72 | + List<TelephoneNumber> telephoneNumbers = entityManager.createQuery( |
| 73 | + "from TelephoneNumber t join fetch t.provider where t.voiceGroup = :voiceGroup", |
| 74 | + TelephoneNumber.class |
| 75 | + ) |
| 76 | + .setParameter( "voiceGroup", voiceGroup ) |
| 77 | + .getResultList(); |
| 78 | + assertThat(telephoneNumbers.size()).isEqualTo(1); |
| 79 | + } |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + @Entity(name = "VoiceGroup") |
| 84 | + public static class VoiceGroup { |
| 85 | + @Id |
| 86 | + private Integer id; |
| 87 | + @OneToOne |
| 88 | + @JoinColumn(name = "primaryNumber") |
| 89 | + private TelephoneNumber primaryNumber; |
| 90 | + |
| 91 | + public VoiceGroup() { |
| 92 | + } |
| 93 | + |
| 94 | + public VoiceGroup(Integer id) { |
| 95 | + this.id = id; |
| 96 | + } |
| 97 | + |
| 98 | + public Integer getId() { |
| 99 | + return id; |
| 100 | + } |
| 101 | + |
| 102 | + public TelephoneNumber getPrimaryNumber() { |
| 103 | + return primaryNumber; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Entity(name = "TelephoneNumber") |
| 108 | + public static class TelephoneNumber { |
| 109 | + @Id |
| 110 | + @GeneratedValue |
| 111 | + private Integer id; |
| 112 | + private String number; |
| 113 | + @ManyToOne(fetch = FetchType.LAZY) |
| 114 | + @JoinColumn(name = "voiceGroup", nullable = false) |
| 115 | + private VoiceGroup voiceGroup; |
| 116 | + @ManyToOne(fetch = FetchType.LAZY) |
| 117 | + @JoinColumn(name = "provider", nullable = false) |
| 118 | + private Provider provider; |
| 119 | + |
| 120 | + public TelephoneNumber() { |
| 121 | + } |
| 122 | + |
| 123 | + public TelephoneNumber(String number, VoiceGroup voiceGroup, Provider provider) { |
| 124 | + this.number = number; |
| 125 | + this.voiceGroup = voiceGroup; |
| 126 | + voiceGroup.primaryNumber = this; |
| 127 | + this.provider = provider; |
| 128 | + } |
| 129 | + public Integer getId() { |
| 130 | + return id; |
| 131 | + } |
| 132 | + |
| 133 | + public String getNumber() { |
| 134 | + return number; |
| 135 | + } |
| 136 | + |
| 137 | + public VoiceGroup getVoiceGroup() { |
| 138 | + return voiceGroup; |
| 139 | + } |
| 140 | + |
| 141 | + public Provider getProvider() { |
| 142 | + return provider; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @Entity(name = "Provider") |
| 147 | + public static class Provider { |
| 148 | + |
| 149 | + @Id |
| 150 | + @GeneratedValue |
| 151 | + private Integer id; |
| 152 | + private String name; |
| 153 | + |
| 154 | + public Provider() { |
| 155 | + } |
| 156 | + |
| 157 | + public Provider(String name) { |
| 158 | + this.name = name; |
| 159 | + } |
| 160 | + |
| 161 | + public Integer getId() { |
| 162 | + return id; |
| 163 | + } |
| 164 | + |
| 165 | + public String getName() { |
| 166 | + return name; |
| 167 | + } |
| 168 | + |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments