1616import jakarta .persistence .GeneratedValue ;
1717import jakarta .persistence .IdClass ;
1818import jakarta .persistence .Inheritance ;
19+ import jakarta .persistence .InheritanceType ;
1920import jakarta .persistence .JoinColumn ;
2021import jakarta .persistence .JoinTable ;
2122import jakarta .persistence .MappedSuperclass ;
9394import static org .hibernate .boot .model .internal .BinderHelper .extractFromPackage ;
9495import static org .hibernate .boot .model .internal .BinderHelper .getMappedSuperclassOrNull ;
9596import static org .hibernate .boot .model .internal .BinderHelper .getPath ;
97+ import static org .hibernate .boot .model .internal .BinderHelper .handleForeignKeyConstraint ;
9698import static org .hibernate .boot .model .internal .BinderHelper .hasToOneAnnotation ;
97- import static org .hibernate .boot .model .internal .BinderHelper .noConstraint ;
9899import static org .hibernate .boot .model .internal .BinderHelper .toAliasEntityMap ;
99100import static org .hibernate .boot .model .internal .BinderHelper .toAliasTableMap ;
100101import static org .hibernate .boot .model .internal .DialectOverridesAnnotationHelper .getOverridableAnnotation ;
@@ -505,8 +506,7 @@ private boolean mapAsIdClass(
505506 classWithIdClass ,
506507 compositeType ,
507508 baseInferredData ,
508- propertyAccessor ,
509- true
509+ propertyAccessor
510510 );
511511 if ( idClassComponent .isSimpleRecord () ) {
512512 mapper .setSimpleRecord ( true );
@@ -552,8 +552,7 @@ private Component createMapperProperty(
552552 ClassDetails classWithIdClass ,
553553 TypeDetails compositeClass ,
554554 PropertyData baseInferredData ,
555- AccessType propertyAccessor ,
556- boolean isIdClass ) {
555+ AccessType propertyAccessor ) {
557556 final var mapper = createMapper (
558557 inheritanceStates ,
559558 persistentClass ,
@@ -562,8 +561,7 @@ private Component createMapperProperty(
562561 classWithIdClass ,
563562 compositeClass ,
564563 baseInferredData ,
565- propertyAccessor ,
566- isIdClass
564+ propertyAccessor
567565 );
568566 final var mapperProperty = new SyntheticProperty ();
569567 mapperProperty .setName ( NavigablePath .IDENTIFIER_MAPPER_PROPERTY );
@@ -583,8 +581,7 @@ private Component createMapper(
583581 ClassDetails classWithIdClass ,
584582 TypeDetails compositeClass ,
585583 PropertyData baseInferredData ,
586- AccessType propertyAccessor ,
587- boolean isIdClass ) {
584+ AccessType propertyAccessor ) {
588585 final var mapper = fillEmbeddable (
589586 propertyHolder ,
590587 new PropertyPreloadedData (
@@ -605,7 +602,7 @@ private Component createMapper(
605602 null ,
606603 context ,
607604 inheritanceStates ,
608- isIdClass
605+ true
609606 );
610607 persistentClass .setIdentifierMapper ( mapper );
611608
@@ -824,38 +821,39 @@ private void handleInheritance(
824821 InheritanceState inheritanceState ,
825822 PersistentClass superEntity ,
826823 PropertyHolder propertyHolder ) {
827- final boolean isJoinedSubclass ;
828824 switch ( inheritanceState .getType () ) {
829825 case JOINED :
830826 joinedInheritance ( inheritanceState , superEntity , propertyHolder );
831- isJoinedSubclass = inheritanceState .hasParents ();
832827 break ;
833828 case SINGLE_TABLE :
834829 singleTableInheritance ( inheritanceState , propertyHolder );
835- isJoinedSubclass = false ;
836830 break ;
837831 case TABLE_PER_CLASS :
838- isJoinedSubclass = false ;
839832 break ;
840833 default :
841834 throw new AssertionFailure ( "Unrecognized InheritanceType" );
842835 }
843836
844837 bindDiscriminatorValue ();
845838
846- if ( !isJoinedSubclass ) {
839+ if ( !isJoinedSubclass ( inheritanceState ) ) {
847840 checkNoJoinColumns ( annotatedClass );
848841 checkNoOnDelete ( annotatedClass );
849842 }
850843 }
851844
845+ private static boolean isJoinedSubclass (InheritanceState inheritanceState ) {
846+ return inheritanceState .getType () == InheritanceType .JOINED
847+ && inheritanceState .hasParents ();
848+ }
849+
852850 private void singleTableInheritance (InheritanceState inheritanceState , PropertyHolder holder ) {
853851 final var discriminatorColumn = processSingleTableDiscriminatorProperties ( inheritanceState );
854852 // todo : sucks that this is separate from RootClass distinction
855853 if ( !inheritanceState .hasParents () ) {
856- final var rootClass = (RootClass ) persistentClass ;
857854 if ( inheritanceState .hasSiblings ()
858855 || discriminatorColumn != null && !discriminatorColumn .isImplicit () ) {
856+ final var rootClass = (RootClass ) persistentClass ;
859857 bindDiscriminatorColumnToRootPersistentClass ( rootClass , discriminatorColumn , holder );
860858 if ( context .getBuildingOptions ().shouldImplicitlyForceDiscriminatorInSelect () ) {
861859 rootClass .setForceDiscriminator ( true );
@@ -870,7 +868,7 @@ private void joinedInheritance(InheritanceState state, PersistentClass superEnti
870868 final var joinedSubclass = (JoinedSubclass ) persistentClass ;
871869 final var key = new DependantValue ( context , joinedSubclass .getTable (), joinedSubclass .getIdentifier () );
872870 joinedSubclass .setKey ( key );
873- handleForeignKeys ( annotatedClass , context , key );
871+ handleForeignKey ( annotatedClass , context , key );
874872 final var onDelete = annotatedClass .getAnnotationUsage ( OnDelete .class , modelsContext () );
875873 key .setOnDeleteAction ( onDelete == null ? null : onDelete .action () );
876874 //we are never in a second pass at that stage, so queue it
@@ -880,14 +878,15 @@ private void joinedInheritance(InheritanceState state, PersistentClass superEnti
880878 }
881879
882880 final var discriminatorColumn = processJoinedDiscriminatorProperties ( state );
883- if ( ! state . hasParents () ) { // todo : sucks that this is separate from RootClass distinction
884- final var rootClass = ( RootClass ) persistentClass ;
881+ // todo : sucks that this is separate from RootClass distinction
882+ if ( ! state . hasParents () ) {
885883 // the class we're processing is the root of the hierarchy, so
886884 // let's see if we had a discriminator column (it's perfectly
887885 // valid for joined inheritance to not have a discriminator)
888886 if ( discriminatorColumn != null ) {
889887 // we do have a discriminator column
890888 if ( state .hasSiblings () || !discriminatorColumn .isImplicit () ) {
889+ final var rootClass = (RootClass ) persistentClass ;
891890 bindDiscriminatorColumnToRootPersistentClass ( rootClass , discriminatorColumn , holder );
892891 if ( context .getBuildingOptions ().shouldImplicitlyForceDiscriminatorInSelect () ) {
893892 rootClass .setForceDiscriminator ( true );
@@ -912,39 +911,22 @@ private void checkNoOnDelete(ClassDetails annotatedClass) {
912911 }
913912 }
914913
915- private void handleForeignKeys (ClassDetails clazzToProcess , MetadataBuildingContext context , DependantValue key ) {
914+ private static void handleForeignKey (ClassDetails clazzToProcess , MetadataBuildingContext context , DependantValue key ) {
915+ final var foreignKey = clazzToProcess .getDirectAnnotationUsage ( ForeignKey .class );
916+ handleForeignKeyConstraint ( key , foreignKey , nestedForeignKey ( clazzToProcess ), context );
917+ }
918+
919+ private static ForeignKey nestedForeignKey (ClassDetails clazzToProcess ) {
916920 final var pkJoinColumn = clazzToProcess .getDirectAnnotationUsage ( PrimaryKeyJoinColumn .class );
917921 final var pkJoinColumns = clazzToProcess .getDirectAnnotationUsage ( PrimaryKeyJoinColumns .class );
918- final boolean noConstraintByDefault = context .getBuildingOptions ().isNoConstraintByDefault ();
919- if ( pkJoinColumn != null && noConstraint ( pkJoinColumn .foreignKey (), noConstraintByDefault )
920- || pkJoinColumns != null && noConstraint ( pkJoinColumns .foreignKey (), noConstraintByDefault ) ) {
921- key .disableForeignKey ();
922+ if ( pkJoinColumn != null ) {
923+ return pkJoinColumn .foreignKey ();
924+ }
925+ else if ( pkJoinColumns != null ) {
926+ return pkJoinColumns .foreignKey ();
922927 }
923928 else {
924- final var foreignKey = clazzToProcess .getDirectAnnotationUsage ( ForeignKey .class );
925- if ( noConstraint ( foreignKey , noConstraintByDefault ) ) {
926- key .disableForeignKey ();
927- }
928- else if ( foreignKey != null ) {
929- key .setForeignKeyName ( nullIfEmpty ( foreignKey .name () ) );
930- key .setForeignKeyDefinition ( nullIfEmpty ( foreignKey .foreignKeyDefinition () ) );
931- key .setForeignKeyOptions ( foreignKey .options () );
932- }
933- else if ( noConstraintByDefault ) {
934- key .disableForeignKey ();
935- }
936- else if ( pkJoinColumns != null ) {
937- final var nestedFk = pkJoinColumns .foreignKey ();
938- key .setForeignKeyName ( nullIfEmpty ( nestedFk .name () ) );
939- key .setForeignKeyDefinition ( nullIfEmpty ( nestedFk .foreignKeyDefinition () ) );
940- key .setForeignKeyOptions ( nestedFk .options () );
941- }
942- else if ( pkJoinColumn != null ) {
943- final var nestedFk = pkJoinColumn .foreignKey ();
944- key .setForeignKeyName ( nullIfEmpty ( nestedFk .name () ) );
945- key .setForeignKeyDefinition ( nullIfEmpty ( nestedFk .foreignKeyDefinition () ) );
946- key .setForeignKeyOptions ( nestedFk .options () );
947- }
929+ return null ;
948930 }
949931 }
950932
0 commit comments