13
13
import java .util .HashMap ;
14
14
import java .util .LinkedHashMap ;
15
15
import java .util .List ;
16
+ import java .util .Locale ;
16
17
import java .util .Map ;
17
18
18
19
import org .hibernate .AnnotationException ;
20
+ import org .hibernate .MappingException ;
19
21
import org .hibernate .annotations .DiscriminatorFormula ;
20
22
import org .hibernate .annotations .Instantiator ;
21
23
import org .hibernate .annotations .TypeBinderType ;
@@ -850,6 +852,7 @@ static Component createEmbeddable(
850
852
else {
851
853
embeddableClass = inferredData .getClassOrPluralElement ();
852
854
component .setComponentClassName ( embeddableClass .getName () );
855
+ checkEmbeddableRecursiveHierarchy ( embeddableClass , inferredData , propertyHolder );
853
856
}
854
857
component .setCustomInstantiator ( customInstantiatorImpl );
855
858
final Constructor <?> constructor = resolveInstantiator ( embeddableClass , context );
@@ -863,6 +866,29 @@ static Component createEmbeddable(
863
866
return component ;
864
867
}
865
868
869
+ private static void checkEmbeddableRecursiveHierarchy (
870
+ XClass embeddableClass ,
871
+ PropertyData propertyData ,
872
+ PropertyHolder propertyHolder ) {
873
+ while ( propertyHolder .isComponent () ) {
874
+ final ComponentPropertyHolder componentHolder = (ComponentPropertyHolder ) propertyHolder ;
875
+ // we need to check that the embeddable is not used in a recursive hierarchy
876
+ XClass classDetails = embeddableClass ;
877
+ while ( classDetails != null ) {
878
+ if ( propertyHolder .getClassName ().equals ( classDetails .getName () ) ) {
879
+ throw new MappingException ( String .format (
880
+ Locale .ROOT ,
881
+ "Recursive embeddable mapping detected for property '%s' for type [%s]" ,
882
+ getPath ( propertyHolder , propertyData ),
883
+ propertyHolder .getClassName ()
884
+ ) );
885
+ }
886
+ classDetails = classDetails .getSuperclass ();
887
+ }
888
+ propertyHolder = componentHolder .parent ;
889
+ }
890
+ }
891
+
866
892
private static Constructor <?> resolveInstantiator (XClass embeddableClass , MetadataBuildingContext buildingContext ) {
867
893
if ( embeddableClass != null ) {
868
894
final Constructor <?>[] declaredConstructors = buildingContext .getBootstrapContext ().getReflectionManager ()
0 commit comments