HHH-15186 Add test showing the issue has been resolved

This commit is contained in:
Andrea Boriero 2024-12-10 18:09:15 +01:00 committed by Andrea Boriero
parent e38ae96acf
commit 0531454f88
3 changed files with 443 additions and 0 deletions

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}

View File

@ -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<BarEntity> bars = new HashSet<>();
@LazyGroup("bazzes")
@OneToMany(mappedBy = "foo")
public Set<BazEntity> 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;
}
}