@@ -586,57 +586,14 @@ public static Property findPropertyByName(PersistentClass associatedClass, Strin
586586 final Property idProperty = associatedClass .getIdentifierProperty ();
587587 final String idName = idProperty == null ? null : idProperty .getName ();
588588 try {
589- if ( isEmpty ( propertyName ) || propertyName .equals ( idName ) ) {
590- //default to id
591- return idProperty ;
592- }
593- else {
594- Property property = null ;
595- if ( propertyName .indexOf ( idName + "." ) == 0 ) {
596- property = idProperty ;
597- propertyName = propertyName .substring ( idName .length () + 1 );
598- }
599- final var tokens = new StringTokenizer ( propertyName , "." , false );
600- while ( tokens .hasMoreTokens () ) {
601- final String element = tokens .nextToken ();
602- if ( property == null ) {
603- property = associatedClass .getProperty ( element );
604- }
605- else if ( property .isComposite () ) {
606- final var value = (Component ) property .getValue ();
607- property = value .getProperty ( element );
608- }
609- else {
610- return null ;
611- }
612- }
613- return property ;
614- }
589+ return isEmpty ( propertyName ) || propertyName .equals ( idName )
590+ ? idProperty // Default to id
591+ : findProperty ( associatedClass , propertyName , idProperty , idName );
615592 }
616593 catch ( MappingException e ) {
617594 try {
618595 // if we do not find it, try to check the identifier mapper
619- if ( associatedClass .getIdentifierMapper () == null ) {
620- return null ;
621- }
622- else {
623- Property property = null ;
624- final var tokens = new StringTokenizer ( propertyName , "." , false );
625- while ( tokens .hasMoreTokens () ) {
626- final String element = tokens .nextToken ();
627- if ( property == null ) {
628- property = associatedClass .getIdentifierMapper ().getProperty ( element );
629- }
630- else if ( property .isComposite () ) {
631- final var value = (Component ) property .getValue ();
632- property = value .getProperty ( element );
633- }
634- else {
635- return null ;
636- }
637- }
638- return property ;
639- }
596+ return findPropertyUsingIdMapper ( associatedClass , propertyName );
640597 }
641598 catch ( MappingException ee ) {
642599 return null ;
@@ -649,60 +606,61 @@ else if ( property.isComposite() ) {
649606 */
650607 public static Property findPropertyByName (Component component , String propertyName ) {
651608 try {
652- if ( isEmpty ( propertyName ) ) {
653- // Do not expect to use a primary key for this case
654- return null ;
655- }
656- else {
657- Property property = null ;
658- final var tokens = new StringTokenizer ( propertyName , "." , false );
659- while ( tokens .hasMoreTokens () ) {
660- final String element = tokens .nextToken ();
661- if ( property == null ) {
662- property = component .getProperty ( element );
663- }
664- else if ( property .isComposite () ) {
665- final var value = (Component ) property .getValue ();
666- property = value .getProperty ( element );
667- }
668- else {
669- return null ;
670- }
671- }
672- return property ;
673- }
609+ return isEmpty ( propertyName )
610+ ? null // Do not expect to use a primary key for this case
611+ : findProperty ( component , propertyName , null );
674612 }
675613 catch (MappingException e ) {
676614 try {
677615 // if we do not find it, try to check the identifier mapper
678- if ( component .getOwner ().getIdentifierMapper () == null ) {
679- return null ;
680- }
681- else {
682- Property property = null ;
683- final var tokens = new StringTokenizer ( propertyName , "." , false );
684- while ( tokens .hasMoreTokens () ) {
685- final String element = tokens .nextToken ();
686- if ( property == null ) {
687- property = component .getOwner ().getIdentifierMapper ().getProperty ( element );
688- }
689- else if ( property .isComposite () ) {
690- final var value = (Component ) property .getValue ();
691- property = value .getProperty ( element );
692- }
693- else {
694- return null ;
695- }
696- }
697- return property ;
698- }
616+ return findPropertyUsingIdMapper ( component .getOwner (), propertyName );
699617 }
700618 catch (MappingException ee ) {
701619 return null ;
702620 }
703621 }
704622 }
705623
624+ private static Property findProperty (
625+ PersistentClass associatedClass , String propertyName ,
626+ Property idProperty , String idName ) {
627+ Property property ;
628+ // Handle id property
629+ final String name ;
630+ if ( propertyName .indexOf ( idName + "." ) == 0 ) {
631+ property = idProperty ;
632+ name = propertyName .substring ( idName .length () + 1 );
633+ }
634+ else {
635+ property = null ;
636+ name = propertyName ;
637+ }
638+ return findProperty ( associatedClass , name , property );
639+ }
640+
641+ private static Property findProperty (AttributeContainer root , String name , Property property ) {
642+ final var tokens = new StringTokenizer ( name , "." , false );
643+ while ( tokens .hasMoreTokens () ) {
644+ final String element = tokens .nextToken ();
645+ if ( property == null ) {
646+ property = root .getProperty ( element );
647+ }
648+ else if ( property .isComposite () ) {
649+ final var value = (Component ) property .getValue ();
650+ property = value .getProperty ( element );
651+ }
652+ else {
653+ return null ;
654+ }
655+ }
656+ return property ;
657+ }
658+
659+ private static Property findPropertyUsingIdMapper (PersistentClass associatedClass , String propertyName ) {
660+ final var identifierMapper = associatedClass .getIdentifierMapper ();
661+ return identifierMapper == null ? null : findProperty ( identifierMapper , propertyName , null );
662+ }
663+
706664 public static String getRelativePath (PropertyHolder propertyHolder , String propertyName ) {
707665 if ( propertyHolder == null ) {
708666 return propertyName ;
0 commit comments