|
30 | 30 | public class FetchMementoHbmStandard implements FetchMemento, FetchMemento.Parent { |
31 | 31 |
|
32 | 32 | private static final String ELEMENT_PREFIX = "element."; |
| 33 | + private static final int ELEMENT_PREFIX_LENGTH = 8; |
33 | 34 |
|
34 | 35 | public interface FetchParentMemento { |
35 | 36 | NavigablePath getNavigablePath(); |
@@ -74,55 +75,110 @@ public FetchBuilder resolve( |
74 | 75 | Parent parent, |
75 | 76 | Consumer<String> querySpaceConsumer, |
76 | 77 | ResultSetMappingResolutionContext context) { |
77 | | - final Map<String, FetchBuilder> fetchBuilderMap = new HashMap<>(); |
78 | | - fetchMementoMap.forEach( |
79 | | - (attrName, fetchMemento) -> fetchBuilderMap.put( |
80 | | - attrName, |
81 | | - fetchMemento.resolve(this, querySpaceConsumer, context ) |
82 | | - ) |
83 | | - ); |
84 | | - final DynamicResultBuilderEntityStandard resultBuilder; |
85 | | - if ( fetchable instanceof PluralAttributeMapping ) { |
86 | | - resultBuilder = new DynamicResultBuilderEntityStandard( |
87 | | - (EntityMappingType) ( (PluralAttributeMapping) fetchable ).getElementDescriptor().getPartMappingType(), |
88 | | - tableAlias, |
89 | | - navigablePath |
90 | | - ); |
91 | | - FetchBuilder element = fetchBuilderMap.get( "element" ); |
92 | | - if ( element != null ) { |
93 | | - if ( element instanceof DynamicFetchBuilder ) { |
94 | | - resultBuilder.addIdColumnAliases( |
95 | | - ( (DynamicFetchBuilder) element ).getColumnAliases().toArray( new String[0] ) |
96 | | - ); |
97 | | - } |
98 | | - else { |
99 | | - resultBuilder.addIdColumnAliases( |
100 | | - ( (CompleteFetchBuilderEntityValuedModelPart) element ).getColumnAliases().toArray( new String[0] ) |
101 | | - ); |
102 | | - } |
103 | | - } |
104 | | - FetchBuilder index = fetchBuilderMap.get( "index" ); |
105 | | - if ( index != null ) { |
106 | | - resultBuilder.addFetchBuilder( CollectionPart.Nature.INDEX.getName(), index ); |
107 | | - } |
108 | | - for ( Map.Entry<String, FetchBuilder> entry : fetchBuilderMap.entrySet() ) { |
109 | | - if ( entry.getKey().startsWith( ELEMENT_PREFIX ) ) { |
110 | | - resultBuilder.addFetchBuilder( entry.getKey().substring( ELEMENT_PREFIX.length() ), entry.getValue() ); |
111 | | - } |
112 | | - } |
| 78 | + if ( fetchable instanceof PluralAttributeMapping pluralAttributeMapping ) { |
| 79 | + return resolve( pluralAttributeMapping, querySpaceConsumer, context ); |
113 | 80 | } |
114 | 81 | else { |
115 | | - resultBuilder = new DynamicResultBuilderEntityStandard( |
116 | | - ( (ToOneAttributeMapping) fetchable ).getEntityMappingType(), |
117 | | - tableAlias, |
118 | | - navigablePath |
119 | | - ); |
120 | | - fetchBuilderMap.forEach( resultBuilder::addFetchBuilder ); |
| 82 | + return resolve( (ToOneAttributeMapping) fetchable, querySpaceConsumer, context ); |
121 | 83 | } |
| 84 | + } |
| 85 | + |
| 86 | + private FetchBuilder resolve( |
| 87 | + PluralAttributeMapping pluralAttributeMapping, |
| 88 | + Consumer<String> querySpaceConsumer, |
| 89 | + ResultSetMappingResolutionContext context) { |
| 90 | + final DynamicResultBuilderEntityStandard resultBuilder; |
| 91 | + EntityMappingType partMappingType = (EntityMappingType) pluralAttributeMapping.getElementDescriptor() |
| 92 | + .getPartMappingType(); |
| 93 | + resultBuilder = new DynamicResultBuilderEntityStandard( |
| 94 | + partMappingType, |
| 95 | + tableAlias, |
| 96 | + navigablePath |
| 97 | + ); |
| 98 | + final Map<Fetchable, FetchBuilder> fetchBuilderMap = new HashMap<>(); |
| 99 | + fetchMementoMap.forEach( |
| 100 | + (attrName, fetchMemento) -> { |
| 101 | + final FetchBuilder fetchBuilder = fetchMemento.resolve( this, querySpaceConsumer, context ); |
| 102 | + |
| 103 | + if ( attrName.equals( "element" ) ) { |
| 104 | + if ( fetchBuilder instanceof DynamicFetchBuilder dynamicFetchBuilder ) { |
| 105 | + resultBuilder.addIdColumnAliases( |
| 106 | + dynamicFetchBuilder.getColumnAliases().toArray( new String[0] ) |
| 107 | + ); |
| 108 | + } |
| 109 | + else { |
| 110 | + resultBuilder.addIdColumnAliases( |
| 111 | + ((CompleteFetchBuilderEntityValuedModelPart) fetchBuilder).getColumnAliases() |
| 112 | + .toArray( new String[0] ) |
| 113 | + ); |
| 114 | + } |
| 115 | + fetchBuilderMap.put( |
| 116 | + pluralAttributeMapping.getElementDescriptor(), |
| 117 | + fetchBuilder |
| 118 | + ); |
| 119 | + } |
| 120 | + else if ( attrName.equals( "index" ) ) { |
| 121 | + final CollectionPart indexDescriptor = pluralAttributeMapping.getIndexDescriptor(); |
| 122 | + resultBuilder.addFetchBuilder( indexDescriptor, fetchBuilder ); |
| 123 | + fetchBuilderMap.put( |
| 124 | + indexDescriptor, |
| 125 | + fetchBuilder |
| 126 | + ); |
| 127 | + } |
| 128 | + else if ( attrName.startsWith( ELEMENT_PREFIX ) ) { |
| 129 | + final Fetchable attributeMapping = (Fetchable) partMappingType.findByPath( |
| 130 | + attrName.substring( ELEMENT_PREFIX_LENGTH ) ); |
| 131 | + resultBuilder.addFetchBuilder( attributeMapping, fetchBuilder ); |
| 132 | + fetchBuilderMap.put( |
| 133 | + attributeMapping, |
| 134 | + fetchBuilder |
| 135 | + ); |
| 136 | + } |
| 137 | + else { |
| 138 | + final Fetchable attributeMapping = (Fetchable) partMappingType.findByPath( attrName ); |
| 139 | + resultBuilder.addFetchBuilder( attributeMapping, fetchBuilder ); |
| 140 | + fetchBuilderMap.put( |
| 141 | + attributeMapping, |
| 142 | + fetchBuilder |
| 143 | + ); |
| 144 | + } |
| 145 | + } |
| 146 | + ); |
| 147 | + return new DynamicFetchBuilderLegacy( |
| 148 | + tableAlias, |
| 149 | + ownerTableAlias, |
| 150 | + fetchable, |
| 151 | + keyColumnNames, |
| 152 | + fetchBuilderMap, |
| 153 | + resultBuilder |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + private FetchBuilder resolve( |
| 158 | + ToOneAttributeMapping toOneAttributeMapping, |
| 159 | + Consumer<String> querySpaceConsumer, |
| 160 | + ResultSetMappingResolutionContext context) { |
| 161 | + final Map<Fetchable, FetchBuilder> fetchBuilderMap = new HashMap<>(); |
| 162 | + fetchMementoMap.forEach( |
| 163 | + (attrName, fetchMemento) -> |
| 164 | + fetchBuilderMap.put( |
| 165 | + (Fetchable) toOneAttributeMapping.findSubPart( attrName ), |
| 166 | + fetchMemento.resolve( this, querySpaceConsumer, context ) |
| 167 | + ) |
| 168 | + ); |
| 169 | + final DynamicResultBuilderEntityStandard resultBuilder; |
| 170 | + resultBuilder = new DynamicResultBuilderEntityStandard( |
| 171 | + toOneAttributeMapping.getEntityMappingType(), |
| 172 | + tableAlias, |
| 173 | + navigablePath |
| 174 | + ); |
| 175 | + fetchBuilderMap.forEach( (fetchable, fetchBuilder) -> |
| 176 | + resultBuilder.addFetchBuilder( fetchable, fetchBuilder ) |
| 177 | + ); |
122 | 178 | return new DynamicFetchBuilderLegacy( |
123 | 179 | tableAlias, |
124 | 180 | ownerTableAlias, |
125 | | - fetchable.getFetchableName(), |
| 181 | + fetchable, |
126 | 182 | keyColumnNames, |
127 | 183 | fetchBuilderMap, |
128 | 184 | resultBuilder |
|
0 commit comments