From 0531454f8820f17acde5ea441d8bab246b0dcb7e Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 10 Dec 2024 18:09:15 +0100 Subject: [PATCH] HHH-15186 Add test showing the issue has been resolved --- ...ynamicStatusLazyGroupOnBasicFieldTest.java | 177 ++++++++++++++++++ ...StatusSingleLazyGroupOnBasicFieldTest.java | 175 +++++++++++++++++ ...cStatusWithMultipleToManyRelationTest.java | 91 +++++++++ 3 files changed, 443 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dynamic/DynamicStatusLazyGroupOnBasicFieldTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dynamic/DynamicStatusSingleLazyGroupOnBasicFieldTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dynamic/DynamicStatusWithMultipleToManyRelationTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dynamic/DynamicStatusLazyGroupOnBasicFieldTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dynamic/DynamicStatusLazyGroupOnBasicFieldTest.java new file mode 100644 index 0000000000..58cf2b2035 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dynamic/DynamicStatusLazyGroupOnBasicFieldTest.java @@ -0,0 +1,177 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.bytecode.enhancement.dynamic; + +import jakarta.persistence.Basic; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.Id; +import org.hibernate.Hibernate; +import org.hibernate.annotations.LazyGroup; +import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + + +@JiraKey("HHH-15186") +@BytecodeEnhanced +@DomainModel( + annotatedClasses = { + DynamicStatusLazyGroupOnBasicFieldTest.Person.class + } +) +@SessionFactory +public class DynamicStatusLazyGroupOnBasicFieldTest { + + private static final Long PERSON_ID = 1L; + private static final String PERSON_NAME = "And"; + private static final String PERSON_SURNAME = "Bor"; + + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Person person = new Person( PERSON_ID, PERSON_SURNAME, PERSON_NAME ); + session.persist( person ); + } + ); + } + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> + scope.getSessionFactory().getSchemaManager().truncateMappedObjects() + ); + } + + @Test + public void test(SessionFactoryScope scope) { + final EntityPersister persister = scope.getSessionFactory().getMappingMetamodel() + .findEntityDescriptor( Person.class ); + assertThat( persister.getEntityMetamodel().isDynamicUpdate() ).isTrue(); + } + + @Test + public void testUpdateSurname(SessionFactoryScope scope) { + String updatedSurname = PERSON_SURNAME + "_1"; + scope.inTransaction( + session -> { + Person person = session.find( Person.class, PERSON_ID ); + assertThat( person ).isNotNull(); + assertThat( Hibernate.isPropertyInitialized( person, "surname" ) ).isFalse(); + assertThat( Hibernate.isPropertyInitialized( person, "name" ) ).isFalse(); + person.setSurname( updatedSurname ); + } + ); + + scope.inTransaction( + session -> { + Person person = session.find( Person.class, PERSON_ID ); + assertThat( person.getSurname() ).isEqualTo( updatedSurname ); + assertThat( person.getName() ).isEqualTo( PERSON_NAME ); + } + ); + } + + @Test + public void testUpdateName(SessionFactoryScope scope) { + String updatedName = PERSON_NAME + "_1"; + scope.inTransaction( + session -> { + Person person = session.find( Person.class, PERSON_ID ); + assertThat( person ).isNotNull(); + assertThat( Hibernate.isPropertyInitialized( person, "surname" ) ).isFalse(); + assertThat( Hibernate.isPropertyInitialized( person, "name" ) ).isFalse(); + person.setName( updatedName ); + } + ); + + scope.inTransaction( + session -> { + Person person = session.find( Person.class, PERSON_ID ); + assertThat( person.getSurname() ).isEqualTo( PERSON_SURNAME ); + assertThat( person.getName() ).isEqualTo( updatedName ); + } + ); + } + + @Test + public void testUpdateNameAndSurname(SessionFactoryScope scope) { + String updatedName = PERSON_NAME + "_1"; + String updatedSurname = PERSON_SURNAME + "_1"; + + scope.inTransaction( + session -> { + Person person = session.find( Person.class, PERSON_ID ); + assertThat( person ).isNotNull(); + assertThat( Hibernate.isPropertyInitialized( person, "surname" ) ).isFalse(); + assertThat( Hibernate.isPropertyInitialized( person, "name" ) ).isFalse(); + person.setName( updatedName ); + person.setSurname( updatedSurname ); + } + ); + + scope.inTransaction( + session -> { + Person person = session.find( Person.class, PERSON_ID ); + assertThat( person.getSurname() ).isEqualTo( updatedSurname ); + assertThat( person.getName() ).isEqualTo( updatedName ); + } + ); + } + + @Entity(name = "Person") + public static class Person { + + @Id + private Long id; + + @Basic(fetch = FetchType.LAZY) + @LazyGroup("group1") + private String surname; + + @Basic(fetch = FetchType.LAZY) + @LazyGroup("group2") + private String name; + + public Person() { + } + + public Person(Long id, String surname, String name) { + this.id = id; + this.surname = surname; + this.name = name; + } + + public Long getId() { + return id; + } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dynamic/DynamicStatusSingleLazyGroupOnBasicFieldTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dynamic/DynamicStatusSingleLazyGroupOnBasicFieldTest.java new file mode 100644 index 0000000000..adee5ba260 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dynamic/DynamicStatusSingleLazyGroupOnBasicFieldTest.java @@ -0,0 +1,175 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.bytecode.enhancement.dynamic; + +import jakarta.persistence.Basic; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.Id; +import org.hibernate.Hibernate; +import org.hibernate.annotations.LazyGroup; +import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + + +@JiraKey("HHH-15186") +@BytecodeEnhanced +@DomainModel( + annotatedClasses = { + DynamicStatusSingleLazyGroupOnBasicFieldTest.Person.class + } +) +@SessionFactory +public class DynamicStatusSingleLazyGroupOnBasicFieldTest { + + private static final Long PERSON_ID = 1L; + private static final String PERSON_NAME = "And"; + private static final String PERSON_SURNAME = "Bor"; + + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Person person = new Person( PERSON_ID, PERSON_SURNAME, PERSON_NAME ); + session.persist( person ); + } + ); + } + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> + scope.getSessionFactory().getSchemaManager().truncateMappedObjects() + ); + } + + @Test + public void test(SessionFactoryScope scope) { + final EntityPersister persister = scope.getSessionFactory().getMappingMetamodel() + .findEntityDescriptor( Person.class ); + assertThat( persister.getEntityMetamodel().isDynamicUpdate() ).isFalse(); + } + + @Test + public void testUpdateSurname(SessionFactoryScope scope) { + String updatedSurname = PERSON_SURNAME + "_1"; + scope.inTransaction( + session -> { + Person person = session.find( Person.class, PERSON_ID ); + assertThat( person ).isNotNull(); + assertThat( Hibernate.isPropertyInitialized( person, "surname" ) ).isFalse(); + assertThat( Hibernate.isPropertyInitialized( person, "name" ) ).isTrue(); + person.setSurname( updatedSurname ); + } + ); + + scope.inTransaction( + session -> { + Person person = session.find( Person.class, PERSON_ID ); + assertThat( person.getSurname() ).isEqualTo( updatedSurname ); + assertThat( person.getName() ).isEqualTo( PERSON_NAME ); + } + ); + } + + @Test + public void testUpdateName(SessionFactoryScope scope) { + String updatedName = PERSON_NAME + "_1"; + scope.inTransaction( + session -> { + Person person = session.find( Person.class, PERSON_ID ); + assertThat( person ).isNotNull(); + assertThat( Hibernate.isPropertyInitialized( person, "surname" ) ).isFalse(); + assertThat( Hibernate.isPropertyInitialized( person, "name" ) ).isTrue(); + person.setName( updatedName ); + } + ); + + scope.inTransaction( + session -> { + Person person = session.find( Person.class, PERSON_ID ); + assertThat( person.getSurname() ).isEqualTo( PERSON_SURNAME ); + assertThat( person.getName() ).isEqualTo( updatedName ); + } + ); + } + + @Test + public void testUpdateNameAndSurname(SessionFactoryScope scope) { + String updatedName = PERSON_NAME + "_1"; + String updatedSurname = PERSON_SURNAME + "_1"; + + scope.inTransaction( + session -> { + Person person = session.find( Person.class, PERSON_ID ); + assertThat( person ).isNotNull(); + assertThat( Hibernate.isPropertyInitialized( person, "surname" ) ).isFalse(); + assertThat( Hibernate.isPropertyInitialized( person, "name" ) ).isTrue(); + person.setName( updatedName ); + person.setSurname( updatedSurname ); + } + ); + + scope.inTransaction( + session -> { + Person person = session.find( Person.class, PERSON_ID ); + assertThat( person.getSurname() ).isEqualTo( updatedSurname ); + assertThat( person.getName() ).isEqualTo( updatedName ); + } + ); + } + + @Entity(name = "Person") + public static class Person { + + @Id + private Long id; + + @Basic(fetch = FetchType.LAZY) + @LazyGroup("group1") + private String surname; + + private String name; + + public Person() { + } + + public Person(Long id, String surname, String name) { + this.id = id; + this.surname = surname; + this.name = name; + } + + public Long getId() { + return id; + } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dynamic/DynamicStatusWithMultipleToManyRelationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dynamic/DynamicStatusWithMultipleToManyRelationTest.java new file mode 100644 index 0000000000..4e4861cca0 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dynamic/DynamicStatusWithMultipleToManyRelationTest.java @@ -0,0 +1,91 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.bytecode.enhancement.dynamic; + +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Version; +import org.hibernate.annotations.LazyGroup; +import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import java.util.HashSet; +import java.util.Set; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + + +@JiraKey("HHH-15186") +@BytecodeEnhanced +@DomainModel( + annotatedClasses = { + DynamicStatusWithMultipleToManyRelationTest.FooEntity.class, + DynamicStatusWithMultipleToManyRelationTest.BarEntity.class, + DynamicStatusWithMultipleToManyRelationTest.BazEntity.class + } +) +@SessionFactory +public class DynamicStatusWithMultipleToManyRelationTest { + + @Test + public void test(SessionFactoryScope scope) { + final EntityPersister persister = scope.getSessionFactory().getMappingMetamodel() + .findEntityDescriptor( FooEntity.class ); + assertThat( persister.getEntityMetamodel().isDynamicUpdate() ).isFalse(); + } + + @Entity(name = "FooEntity") + public static class FooEntity { + + @Id + private long id; + @Version + private int version; + + private String name; + + @LazyGroup("bars") + @OneToMany(mappedBy = "foo") + public Set bars = new HashSet<>(); + + @LazyGroup("bazzes") + @OneToMany(mappedBy = "foo") + public Set bazzes = new HashSet<>(); + + } + + @Entity(name = "BazEntity") + public static class BazEntity { + + @Id + @GeneratedValue + private long id; + + @ManyToOne(fetch = FetchType.LAZY, optional = false) + private FooEntity foo; + + } + + @Entity(name = "BarEntity") + public static class BarEntity { + + @Id + @GeneratedValue + private long id; + + @ManyToOne(fetch = FetchType.LAZY, optional = false) + private FooEntity foo; + + } +}