Skip to content

HHH-19648 Recursive @Embeddable mapping leads to stack overflow #10748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,9 @@ static Component createEmbeddable(
component.setComponentClassName( component.getOwner().getClassName() );
}
else {
component.setComponentClassName( inferredData.getClassOrElementType().getName() );
final TypeDetails type = inferredData.getClassOrElementType();
component.setComponentClassName( type.getName() );
checkEmbeddableRecursiveHierarchy( type, inferredData, propertyHolder );
}
component.setCustomInstantiator( customInstantiatorImpl );
final Constructor<?> constructor = resolveInstantiator( inferredData.getClassOrElementType() );
Expand All @@ -1003,6 +1005,30 @@ static Component createEmbeddable(
return component;
}

private static void checkEmbeddableRecursiveHierarchy(
TypeDetails type,
PropertyData propertyData,
PropertyHolder propertyHolder) {
final ClassDetails embeddableClass = type.determineRawClass();
while ( propertyHolder.isComponent() ) {
final ComponentPropertyHolder componentHolder = (ComponentPropertyHolder) propertyHolder;
// we need to check that the embeddable is not used in a recursive hierarchy
ClassDetails classDetails = embeddableClass;
while ( classDetails != null ) {
if ( propertyHolder.getClassName().equals( classDetails.getClassName() ) ) {
throw new MappingException( String.format(
Locale.ROOT,
"Recursive embeddable mapping detected for property '%s' for type [%s]",
getPath( propertyHolder, propertyData ),
propertyHolder.getClassName()
) );
}
classDetails = classDetails.getSuperClass();
}
propertyHolder = componentHolder.parent;
}
}

private static void applyColumnNamingPattern(Component component, PropertyData inferredData) {
final Class<?> componentClass = component.getComponentClass();
if ( componentClass == null || Map.class.equals( componentClass ) ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.inheritance.embeddable;

import org.hibernate.MappingException;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

import org.hibernate.testing.orm.junit.Jira;
import org.hibernate.testing.util.ServiceRegistryUtil;
import org.junit.jupiter.api.Test;

import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

@Jira("https://hibernate.atlassian.net/browse/HHH-19648")
public class EmbeddableInheritanceRecursiveTest {
@Test
public void testSimpleRecursiveEmbedded() {
final StandardServiceRegistryBuilder registryBuilder = ServiceRegistryUtil.serviceRegistryBuilder();
final MetadataSources metadataSources = new MetadataSources( registryBuilder.build() )
.addAnnotatedClass( Root1.class )
.addAnnotatedClass( Entity1.class );
try {
final Metadata metadata = metadataSources.buildMetadata();
fail( "Expected MappingException due to recursive embeddable mapping" );
}
catch (Exception e) {
assertThat( e ).isInstanceOf( MappingException.class );
assertThat( e.getMessage() )
.contains( "Recursive embeddable mapping detected" )
.contains( Root1.class.getName() );
}
}

@Test
public void testChildWithRootProp() {
final StandardServiceRegistryBuilder registryBuilder = ServiceRegistryUtil.serviceRegistryBuilder();
final MetadataSources metadataSources = new MetadataSources( registryBuilder.build() )
.addAnnotatedClass( Root2.class )
.addAnnotatedClass( Child2.class )
.addAnnotatedClass( Entity2.class );
try {
final Metadata metadata = metadataSources.buildMetadata();
fail( "Expected MappingException due to recursive embeddable mapping" );
}
catch (Exception e) {
assertThat( e ).isInstanceOf( MappingException.class );
assertThat( e.getMessage() )
.contains( "Recursive embeddable mapping detected" )
.contains( Root2.class.getName() );
}
}

@Test
public void testRootWithChildProp() {
final StandardServiceRegistryBuilder registryBuilder = ServiceRegistryUtil.serviceRegistryBuilder();
final MetadataSources metadataSources = new MetadataSources( registryBuilder.build() )
.addAnnotatedClass( Root3.class )
.addAnnotatedClass( Child3.class )
.addAnnotatedClass( Entity3.class );
try {
final Metadata metadata = metadataSources.buildMetadata();
fail( "Expected MappingException due to recursive embeddable mapping" );
}
catch (Exception e) {
assertThat( e ).isInstanceOf( MappingException.class );
assertThat( e.getMessage() )
.contains( "Recursive embeddable mapping detected" )
.contains( Root3.class.getName() );
}
}

@Test
public void testMidEmbedded() {
final StandardServiceRegistryBuilder registryBuilder = ServiceRegistryUtil.serviceRegistryBuilder();
final MetadataSources metadataSources = new MetadataSources( registryBuilder.build() )
.addAnnotatedClass( Root4.class )
.addAnnotatedClass( Mid4.class )
.addAnnotatedClass( Child4.class )
.addAnnotatedClass( Entity4.class );
try {
final Metadata metadata = metadataSources.buildMetadata();
fail( "Expected MappingException due to recursive embeddable mapping" );
}
catch (Exception e) {
assertThat( e ).isInstanceOf( MappingException.class );
assertThat( e.getMessage() )
.contains( "Recursive embeddable mapping detected" )
.contains( Root4.class.getName() );
}
}

@Test
public void testUnrelatedRecursive() {
final StandardServiceRegistryBuilder registryBuilder = ServiceRegistryUtil.serviceRegistryBuilder();
final MetadataSources metadataSources = new MetadataSources( registryBuilder.build() )
.addAnnotatedClass( EmbA.class )
.addAnnotatedClass( EmbB.class )
.addAnnotatedClass( Entity5.class );
try {
final Metadata metadata = metadataSources.buildMetadata();
fail( "Expected MappingException due to recursive embeddable mapping" );
}
catch (Exception e) {
assertThat( e ).isInstanceOf( MappingException.class );
assertThat( e.getMessage() )
.contains( "Recursive embeddable mapping detected" )
.contains( EmbA.class.getName() );
}
}

@Embeddable
static class Root1 {
String root1Prop;

@Embedded
Root1 nested1;
}

@Entity(name = "Entity1")
static class Entity1 {
@Id
private Long id;

@Embedded
private Root1 root1;
}

@Embeddable
static class Root2 {
String root2Prop;
}

@Embeddable
static class Child2 extends Root2 {
String child2Prop;

@Embedded
Root2 nested2;
}

@Entity(name = "Entity2")
static class Entity2 {
@Id
private Long id;

@Embedded
private Root2 root2;
}

@Embeddable
static class Root3 {
String root3Prop;

@Embedded
Child3 nested3;
}

@Embeddable
static class Child3 extends Root3 {
String child3Prop;
}

@Entity(name = "Entity3")
static class Entity3 {
@Id
private Long id;

@Embedded
private Root3 root3;
}

@Embeddable
static class Root4 {
String root4Prop;
}

@Embeddable
static class Mid4 extends Root4 {
}

@Embeddable
static class Child4 extends Mid4 {
String child4Prop;

@Embedded
Mid4 nested4;
}

@Entity(name = "Entity4")
static class Entity4 {
@Id
private Long id;

@Embedded
private Root4 root4;
}

@Embeddable
static class EmbA {
String emb1;

@Embedded
EmbB embB;
}

@Embeddable
static class EmbB {
String embB;

@Embedded
EmbA embA;
}

@Entity(name = "Entity5")
static class Entity5 {
@Id
private Long id;

@Embedded
private EmbA embA;
}
}
Loading