|
10 | 10 | import java.util.Iterator;
|
11 | 11 | import java.util.Locale;
|
12 | 12 | import java.util.Map;
|
| 13 | +import java.util.concurrent.atomic.AtomicInteger; |
13 | 14 |
|
14 | 15 | import org.hibernate.AnnotationException;
|
15 | 16 | import org.hibernate.AssertionFailure;
|
@@ -75,81 +76,138 @@ public void doSecondPass(Map persistentClasses) throws MappingException {
|
75 | 76 | columnByReferencedName.put( referencedColumnName.toLowerCase(Locale.ROOT), joinColumn );
|
76 | 77 | }
|
77 | 78 | //try default column orientation
|
78 |
| - int index = 0; |
| 79 | + AtomicInteger index = new AtomicInteger( 0 ); |
79 | 80 | if ( columnByReferencedName.isEmpty() ) {
|
80 | 81 | isExplicitReference = false;
|
81 | 82 | for (Ejb3JoinColumn joinColumn : joinColumns) {
|
82 |
| - columnByReferencedName.put( "" + index, joinColumn ); |
83 |
| - index++; |
| 83 | + columnByReferencedName.put( "" + index.get(), joinColumn ); |
| 84 | + index.getAndIncrement(); |
84 | 85 | }
|
85 |
| - index = 0; |
| 86 | + index.set( 0 ); |
86 | 87 | }
|
87 | 88 |
|
88 | 89 | while ( properties.hasNext() ) {
|
89 | 90 | Property referencedProperty = properties.next();
|
90 | 91 | if ( referencedProperty.isComposite() ) {
|
91 |
| - throw new AssertionFailure( "Unexpected nested component on the referenced entity when mapping a @MapsId: " |
92 |
| - + referencedEntityName); |
| 92 | + Property property = createComponentProperty( referencedPersistentClass, isExplicitReference, columnByReferencedName, index, referencedProperty ); |
| 93 | + component.addProperty( property ); |
93 | 94 | }
|
94 | 95 | else {
|
95 |
| - Property property = new Property(); |
96 |
| - property.setName( referencedProperty.getName() ); |
97 |
| - //FIXME set optional? |
98 |
| - //property.setOptional( property.isOptional() ); |
99 |
| - property.setPersistentClass( component.getOwner() ); |
100 |
| - property.setPropertyAccessorName( referencedProperty.getPropertyAccessorName() ); |
101 |
| - SimpleValue value = new SimpleValue( buildingContext.getMetadataCollector(), component.getTable() ); |
102 |
| - property.setValue( value ); |
103 |
| - final SimpleValue referencedValue = (SimpleValue) referencedProperty.getValue(); |
104 |
| - value.setTypeName( referencedValue.getTypeName() ); |
105 |
| - value.setTypeParameters( referencedValue.getTypeParameters() ); |
106 |
| - final Iterator<Selectable> columns = referencedValue.getColumnIterator(); |
107 |
| - |
108 |
| - if ( joinColumns[0].isNameDeferred() ) { |
109 |
| - joinColumns[0].copyReferencedStructureAndCreateDefaultJoinColumns( |
110 |
| - referencedPersistentClass, |
111 |
| - columns, |
112 |
| - value); |
| 96 | + Property property = createSimpleProperty( referencedPersistentClass, isExplicitReference, columnByReferencedName, index, referencedProperty ); |
| 97 | + component.addProperty( property ); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private Property createComponentProperty( |
| 103 | + PersistentClass referencedPersistentClass, |
| 104 | + boolean isExplicitReference, |
| 105 | + Map<String, Ejb3JoinColumn> columnByReferencedName, |
| 106 | + AtomicInteger index, |
| 107 | + Property referencedProperty ) { |
| 108 | + Property property = new Property(); |
| 109 | + property.setName( referencedProperty.getName() ); |
| 110 | + //FIXME set optional? |
| 111 | + //property.setOptional( property.isOptional() ); |
| 112 | + property.setPersistentClass( component.getOwner() ); |
| 113 | + property.setPropertyAccessorName( referencedProperty.getPropertyAccessorName() ); |
| 114 | + Component value = new Component( buildingContext.getMetadataCollector(), component.getOwner() ); |
| 115 | + |
| 116 | + property.setValue( value ); |
| 117 | + final Component referencedValue = (Component) referencedProperty.getValue(); |
| 118 | + value.setTypeName( referencedValue.getTypeName() ); |
| 119 | + value.setTypeParameters( referencedValue.getTypeParameters() ); |
| 120 | + value.setComponentClassName( referencedValue.getComponentClassName() ); |
| 121 | + |
| 122 | + |
| 123 | + Iterator<Property> propertyIterator = referencedValue.getPropertyIterator(); |
| 124 | + while(propertyIterator.hasNext()) |
| 125 | + { |
| 126 | + Property referencedComponentProperty = propertyIterator.next(); |
| 127 | + |
| 128 | + if ( referencedComponentProperty.isComposite() ) { |
| 129 | + Property componentProperty = createComponentProperty( referencedValue.getOwner(), isExplicitReference, columnByReferencedName, index, referencedComponentProperty ); |
| 130 | + value.addProperty( componentProperty ); |
| 131 | + } |
| 132 | + else { |
| 133 | + Property componentProperty = createSimpleProperty( referencedValue.getOwner(), isExplicitReference, columnByReferencedName, index, referencedComponentProperty ); |
| 134 | + value.addProperty( componentProperty ); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return property; |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | + private Property createSimpleProperty( |
| 143 | + PersistentClass referencedPersistentClass, |
| 144 | + boolean isExplicitReference, |
| 145 | + Map<String, Ejb3JoinColumn> columnByReferencedName, |
| 146 | + AtomicInteger index, |
| 147 | + Property referencedProperty ) { |
| 148 | + Property property = new Property(); |
| 149 | + property.setName( referencedProperty.getName() ); |
| 150 | + //FIXME set optional? |
| 151 | + //property.setOptional( property.isOptional() ); |
| 152 | + property.setPersistentClass( component.getOwner() ); |
| 153 | + property.setPropertyAccessorName( referencedProperty.getPropertyAccessorName() ); |
| 154 | + SimpleValue value = new SimpleValue( buildingContext.getMetadataCollector(), component.getTable() ); |
| 155 | + property.setValue( value ); |
| 156 | + final SimpleValue referencedValue = (SimpleValue) referencedProperty.getValue(); |
| 157 | + value.setTypeName( referencedValue.getTypeName() ); |
| 158 | + value.setTypeParameters( referencedValue.getTypeParameters() ); |
| 159 | + final Iterator<Selectable> columns = referencedValue.getColumnIterator(); |
| 160 | + |
| 161 | + if ( joinColumns[0].isNameDeferred() ) { |
| 162 | + joinColumns[0].copyReferencedStructureAndCreateDefaultJoinColumns( |
| 163 | + referencedPersistentClass, |
| 164 | + columns, |
| 165 | + value); |
| 166 | + } |
| 167 | + else { |
| 168 | + //FIXME take care of Formula |
| 169 | + while ( columns.hasNext() ) { |
| 170 | + final Selectable selectable = columns.next(); |
| 171 | + if ( ! Column.class.isInstance( selectable ) ) { |
| 172 | + log.debug( "Encountered formula definition; skipping" ); |
| 173 | + continue; |
| 174 | + } |
| 175 | + final Column column = (Column) selectable; |
| 176 | + final Ejb3JoinColumn joinColumn; |
| 177 | + String logicalColumnName = null; |
| 178 | + if ( isExplicitReference ) { |
| 179 | + final String columnName = column.getName(); |
| 180 | + logicalColumnName = buildingContext.getMetadataCollector().getLogicalColumnName( |
| 181 | + referencedPersistentClass.getTable(), |
| 182 | + columnName |
| 183 | + ); |
| 184 | + //JPA 2 requires referencedColumnNames to be case insensitive |
| 185 | + joinColumn = columnByReferencedName.get( logicalColumnName.toLowerCase(Locale.ROOT ) ); |
113 | 186 | }
|
114 | 187 | else {
|
115 |
| - //FIXME take care of Formula |
116 |
| - while ( columns.hasNext() ) { |
117 |
| - final Selectable selectable = columns.next(); |
118 |
| - if ( ! Column.class.isInstance( selectable ) ) { |
119 |
| - log.debug( "Encountered formula definition; skipping" ); |
120 |
| - continue; |
121 |
| - } |
122 |
| - final Column column = (Column) selectable; |
123 |
| - final Ejb3JoinColumn joinColumn; |
124 |
| - String logicalColumnName = null; |
125 |
| - if ( isExplicitReference ) { |
126 |
| - final String columnName = column.getName(); |
127 |
| - logicalColumnName = buildingContext.getMetadataCollector().getLogicalColumnName( |
128 |
| - referencedPersistentClass.getTable(), |
129 |
| - columnName |
130 |
| - ); |
131 |
| - //JPA 2 requires referencedColumnNames to be case insensitive |
132 |
| - joinColumn = columnByReferencedName.get( logicalColumnName.toLowerCase(Locale.ROOT ) ); |
133 |
| - } |
134 |
| - else { |
135 |
| - joinColumn = columnByReferencedName.get( "" + index ); |
136 |
| - index++; |
137 |
| - } |
138 |
| - if ( joinColumn == null && ! joinColumns[0].isNameDeferred() ) { |
139 |
| - throw new AnnotationException( |
140 |
| - isExplicitReference ? |
141 |
| - "Unable to find column reference in the @MapsId mapping: " + logicalColumnName : |
142 |
| - "Implicit column reference in the @MapsId mapping fails, try to use explicit referenceColumnNames: " + referencedEntityName |
143 |
| - ); |
144 |
| - } |
145 |
| - final String columnName = joinColumn == null || joinColumn.isNameDeferred() ? "tata_" + column.getName() : joinColumn |
146 |
| - .getName(); |
147 |
| - value.addColumn( new Column( columnName ) ); |
148 |
| - column.setValue( value ); |
149 |
| - } |
| 188 | + joinColumn = columnByReferencedName.get( "" + index.get() ); |
| 189 | + index.getAndIncrement(); |
150 | 190 | }
|
151 |
| - component.addProperty( property ); |
| 191 | + if ( joinColumn == null && ! joinColumns[0].isNameDeferred() ) { |
| 192 | + throw new AnnotationException( |
| 193 | + isExplicitReference ? |
| 194 | + "Unable to find column reference in the @MapsId mapping: " + logicalColumnName : |
| 195 | + "Implicit column reference in the @MapsId mapping fails, try to use explicit referenceColumnNames: " + referencedEntityName |
| 196 | + ); |
| 197 | + } |
| 198 | + final String columnName = joinColumn == null || joinColumn.isNameDeferred() ? "tata_" + column.getName() : joinColumn |
| 199 | + .getName(); |
| 200 | + value.addColumn( new Column( columnName ) ); |
| 201 | + if ( joinColumn != null ) { |
| 202 | + joinColumn.linkWithValue( value ); |
| 203 | + } |
| 204 | + column.setValue( value ); |
152 | 205 | }
|
153 | 206 | }
|
| 207 | + return property; |
| 208 | + } |
| 209 | + |
| 210 | + public boolean dependentUpon( CopyIdentifierComponentSecondPass other ) { |
| 211 | + return this.referencedEntityName.equals( other.component.getOwner().getEntityName() ); |
154 | 212 | }
|
155 | 213 | }
|
0 commit comments