-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
AbstractPluralAttributeSourceImpl.java
334 lines (294 loc) · 9.56 KB
/
AbstractPluralAttributeSourceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.metamodel.internal.source.hbm;
import java.util.Collections;
import java.util.Map;
import org.hibernate.cfg.NotYetImplementedException;
import org.hibernate.engine.FetchStyle;
import org.hibernate.engine.FetchTiming;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.jaxb.spi.hbm.PluralAttributeElement;
import org.hibernate.metamodel.spi.binding.Caching;
import org.hibernate.metamodel.spi.binding.CustomSQL;
import org.hibernate.metamodel.spi.source.AttributeSourceContainer;
import org.hibernate.metamodel.spi.source.ExplicitHibernateTypeSource;
import org.hibernate.metamodel.spi.source.MappingException;
import org.hibernate.metamodel.spi.source.MetaAttributeSource;
import org.hibernate.metamodel.spi.source.PluralAttributeElementSource;
import org.hibernate.metamodel.spi.source.PluralAttributeKeySource;
import org.hibernate.metamodel.spi.source.PluralAttributeSource;
import org.hibernate.metamodel.spi.source.TableSpecificationSource;
/**
* @author Steve Ebersole
*/
public abstract class AbstractPluralAttributeSourceImpl
extends AbstractHbmSourceNode
implements PluralAttributeSource, Helper.InLineViewNameInferrer {
private final PluralAttributeElement pluralAttributeElement;
private final AttributeSourceContainer container;
private final ExplicitHibernateTypeSource typeInformation;
private final PluralAttributeKeySource keySource;
private final PluralAttributeElementSource elementSource;
private final Caching caching;
protected AbstractPluralAttributeSourceImpl(
MappingDocument sourceMappingDocument,
final PluralAttributeElement pluralAttributeElement,
AttributeSourceContainer container) {
super( sourceMappingDocument );
this.pluralAttributeElement = pluralAttributeElement;
this.container = container;
this.keySource = new PluralAttributeKeySourceImpl(
sourceMappingDocument(),
pluralAttributeElement.getKey(),
container
);
this.elementSource = interpretElementType();
this.caching = Helper.createCaching(
pluralAttributeElement.getCache(),
StringHelper.qualify( container().getPath(), getName() )
);
this.typeInformation = new ExplicitHibernateTypeSource() {
@Override
public String getName() {
return pluralAttributeElement.getCollectionType();
}
@Override
public Map<String, String> getParameters() {
return Collections.emptyMap();
}
};
}
private PluralAttributeElementSource interpretElementType() {
if ( pluralAttributeElement.getElement() != null ) {
return new BasicPluralAttributeElementSourceImpl(
sourceMappingDocument(),
pluralAttributeElement.getElement()
);
}
else if ( pluralAttributeElement.getCompositeElement() != null ) {
return new CompositePluralAttributeElementSourceImpl(
sourceMappingDocument(),
pluralAttributeElement.getCompositeElement(),
pluralAttributeElement.getCascade()
);
}
else if ( pluralAttributeElement.getOneToMany() != null ) {
return new OneToManyPluralAttributeElementSourceImpl(
sourceMappingDocument(),
pluralAttributeElement.getOneToMany(),
pluralAttributeElement.getCascade()
);
}
else if ( pluralAttributeElement.getManyToMany() != null ) {
return new ManyToManyPluralAttributeElementSourceImpl(
sourceMappingDocument(),
pluralAttributeElement.getManyToMany(),
pluralAttributeElement.getCascade()
);
}
else if ( pluralAttributeElement.getManyToAny() != null ) {
throw new NotYetImplementedException( "Support for many-to-any not yet implemented" );
// return Nature.MANY_TO_ANY;
}
else {
throw new MappingException(
"Unexpected collection element type : " + pluralAttributeElement.getName(),
bindingContext().getOrigin()
);
}
}
public PluralAttributeElement getPluralAttributeElement() {
return pluralAttributeElement;
}
protected AttributeSourceContainer container() {
return container;
}
@Override
public PluralAttributeKeySource getKeySource() {
return keySource;
}
@Override
public PluralAttributeElementSource getElementSource() {
return elementSource;
}
@Override
public String getMappedBy() {
return null;
}
@Override
public String inferInLineViewName() {
return container().getPath() + "." + pluralAttributeElement.getName();
}
@Override
public TableSpecificationSource getCollectionTableSpecificationSource() {
return pluralAttributeElement.getOneToMany() == null ?
Helper.createTableSource( sourceMappingDocument(), pluralAttributeElement, this ) :
null;
}
@Override
public String getCollectionTableComment() {
return pluralAttributeElement.getComment();
}
@Override
public String getCollectionTableCheck() {
return pluralAttributeElement.getCheck();
}
@Override
public Caching getCaching() {
return caching;
}
@Override
public String getWhere() {
return pluralAttributeElement.getWhere();
}
@Override
public String getName() {
return pluralAttributeElement.getName();
}
@Override
public boolean isSingular() {
return false;
}
@Override
public ExplicitHibernateTypeSource getTypeInformation() {
return typeInformation;
}
@Override
public String getPropertyAccessorName() {
return pluralAttributeElement.getAccess();
}
@Override
public boolean isIncludedInOptimisticLocking() {
return pluralAttributeElement.isOptimisticLock();
}
@Override
public boolean isInverse() {
return pluralAttributeElement.isInverse();
}
@Override
public String getCustomPersisterClassName() {
return pluralAttributeElement.getPersister();
}
@Override
public String getCustomLoaderName() {
return pluralAttributeElement.getLoader() == null
? null
: pluralAttributeElement.getLoader().getQueryRef();
}
@Override
public CustomSQL getCustomSqlInsert() {
return Helper.buildCustomSql( pluralAttributeElement.getSqlInsert() );
}
@Override
public CustomSQL getCustomSqlUpdate() {
return Helper.buildCustomSql( pluralAttributeElement.getSqlUpdate() );
}
@Override
public CustomSQL getCustomSqlDelete() {
return Helper.buildCustomSql( pluralAttributeElement.getSqlDelete() );
}
@Override
public CustomSQL getCustomSqlDeleteAll() {
return Helper.buildCustomSql( pluralAttributeElement.getSqlDeleteAll() );
}
@Override
public Iterable<? extends MetaAttributeSource> getMetaAttributeSources() {
return pluralAttributeElement.getMeta();
}
@Override
public FetchTiming getFetchTiming() {
final String fetchSelection = pluralAttributeElement.getFetch() != null ?
pluralAttributeElement.getFetch().value() :
null;
final String lazySelection = pluralAttributeElement.getLazy() != null
? pluralAttributeElement.getLazy().value()
: null;
final String outerJoinSelection = pluralAttributeElement.getOuterJoin() != null
? pluralAttributeElement.getOuterJoin().value()
: null;
if ( lazySelection == null ) {
if ( "join".equals( fetchSelection ) || "true".equals( outerJoinSelection ) ) {
return FetchTiming.IMMEDIATE;
}
else if ( "false".equals( outerJoinSelection ) ) {
return FetchTiming.DELAYED;
}
else {
return bindingContext().getMappingDefaults().areAssociationsLazy()
? FetchTiming.DELAYED
: FetchTiming.IMMEDIATE;
}
}
else if ( "extra".equals( lazySelection ) ) {
return FetchTiming.EXTRA_DELAYED;
}
else if ( "true".equals( lazySelection ) ) {
return FetchTiming.DELAYED;
}
else if ( "false".equals( lazySelection ) ) {
return FetchTiming.IMMEDIATE;
}
throw new MappingException(
String.format(
"Unexpected lazy selection [%s] on '%s'",
lazySelection,
pluralAttributeElement.getName()
),
origin()
);
}
@Override
public FetchStyle getFetchStyle() {
final String fetchSelection = pluralAttributeElement.getFetch() != null ?
pluralAttributeElement.getFetch().value() :
null;
final String outerJoinSelection = pluralAttributeElement.getOuterJoin() != null
? pluralAttributeElement.getOuterJoin().value()
: null;
final int batchSize = pluralAttributeElement.getBatchSize();
if ( fetchSelection == null ) {
if ( outerJoinSelection == null ) {
return batchSize > 1 ? FetchStyle.BATCH : FetchStyle.SELECT;
}
else {
if ( "auto".equals( outerJoinSelection ) ) {
return bindingContext().getMappingDefaults().areAssociationsLazy()
? FetchStyle.SELECT
: FetchStyle.JOIN;
}
else {
return "true".equals( outerJoinSelection ) ? FetchStyle.JOIN : FetchStyle.SELECT;
}
}
}
else {
if ( "subselect".equals( fetchSelection ) ) {
return FetchStyle.SUBSELECT;
}
else {
return "join".equals( fetchSelection ) ? FetchStyle.JOIN : FetchStyle.SELECT;
}
}
}
}