|
| 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.mapping.fetch.depth.form; |
| 8 | + |
| 9 | +import java.io.Serializable; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Set; |
| 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.CascadeType; |
| 23 | +import jakarta.persistence.Embeddable; |
| 24 | +import jakarta.persistence.EmbeddedId; |
| 25 | +import jakarta.persistence.Entity; |
| 26 | +import jakarta.persistence.FetchType; |
| 27 | +import jakarta.persistence.ForeignKey; |
| 28 | +import jakarta.persistence.GeneratedValue; |
| 29 | +import jakarta.persistence.Id; |
| 30 | +import jakarta.persistence.JoinColumn; |
| 31 | +import jakarta.persistence.JoinColumns; |
| 32 | +import jakarta.persistence.ManyToOne; |
| 33 | +import jakarta.persistence.OneToMany; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 37 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 38 | + |
| 39 | +/** |
| 40 | + * @author Marco Belladelli |
| 41 | + */ |
| 42 | +@SessionFactory |
| 43 | +@DomainModel( annotatedClasses = { |
| 44 | + AbstractFormFetchDepthTest.Form.class, |
| 45 | + AbstractFormFetchDepthTest.FormOption.class, |
| 46 | + AbstractFormFetchDepthTest.FormVersion.class, |
| 47 | + AbstractFormFetchDepthTest.FormVersionId.class, |
| 48 | + AbstractFormFetchDepthTest.StepConfiguration.class, |
| 49 | +} ) |
| 50 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-16871" ) |
| 51 | +public abstract class AbstractFormFetchDepthTest { |
| 52 | + @BeforeAll |
| 53 | + public void setUp(SessionFactoryScope scope) { |
| 54 | + scope.inTransaction( session -> { |
| 55 | + final Form form = new Form( new FormInfo( "test_form" ) ); |
| 56 | + session.persist( form ); |
| 57 | + final FormVersion formVersion = new FormVersion( form, 1 ); |
| 58 | + session.persist( formVersion ); |
| 59 | + final StepConfiguration stepConfiguration = new StepConfiguration( 1L ); |
| 60 | + final FormOption formOption = new FormOption( 2L, stepConfiguration, formVersion ); |
| 61 | + stepConfiguration.getFormOptions().add( formOption ); |
| 62 | + session.persist( stepConfiguration ); |
| 63 | + } ); |
| 64 | + } |
| 65 | + |
| 66 | + @AfterAll |
| 67 | + public void tearDown(SessionFactoryScope scope) { |
| 68 | + scope.inTransaction( session -> { |
| 69 | + session.createMutationQuery( "delete from FormOption" ).executeUpdate(); |
| 70 | + session.createMutationQuery( "delete from FormVersion" ).executeUpdate(); |
| 71 | + session.createMutationQuery( "delete from Form" ).executeUpdate(); |
| 72 | + session.createMutationQuery( "delete from StepConfiguration" ).executeUpdate(); |
| 73 | + } ); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void formVersionLoadTest(SessionFactoryScope scope) { |
| 78 | + scope.inTransaction( session -> { |
| 79 | + final StepConfiguration configuration = session.createQuery( |
| 80 | + "SELECT s FROM StepConfiguration s WHERE s.id = :id", |
| 81 | + StepConfiguration.class |
| 82 | + ).setParameter( "id", 1L ).getSingleResult(); |
| 83 | + assertNotNull( configuration.getFormOptions() ); |
| 84 | + assertEquals( 1, configuration.getFormOptions().size() ); |
| 85 | + configuration.getFormOptions().forEach( formOption -> { |
| 86 | + final FormVersion fv = formOption.getFormVersion(); |
| 87 | + assertThat( fv ).isNotNull(); |
| 88 | + assertThat( fv.getId().getForm() ).isNotNull(); |
| 89 | + assertThat( fv.getId().getForm().getFormInfo().getName() ).isEqualTo( "test_form" ); |
| 90 | + } ); |
| 91 | + } ); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void formOptionsLoadTest(SessionFactoryScope scope) { |
| 96 | + scope.inTransaction( session -> { |
| 97 | + final List<FormOption> fos = session.createQuery( |
| 98 | + "from FormOption fo", |
| 99 | + FormOption.class |
| 100 | + ).getResultList(); |
| 101 | + assertEquals( 1, fos.size() ); |
| 102 | + fos.forEach( formOption -> { |
| 103 | + final FormVersion fv = formOption.getFormVersion(); |
| 104 | + assertThat( fv ).isNotNull(); |
| 105 | + assertThat( fv.getId().getForm() ).isNotNull(); |
| 106 | + assertThat( fv.getId().getForm().getFormInfo().getName() ).isEqualTo( "test_form" ); |
| 107 | + } ); |
| 108 | + } ); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void singleFormOptionLoadTest(SessionFactoryScope scope) { |
| 113 | + scope.inTransaction( session -> { |
| 114 | + final FormOption fo = session.createQuery( |
| 115 | + "from FormOption fo where fo.id = :id", |
| 116 | + FormOption.class |
| 117 | + ).setParameter( "id", 2L ).getSingleResult(); |
| 118 | + final FormVersion fv = fo.getFormVersion(); |
| 119 | + assertThat( fv ).isNotNull(); |
| 120 | + assertThat( fv.getId().getForm() ).isNotNull(); |
| 121 | + assertThat( fv.getId().getForm().getFormInfo().getName() ).isEqualTo( "test_form" ); |
| 122 | + } ); |
| 123 | + } |
| 124 | + |
| 125 | + @Embeddable |
| 126 | + public static class FormInfo implements Serializable { |
| 127 | + private String name; |
| 128 | + |
| 129 | + public FormInfo() { |
| 130 | + } |
| 131 | + |
| 132 | + public FormInfo(String name) { |
| 133 | + this.name = name; |
| 134 | + } |
| 135 | + |
| 136 | + public String getName() { |
| 137 | + return name; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @Entity( name = "Form" ) |
| 142 | + public static class Form { |
| 143 | + @EmbeddedId |
| 144 | + private FormInfo formInfo; |
| 145 | + |
| 146 | + @OneToMany( mappedBy = "id.form" ) |
| 147 | + private List<FormVersion> versions; |
| 148 | + |
| 149 | + public Form() { |
| 150 | + } |
| 151 | + |
| 152 | + public Form(FormInfo formInfo) { |
| 153 | + this.formInfo = formInfo; |
| 154 | + } |
| 155 | + |
| 156 | + public List<FormVersion> getVersions() { |
| 157 | + return versions; |
| 158 | + } |
| 159 | + |
| 160 | + public FormInfo getFormInfo() { |
| 161 | + return formInfo; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + @Entity( name = "FormOption" ) |
| 166 | + public static class FormOption { |
| 167 | + @Id |
| 168 | + private Long id; |
| 169 | + |
| 170 | + @ManyToOne( optional = false ) |
| 171 | + @JoinColumn( name = "stepConfiguration_id", foreignKey = @ForeignKey( name = "FK_FormOption_StepConfiguration" ) ) |
| 172 | + private StepConfiguration configuration; |
| 173 | + |
| 174 | + @ManyToOne( optional = false ) |
| 175 | + @JoinColumns( foreignKey = @ForeignKey( name = "FK_FormOption_FormVersion" ), value = { |
| 176 | + @JoinColumn( name = "form_id", updatable = false ), |
| 177 | + @JoinColumn( name = "versionNumber", updatable = false ) |
| 178 | + } ) |
| 179 | + private FormVersion formVersion; |
| 180 | + |
| 181 | + public FormOption() { |
| 182 | + } |
| 183 | + |
| 184 | + public FormOption(Long id, StepConfiguration configuration, FormVersion formVersion) { |
| 185 | + this.id = id; |
| 186 | + this.configuration = configuration; |
| 187 | + this.formVersion = formVersion; |
| 188 | + } |
| 189 | + |
| 190 | + public Long getId() { |
| 191 | + return id; |
| 192 | + } |
| 193 | + |
| 194 | + public void setId(Long id) { |
| 195 | + this.id = id; |
| 196 | + } |
| 197 | + |
| 198 | + public StepConfiguration getConfiguration() { |
| 199 | + return configuration; |
| 200 | + } |
| 201 | + |
| 202 | + public void setConfiguration(StepConfiguration configuration) { |
| 203 | + this.configuration = configuration; |
| 204 | + } |
| 205 | + |
| 206 | + public FormVersion getFormVersion() { |
| 207 | + return formVersion; |
| 208 | + } |
| 209 | + |
| 210 | + public void setFormVersion(FormVersion formVersion) { |
| 211 | + this.formVersion = formVersion; |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + @Entity( name = "FormVersion" ) |
| 216 | + public static class FormVersion { |
| 217 | + @EmbeddedId |
| 218 | + private FormVersionId id; |
| 219 | + |
| 220 | + public FormVersion() { |
| 221 | + id = new FormVersionId(); |
| 222 | + } |
| 223 | + |
| 224 | + public FormVersion(Form form, int version) { |
| 225 | + this(); |
| 226 | + this.id.setForm( form ); |
| 227 | + this.id.setVersionNumber( version ); |
| 228 | + } |
| 229 | + |
| 230 | + public FormVersionId getId() { |
| 231 | + return id; |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + @Embeddable |
| 236 | + public static class FormVersionId implements Serializable { |
| 237 | + @ManyToOne |
| 238 | + @JoinColumn( name = "form_id" ) |
| 239 | + private Form form; |
| 240 | + |
| 241 | + private Integer versionNumber; |
| 242 | + |
| 243 | + public Form getForm() { |
| 244 | + return form; |
| 245 | + } |
| 246 | + |
| 247 | + public void setForm(Form form) { |
| 248 | + this.form = form; |
| 249 | + } |
| 250 | + |
| 251 | + public void setVersionNumber(Integer versionNumber) { |
| 252 | + this.versionNumber = versionNumber; |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + @Entity( name = "StepConfiguration" ) |
| 257 | + public static class StepConfiguration { |
| 258 | + @Id |
| 259 | + private Long id; |
| 260 | + |
| 261 | + @OneToMany( cascade = CascadeType.ALL, mappedBy = "configuration", fetch = FetchType.EAGER ) |
| 262 | + private Set<FormOption> formOptions = new HashSet<>(); |
| 263 | + |
| 264 | + public StepConfiguration() { |
| 265 | + } |
| 266 | + |
| 267 | + public StepConfiguration(Long id) { |
| 268 | + this.id = id; |
| 269 | + } |
| 270 | + |
| 271 | + public Set<FormOption> getFormOptions() { |
| 272 | + return formOptions; |
| 273 | + } |
| 274 | + } |
| 275 | +} |
0 commit comments