|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.proxy.narrow; |
| 8 | + |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.Set; |
| 11 | + |
| 12 | +import org.hibernate.Hibernate; |
| 13 | + |
| 14 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 15 | +import org.hibernate.testing.orm.junit.Jira; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 18 | +import org.junit.jupiter.api.AfterAll; |
| 19 | +import org.junit.jupiter.api.BeforeAll; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import jakarta.persistence.Entity; |
| 23 | +import jakarta.persistence.FetchType; |
| 24 | +import jakarta.persistence.Id; |
| 25 | +import jakarta.persistence.JoinColumn; |
| 26 | +import jakarta.persistence.ManyToOne; |
| 27 | +import jakarta.persistence.OneToMany; |
| 28 | + |
| 29 | +import static jakarta.persistence.FetchType.LAZY; |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | + |
| 32 | +/** |
| 33 | + * @author Marco Belladelli |
| 34 | + */ |
| 35 | +@DomainModel( annotatedClasses = { |
| 36 | + BidirectionalManyToOneNarrowingTest.Address.class, |
| 37 | + BidirectionalManyToOneNarrowingTest.MyAddress.class, |
| 38 | + BidirectionalManyToOneNarrowingTest.Message.class, |
| 39 | + BidirectionalManyToOneNarrowingTest.AddressContainer.class |
| 40 | +} ) |
| 41 | +@SessionFactory |
| 42 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-17594" ) |
| 43 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-17665" ) |
| 44 | +public class BidirectionalManyToOneNarrowingTest { |
| 45 | + @BeforeAll |
| 46 | + public void setUp(SessionFactoryScope scope) { |
| 47 | + scope.inTransaction( session -> { |
| 48 | + final MyAddress address = new MyAddress( 1L ); |
| 49 | + session.persist( address ); |
| 50 | + session.persist( new Message( 2L, address ) ); |
| 51 | + final AddressContainer relation = new AddressContainer(); |
| 52 | + relation.setId( 3L ); |
| 53 | + relation.setMyAddress( address ); |
| 54 | + session.persist( relation ); |
| 55 | + } ); |
| 56 | + } |
| 57 | + |
| 58 | + @AfterAll |
| 59 | + public void tearDown(SessionFactoryScope scope) { |
| 60 | + scope.inTransaction( session -> { |
| 61 | + session.createMutationQuery( "delete from Message" ).executeUpdate(); |
| 62 | + session.createMutationQuery( "delete from AddressContainer" ).executeUpdate(); |
| 63 | + session.createMutationQuery( "delete from Address" ).executeUpdate(); |
| 64 | + } ); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testQuery(SessionFactoryScope scope) { |
| 69 | + scope.inTransaction( session -> { |
| 70 | + final Message result = session.createQuery( "from Message", Message.class ).getSingleResult(); |
| 71 | + assertResult( result ); |
| 72 | + } ); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testFind(SessionFactoryScope scope) { |
| 77 | + scope.inTransaction( session -> { |
| 78 | + final Message result = session.find( Message.class, 2L ); |
| 79 | + assertResult( result ); |
| 80 | + } ); |
| 81 | + } |
| 82 | + |
| 83 | + private void assertResult(Message result) { |
| 84 | + final Address address = result.getReceiverAddress(); |
| 85 | + assertThat( Hibernate.isInitialized( address ) ).isFalse(); |
| 86 | + assertThat( Hibernate.getClass( address ) ).isEqualTo( MyAddress.class ); |
| 87 | + final MyAddress myAddress = (MyAddress) Hibernate.unproxy( address ); |
| 88 | + final Set<AddressContainer> relations = myAddress.getAddressUserRelations(); |
| 89 | + assertThat( relations ).hasSize( 1 ); |
| 90 | + final MyAddress relatedAddress = relations.iterator().next().getMyAddress(); |
| 91 | + assertThat( relatedAddress.getId() ).isEqualTo( 1L ); |
| 92 | + assertThat( relatedAddress ).isSameAs( myAddress ); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testProxyReuse(SessionFactoryScope scope) { |
| 97 | + // uninitialized proxy |
| 98 | + scope.inTransaction( session -> { |
| 99 | + final Address address = session.getReference( Address.class, 1L ); |
| 100 | + assertThat( Hibernate.isInitialized( address ) ).isFalse(); |
| 101 | + final AddressContainer addressContainer = session.find( AddressContainer.class, 3L ); |
| 102 | + final MyAddress myAddress = addressContainer.getMyAddress(); |
| 103 | + assertThat( Hibernate.isInitialized( myAddress ) ).isFalse(); |
| 104 | + assertThat( myAddress.getId() ).isEqualTo( address.getId() ); |
| 105 | + } ); |
| 106 | + // initialized proxy |
| 107 | + scope.inTransaction( session -> { |
| 108 | + final Address address = session.getReference( Address.class, 1L ); |
| 109 | + assertThat( Hibernate.getClass( address ) ).isEqualTo( MyAddress.class ); |
| 110 | + assertThat( Hibernate.isInitialized( address ) ).isTrue(); |
| 111 | + final AddressContainer addressContainer = session.find( AddressContainer.class, 3L ); |
| 112 | + final MyAddress myAddress = addressContainer.getMyAddress(); |
| 113 | + assertThat( Hibernate.isInitialized( myAddress ) ).isTrue(); |
| 114 | + assertThat( myAddress.getId() ).isEqualTo( address.getId() ); |
| 115 | + } ); |
| 116 | + } |
| 117 | + |
| 118 | + @Entity( name = "Address" ) |
| 119 | + public static abstract class Address { |
| 120 | + @Id |
| 121 | + private Long id; |
| 122 | + |
| 123 | + public Address() { |
| 124 | + } |
| 125 | + |
| 126 | + public Address(Long id) { |
| 127 | + this.id = id; |
| 128 | + } |
| 129 | + |
| 130 | + public Long getId() { |
| 131 | + return id; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Entity( name = "Message" ) |
| 136 | + public static class Message { |
| 137 | + @Id |
| 138 | + private Long id; |
| 139 | + |
| 140 | + @ManyToOne( fetch = FetchType.LAZY ) |
| 141 | + @JoinColumn( name = "receiver_address_id" ) |
| 142 | + private Address receiverAddress; |
| 143 | + |
| 144 | + public Message() { |
| 145 | + } |
| 146 | + |
| 147 | + public Message(Long id, Address receiverAddress) { |
| 148 | + this.id = id; |
| 149 | + this.receiverAddress = receiverAddress; |
| 150 | + } |
| 151 | + |
| 152 | + public Address getReceiverAddress() { |
| 153 | + return receiverAddress; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + @Entity( name = "MyAddress" ) |
| 158 | + public static class MyAddress extends Address { |
| 159 | + @OneToMany( mappedBy = "myAddress" ) |
| 160 | + private Set<AddressContainer> addressContainers = new HashSet<>(); |
| 161 | + |
| 162 | + public MyAddress() { |
| 163 | + } |
| 164 | + |
| 165 | + public MyAddress(Long id) { |
| 166 | + super( id ); |
| 167 | + } |
| 168 | + |
| 169 | + public Set<AddressContainer> getAddressUserRelations() { |
| 170 | + return addressContainers; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + @Entity( name = "AddressContainer" ) |
| 175 | + public static class AddressContainer { |
| 176 | + @Id |
| 177 | + private Long id; |
| 178 | + |
| 179 | + @ManyToOne( fetch = LAZY ) |
| 180 | + @JoinColumn( name = "address_id" ) |
| 181 | + private MyAddress myAddress; |
| 182 | + |
| 183 | + public Long getId() { |
| 184 | + return id; |
| 185 | + } |
| 186 | + |
| 187 | + public void setId(Long id) { |
| 188 | + this.id = id; |
| 189 | + } |
| 190 | + |
| 191 | + public MyAddress getMyAddress() { |
| 192 | + return myAddress; |
| 193 | + } |
| 194 | + |
| 195 | + public void setMyAddress(MyAddress myAddress) { |
| 196 | + this.myAddress = myAddress; |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments