|
| 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.inheritance; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import org.hibernate.testing.jdbc.SQLStatementInspector; |
| 12 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 13 | +import org.hibernate.testing.orm.junit.Jira; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 16 | +import org.junit.jupiter.api.AfterAll; |
| 17 | +import org.junit.jupiter.api.BeforeAll; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import jakarta.persistence.DiscriminatorColumn; |
| 21 | +import jakarta.persistence.DiscriminatorValue; |
| 22 | +import jakarta.persistence.Entity; |
| 23 | +import jakarta.persistence.GeneratedValue; |
| 24 | +import jakarta.persistence.Id; |
| 25 | +import jakarta.persistence.Inheritance; |
| 26 | +import jakarta.persistence.InheritanceType; |
| 27 | +import jakarta.persistence.ManyToOne; |
| 28 | +import jakarta.persistence.OneToOne; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | + |
| 32 | +/** |
| 33 | + * @author Marco Belladelli |
| 34 | + */ |
| 35 | +@DomainModel( annotatedClasses = { |
| 36 | + ToOneSingleTableInheritanceAutoFlushTest.SiteUser.class, |
| 37 | + ToOneSingleTableInheritanceAutoFlushTest.Profile.class, |
| 38 | + ToOneSingleTableInheritanceAutoFlushTest.CommunityProfile.class, |
| 39 | +} ) |
| 40 | +@SessionFactory( useCollectingStatementInspector = true ) |
| 41 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-17686" ) |
| 42 | +public class ToOneSingleTableInheritanceAutoFlushTest { |
| 43 | + @BeforeAll |
| 44 | + public void setUp(SessionFactoryScope scope) { |
| 45 | + scope.inTransaction( session -> { |
| 46 | + final Profile profile = new Profile( 1L, "profile_1" ); |
| 47 | + session.persist( profile ); |
| 48 | + final CommunityProfile communityProfile = new CommunityProfile( 2L, "community_2", "community_2" ); |
| 49 | + session.persist( communityProfile ); |
| 50 | + final SiteUser siteUser = new SiteUser(); |
| 51 | + siteUser.setProfile( profile ); |
| 52 | + siteUser.setCommunityProfile( communityProfile ); |
| 53 | + session.persist( siteUser ); |
| 54 | + } ); |
| 55 | + } |
| 56 | + |
| 57 | + @AfterAll |
| 58 | + public void tearDown(SessionFactoryScope scope) { |
| 59 | + scope.inTransaction( session -> { |
| 60 | + session.createMutationQuery( "delete from SiteUser" ).executeUpdate(); |
| 61 | + session.createMutationQuery( "delete from Profile" ).executeUpdate(); |
| 62 | + } ); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testSupertypeAutoFlush(SessionFactoryScope scope) { |
| 67 | + final SQLStatementInspector inspector = scope.getCollectingStatementInspector(); |
| 68 | + scope.inTransaction( session -> { |
| 69 | + final Profile profile = session.find( Profile.class, 1L ); |
| 70 | + inspector.clear(); |
| 71 | + |
| 72 | + profile.setName( "new_profile_1" ); |
| 73 | + final List<SiteUser> resultList = session.createQuery( |
| 74 | + "from SiteUser u join u.profile p where p.name = 'new_profile_1'", |
| 75 | + SiteUser.class |
| 76 | + ).getResultList(); |
| 77 | + assertThat( resultList ).hasSize( 1 ); |
| 78 | + assertThat( resultList.get( 0 ).getProfile().getName() ).isEqualTo( "new_profile_1" ); |
| 79 | + inspector.assertIsUpdate( 0 ); |
| 80 | + inspector.assertIsSelect( 1 ); |
| 81 | + } ); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testSubtypeAutoFlush(SessionFactoryScope scope) { |
| 86 | + final SQLStatementInspector inspector = scope.getCollectingStatementInspector(); |
| 87 | + scope.inTransaction( session -> { |
| 88 | + final CommunityProfile communityProfile = session.find( CommunityProfile.class, 2L ); |
| 89 | + inspector.clear(); |
| 90 | + |
| 91 | + communityProfile.setCommunity( "new_community_2" ); |
| 92 | + final List<SiteUser> resultList = session.createQuery( |
| 93 | + "from SiteUser u join u.communityProfile p where p.community = 'new_community_2'", |
| 94 | + SiteUser.class |
| 95 | + ).getResultList(); |
| 96 | + assertThat( resultList ).hasSize( 1 ); |
| 97 | + assertThat( resultList.get( 0 ).getCommunityProfile().getCommunity() ).isEqualTo( "new_community_2" ); |
| 98 | + inspector.assertIsUpdate( 0 ); |
| 99 | + inspector.assertIsSelect( 1 ); |
| 100 | + } ); |
| 101 | + } |
| 102 | + |
| 103 | + @Entity( name = "SiteUser" ) |
| 104 | + public static class SiteUser { |
| 105 | + @Id |
| 106 | + @GeneratedValue |
| 107 | + private Long id; |
| 108 | + |
| 109 | + @ManyToOne |
| 110 | + private Profile profile; |
| 111 | + |
| 112 | + @OneToOne |
| 113 | + private CommunityProfile communityProfile; |
| 114 | + |
| 115 | + public Profile getProfile() { |
| 116 | + return profile; |
| 117 | + } |
| 118 | + |
| 119 | + public void setProfile(Profile profile) { |
| 120 | + this.profile = profile; |
| 121 | + } |
| 122 | + |
| 123 | + public CommunityProfile getCommunityProfile() { |
| 124 | + return communityProfile; |
| 125 | + } |
| 126 | + |
| 127 | + public void setCommunityProfile(CommunityProfile communityProfile) { |
| 128 | + this.communityProfile = communityProfile; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @Entity( name = "Profile" ) |
| 133 | + @Inheritance( strategy = InheritanceType.SINGLE_TABLE ) |
| 134 | + @DiscriminatorColumn( name = "disc_col" ) |
| 135 | + @DiscriminatorValue( "profile" ) |
| 136 | + public static class Profile { |
| 137 | + @Id |
| 138 | + private Long id; |
| 139 | + |
| 140 | + private String name; |
| 141 | + |
| 142 | + public Profile() { |
| 143 | + } |
| 144 | + |
| 145 | + public Profile(Long id, String name) { |
| 146 | + this.id = id; |
| 147 | + this.name = name; |
| 148 | + } |
| 149 | + |
| 150 | + public Long getId() { |
| 151 | + return id; |
| 152 | + } |
| 153 | + |
| 154 | + public String getName() { |
| 155 | + return name; |
| 156 | + } |
| 157 | + |
| 158 | + public void setName(String profileName) { |
| 159 | + this.name = profileName; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + @Entity( name = "CommunityProfile" ) |
| 164 | + @DiscriminatorValue( "community" ) |
| 165 | + public static class CommunityProfile extends Profile { |
| 166 | + private String community; |
| 167 | + |
| 168 | + public CommunityProfile() { |
| 169 | + } |
| 170 | + |
| 171 | + public CommunityProfile(Long id, String name, String community) { |
| 172 | + super( id, name ); |
| 173 | + this.community = community; |
| 174 | + } |
| 175 | + |
| 176 | + public String getCommunity() { |
| 177 | + return community; |
| 178 | + } |
| 179 | + |
| 180 | + public void setCommunity(String community) { |
| 181 | + this.community = community; |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments