From c87fa007a31914e4e8d4753a5d3e47f65cdf77fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=8Cedomir=20Igaly?= Date: Tue, 1 Apr 2025 13:29:25 +0200 Subject: [PATCH] HHH-19312 Test for 'Index out of bounds' in org.hibernate.bytecode.internal.bytebuddy.BytecodeProviderImpl.encodeName(java.util.List, java.util.List, java.util.List) --- .../BytecodeProviderIndexOutOfBoundsTest.java | 47 +++++++++++++++++++ .../index_out_of_bounds/base/BaseEntity.java | 47 +++++++++++++++++++ .../base/EmbeddableType.java | 23 +++++++++ .../derived/TestEntity.java | 16 +++++++ 4 files changed, 133 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/index_out_of_bounds/BytecodeProviderIndexOutOfBoundsTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/index_out_of_bounds/base/BaseEntity.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/index_out_of_bounds/base/EmbeddableType.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/index_out_of_bounds/derived/TestEntity.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/index_out_of_bounds/BytecodeProviderIndexOutOfBoundsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/index_out_of_bounds/BytecodeProviderIndexOutOfBoundsTest.java new file mode 100644 index 000000000000..f7b4cce26894 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/index_out_of_bounds/BytecodeProviderIndexOutOfBoundsTest.java @@ -0,0 +1,47 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.index_out_of_bounds; + +import org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.index_out_of_bounds.base.EmbeddableType; +import org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.index_out_of_bounds.derived.TestEntity; +import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; +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 static org.assertj.core.api.Assertions.assertThat; + +@DomainModel( + annotatedClasses = { + TestEntity.class + } +) +@SessionFactory +@BytecodeEnhanced +@EnhancementOptions(lazyLoading = true, inlineDirtyChecking = true) +@JiraKey( "HHH-19312" ) +public class BytecodeProviderIndexOutOfBoundsTest { + + @Test + public void testIt(SessionFactoryScope scope) { + // Just a smoke test; the original failure happened during bytecode enhancement. + Long id = scope.fromTransaction( s -> { + TestEntity testEntity = new TestEntity(); + EmbeddableType embedded = new EmbeddableType(); + embedded.setField( "someValue" ); + testEntity.setEmbeddedField( embedded ); + s.persist( testEntity ); + return testEntity.getId(); + } ); + scope.inTransaction( s -> { + TestEntity testEntity = s.find( TestEntity.class, id ); + assertThat( testEntity.getEmbeddedField().getField() ).isEqualTo( "someValue" ); + } ); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/index_out_of_bounds/base/BaseEntity.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/index_out_of_bounds/base/BaseEntity.java new file mode 100644 index 000000000000..be2788cb3706 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/index_out_of_bounds/base/BaseEntity.java @@ -0,0 +1,47 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.index_out_of_bounds.base; + +import jakarta.persistence.Embedded; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.MappedSuperclass; + +@MappedSuperclass +public abstract class BaseEntity { + + private Long id; + + protected EmbeddableType embeddedField; + + private Long dummyField; + + @Id + @GeneratedValue + public Long getId() { + return id; + } + + public void setId(final Long id) { + this.id = id; + } + + @Embedded + public EmbeddableType getEmbeddedField() { + return embeddedField; + } + + public void setEmbeddedField(final EmbeddableType embeddedField) { + this.embeddedField = embeddedField; + } + + public Long getDummyField() { + return dummyField; + } + + void setDummyField(Long dummyField) { + this.dummyField = dummyField; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/index_out_of_bounds/base/EmbeddableType.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/index_out_of_bounds/base/EmbeddableType.java new file mode 100644 index 000000000000..86738592bbad --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/index_out_of_bounds/base/EmbeddableType.java @@ -0,0 +1,23 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.index_out_of_bounds.base; + +import jakarta.persistence.Column; +import jakarta.persistence.Embeddable; + +@Embeddable +public class EmbeddableType { + + @Column + private String field; + + public String getField() { + return field; + } + + public void setField(final String field) { + this.field = field; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/index_out_of_bounds/derived/TestEntity.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/index_out_of_bounds/derived/TestEntity.java new file mode 100644 index 000000000000..222db372cd12 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/index_out_of_bounds/derived/TestEntity.java @@ -0,0 +1,16 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.index_out_of_bounds.derived; + +import jakarta.persistence.Access; +import jakarta.persistence.AccessType; +import jakarta.persistence.Entity; +import org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.index_out_of_bounds.base.BaseEntity; + +@Entity +@Access(AccessType.PROPERTY) +public class TestEntity extends BaseEntity { + +}