|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.bytecode.enhancement.dynamic; |
| 6 | + |
| 7 | +import jakarta.persistence.Basic; |
| 8 | +import jakarta.persistence.Entity; |
| 9 | +import jakarta.persistence.FetchType; |
| 10 | +import jakarta.persistence.Id; |
| 11 | +import org.hibernate.Hibernate; |
| 12 | +import org.hibernate.annotations.LazyGroup; |
| 13 | +import org.hibernate.persister.entity.EntityPersister; |
| 14 | +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; |
| 15 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 16 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 18 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 19 | +import org.junit.jupiter.api.AfterEach; |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 24 | + |
| 25 | + |
| 26 | +@JiraKey("HHH-15186") |
| 27 | +@BytecodeEnhanced |
| 28 | +@DomainModel( |
| 29 | + annotatedClasses = { |
| 30 | + DynamicStatusLazyGroupOnBasicFieldTest.Person.class |
| 31 | + } |
| 32 | +) |
| 33 | +@SessionFactory |
| 34 | +public class DynamicStatusLazyGroupOnBasicFieldTest { |
| 35 | + |
| 36 | + private static final Long PERSON_ID = 1L; |
| 37 | + private static final String PERSON_NAME = "And"; |
| 38 | + private static final String PERSON_SURNAME = "Bor"; |
| 39 | + |
| 40 | + @BeforeEach |
| 41 | + public void setUp(SessionFactoryScope scope) { |
| 42 | + scope.inTransaction( |
| 43 | + session -> { |
| 44 | + Person person = new Person( PERSON_ID, PERSON_SURNAME, PERSON_NAME ); |
| 45 | + session.persist( person ); |
| 46 | + } |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + @AfterEach |
| 51 | + public void tearDown(SessionFactoryScope scope) { |
| 52 | + scope.inTransaction( |
| 53 | + session -> |
| 54 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects() |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void test(SessionFactoryScope scope) { |
| 60 | + final EntityPersister persister = scope.getSessionFactory().getMappingMetamodel() |
| 61 | + .findEntityDescriptor( Person.class ); |
| 62 | + assertThat( persister.getEntityMetamodel().isDynamicUpdate() ).isTrue(); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testUpdateSurname(SessionFactoryScope scope) { |
| 67 | + String updatedSurname = PERSON_SURNAME + "_1"; |
| 68 | + scope.inTransaction( |
| 69 | + session -> { |
| 70 | + Person person = session.find( Person.class, PERSON_ID ); |
| 71 | + assertThat( person ).isNotNull(); |
| 72 | + assertThat( Hibernate.isPropertyInitialized( person, "surname" ) ).isFalse(); |
| 73 | + assertThat( Hibernate.isPropertyInitialized( person, "name" ) ).isFalse(); |
| 74 | + person.setSurname( updatedSurname ); |
| 75 | + } |
| 76 | + ); |
| 77 | + |
| 78 | + scope.inTransaction( |
| 79 | + session -> { |
| 80 | + Person person = session.find( Person.class, PERSON_ID ); |
| 81 | + assertThat( person.getSurname() ).isEqualTo( updatedSurname ); |
| 82 | + assertThat( person.getName() ).isEqualTo( PERSON_NAME ); |
| 83 | + } |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testUpdateName(SessionFactoryScope scope) { |
| 89 | + String updatedName = PERSON_NAME + "_1"; |
| 90 | + scope.inTransaction( |
| 91 | + session -> { |
| 92 | + Person person = session.find( Person.class, PERSON_ID ); |
| 93 | + assertThat( person ).isNotNull(); |
| 94 | + assertThat( Hibernate.isPropertyInitialized( person, "surname" ) ).isFalse(); |
| 95 | + assertThat( Hibernate.isPropertyInitialized( person, "name" ) ).isFalse(); |
| 96 | + person.setName( updatedName ); |
| 97 | + } |
| 98 | + ); |
| 99 | + |
| 100 | + scope.inTransaction( |
| 101 | + session -> { |
| 102 | + Person person = session.find( Person.class, PERSON_ID ); |
| 103 | + assertThat( person.getSurname() ).isEqualTo( PERSON_SURNAME ); |
| 104 | + assertThat( person.getName() ).isEqualTo( updatedName ); |
| 105 | + } |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testUpdateNameAndSurname(SessionFactoryScope scope) { |
| 111 | + String updatedName = PERSON_NAME + "_1"; |
| 112 | + String updatedSurname = PERSON_SURNAME + "_1"; |
| 113 | + |
| 114 | + scope.inTransaction( |
| 115 | + session -> { |
| 116 | + Person person = session.find( Person.class, PERSON_ID ); |
| 117 | + assertThat( person ).isNotNull(); |
| 118 | + assertThat( Hibernate.isPropertyInitialized( person, "surname" ) ).isFalse(); |
| 119 | + assertThat( Hibernate.isPropertyInitialized( person, "name" ) ).isFalse(); |
| 120 | + person.setName( updatedName ); |
| 121 | + person.setSurname( updatedSurname ); |
| 122 | + } |
| 123 | + ); |
| 124 | + |
| 125 | + scope.inTransaction( |
| 126 | + session -> { |
| 127 | + Person person = session.find( Person.class, PERSON_ID ); |
| 128 | + assertThat( person.getSurname() ).isEqualTo( updatedSurname ); |
| 129 | + assertThat( person.getName() ).isEqualTo( updatedName ); |
| 130 | + } |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + @Entity(name = "Person") |
| 135 | + public static class Person { |
| 136 | + |
| 137 | + @Id |
| 138 | + private Long id; |
| 139 | + |
| 140 | + @Basic(fetch = FetchType.LAZY) |
| 141 | + @LazyGroup("group1") |
| 142 | + private String surname; |
| 143 | + |
| 144 | + @Basic(fetch = FetchType.LAZY) |
| 145 | + @LazyGroup("group2") |
| 146 | + private String name; |
| 147 | + |
| 148 | + public Person() { |
| 149 | + } |
| 150 | + |
| 151 | + public Person(Long id, String surname, String name) { |
| 152 | + this.id = id; |
| 153 | + this.surname = surname; |
| 154 | + this.name = name; |
| 155 | + } |
| 156 | + |
| 157 | + public Long getId() { |
| 158 | + return id; |
| 159 | + } |
| 160 | + |
| 161 | + public String getSurname() { |
| 162 | + return surname; |
| 163 | + } |
| 164 | + |
| 165 | + public void setSurname(String surname) { |
| 166 | + this.surname = surname; |
| 167 | + } |
| 168 | + |
| 169 | + public String getName() { |
| 170 | + return name; |
| 171 | + } |
| 172 | + |
| 173 | + public void setName(String name) { |
| 174 | + this.name = name; |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments