-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
CustomPersister.java
741 lines (612 loc) · 17.2 KB
/
CustomPersister.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.legacy;
import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.hibernate.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.MappingException;
import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata;
import org.hibernate.cache.spi.access.EntityDataAccess;
import org.hibernate.cache.spi.access.NaturalIdDataAccess;
import org.hibernate.cache.spi.entry.CacheEntry;
import org.hibernate.cache.spi.entry.CacheEntryStructure;
import org.hibernate.cache.spi.entry.StandardCacheEntryImpl;
import org.hibernate.cache.spi.entry.UnstructuredCacheEntry;
import org.hibernate.cfg.NotYetImplementedException;
import org.hibernate.engine.internal.MutableEntityEntryFactory;
import org.hibernate.engine.internal.TwoPhaseLoad;
import org.hibernate.engine.spi.CascadeStyle;
import org.hibernate.engine.spi.EntityEntryFactory;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.engine.spi.ValueInclusion;
import org.hibernate.event.spi.EventSource;
import org.hibernate.event.spi.PostLoadEvent;
import org.hibernate.event.spi.PreLoadEvent;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.UUIDHexGenerator;
import org.hibernate.internal.FilterAliasGenerator;
import org.hibernate.internal.StaticFilterAliasGenerator;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.metamodel.model.domain.NavigableRole;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.persister.entity.MultiLoadOptions;
import org.hibernate.persister.spi.PersisterCreationContext;
import org.hibernate.persister.walking.spi.AttributeDefinition;
import org.hibernate.persister.walking.spi.EntityIdentifierDefinition;
import org.hibernate.tuple.entity.BytecodeEnhancementMetadataNonPojoImpl;
import org.hibernate.tuple.entity.EntityMetamodel;
import org.hibernate.tuple.entity.EntityTuplizer;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;
import org.hibernate.type.VersionType;
public class CustomPersister implements EntityPersister {
private static final Hashtable INSTANCES = new Hashtable();
private static final IdentifierGenerator GENERATOR = new UUIDHexGenerator();
private SessionFactoryImplementor factory;
private EntityMetamodel entityMetamodel;
@SuppressWarnings("UnusedParameters")
public CustomPersister(
PersistentClass model,
EntityDataAccess cacheAccessStrategy,
NaturalIdDataAccess naturalIdRegionAccessStrategy,
PersisterCreationContext creationContext) {
this.factory = creationContext.getSessionFactory();
this.entityMetamodel = new EntityMetamodel( model, this, creationContext.getSessionFactory() );
}
public boolean hasLazyProperties() {
return false;
}
public boolean isInherited() {
return false;
}
public SessionFactoryImplementor getFactory() {
return factory;
}
@Override
public NavigableRole getNavigableRole() {
return new NavigableRole( getEntityName() );
}
@Override
public EntityEntryFactory getEntityEntryFactory() {
return MutableEntityEntryFactory.INSTANCE;
}
@Override
public Class getMappedClass() {
return Custom.class;
}
@Override
public void generateEntityDefinition() {
}
public void postInstantiate() throws MappingException {}
public String getEntityName() {
return Custom.class.getName();
}
public boolean isSubclassEntityName(String entityName) {
return Custom.class.getName().equals(entityName);
}
public boolean hasProxy() {
return false;
}
public boolean hasCollections() {
return false;
}
public boolean hasCascades() {
return false;
}
public boolean isMutable() {
return true;
}
public boolean isSelectBeforeUpdateRequired() {
return false;
}
public boolean isIdentifierAssignedByInsert() {
return false;
}
public Boolean isTransient(Object object, SharedSessionContractImplementor session) {
return ( (Custom) object ).id==null;
}
@Override
public Object[] getPropertyValuesToInsert(Object object, Map mergeMap, SharedSessionContractImplementor session) {
return getPropertyValues( object );
}
public void processInsertGeneratedProperties(Serializable id, Object entity, Object[] state, SharedSessionContractImplementor session) {
}
public void processUpdateGeneratedProperties(Serializable id, Object entity, Object[] state, SharedSessionContractImplementor session) {
}
public void retrieveGeneratedProperties(Serializable id, Object entity, Object[] state, SharedSessionContractImplementor session) {
throw new UnsupportedOperationException();
}
@Override
public boolean implementsLifecycle() {
return false;
}
@Override
public Class getConcreteProxyClass() {
return Custom.class;
}
@Override
public void setPropertyValues(Object object, Object[] values) {
setPropertyValue( object, 0, values[0] );
}
@Override
public void setPropertyValue(Object object, int i, Object value) {
( (Custom) object ).setName( (String) value );
}
@Override
public Object[] getPropertyValues(Object object) throws HibernateException {
Custom c = (Custom) object;
return new Object[] { c.getName() };
}
@Override
public Object getPropertyValue(Object object, int i) throws HibernateException {
return ( (Custom) object ).getName();
}
@Override
public Object getPropertyValue(Object object, String propertyName) throws HibernateException {
return ( (Custom) object ).getName();
}
@Override
public Serializable getIdentifier(Object object) throws HibernateException {
return ( (Custom) object ).id;
}
@Override
public Serializable getIdentifier(Object entity, SharedSessionContractImplementor session) {
return ( (Custom) entity ).id;
}
@Override
public void setIdentifier(Object entity, Serializable id, SharedSessionContractImplementor session) {
( (Custom) entity ).id = (String) id;
}
@Override
public Object getVersion(Object object) throws HibernateException {
return null;
}
@Override
public Object instantiate(Serializable id, SharedSessionContractImplementor session) {
Custom c = new Custom();
c.id = (String) id;
return c;
}
@Override
public boolean isInstance(Object object) {
return object instanceof Custom;
}
@Override
public boolean hasUninitializedLazyProperties(Object object) {
return false;
}
@Override
public void resetIdentifier(Object entity, Serializable currentId, Object currentVersion, SharedSessionContractImplementor session) {
( ( Custom ) entity ).id = ( String ) currentId;
}
public EntityPersister getSubclassEntityPersister(Object instance, SessionFactoryImplementor factory) {
return this;
}
public int[] findDirty(
Object[] x,
Object[] y,
Object owner,
SharedSessionContractImplementor session) throws HibernateException {
if ( !Objects.equals( x[0], y[0] ) ) {
return new int[] { 0 };
}
else {
return null;
}
}
public int[] findModified(
Object[] x,
Object[] y,
Object owner,
SharedSessionContractImplementor session) throws HibernateException {
if ( !Objects.equals( x[0], y[0] ) ) {
return new int[] { 0 };
}
else {
return null;
}
}
/**
* @see EntityPersister#hasIdentifierProperty()
*/
public boolean hasIdentifierProperty() {
return true;
}
/**
* @see EntityPersister#isVersioned()
*/
public boolean isVersioned() {
return false;
}
/**
* @see EntityPersister#getVersionType()
*/
public VersionType getVersionType() {
return null;
}
/**
* @see EntityPersister#getVersionProperty()
*/
public int getVersionProperty() {
return 0;
}
/**
* @see EntityPersister#getIdentifierGenerator()
*/
public IdentifierGenerator getIdentifierGenerator()
throws HibernateException {
return GENERATOR;
}
/**
* @see EntityPersister#load(Serializable, Object, org.hibernate.LockOptions , SharedSessionContractImplementor)
*/
public Object load(
Serializable id,
Object optionalObject,
LockOptions lockOptions,
SharedSessionContractImplementor session
) throws HibernateException {
return load(id, optionalObject, lockOptions.getLockMode(), session);
}
@Override
public List multiLoad(Serializable[] ids, SharedSessionContractImplementor session, MultiLoadOptions loadOptions) {
return Collections.emptyList();
}
/**
* @see EntityPersister#load(Serializable, Object, LockMode, SharedSessionContractImplementor)
*/
public Object load(
Serializable id,
Object optionalObject,
LockMode lockMode,
SharedSessionContractImplementor session
) throws HibernateException {
// fails when optional object is supplied
Custom clone = null;
Custom obj = (Custom) INSTANCES.get(id);
if (obj!=null) {
clone = (Custom) obj.clone();
TwoPhaseLoad.addUninitializedEntity(
session.generateEntityKey( id, this ),
clone,
this,
LockMode.NONE,
session
);
TwoPhaseLoad.postHydrate(
this, id,
new String[] { obj.getName() },
null,
clone,
LockMode.NONE,
session
);
TwoPhaseLoad.initializeEntity(
clone,
false,
session,
new PreLoadEvent( (EventSource) session )
);
TwoPhaseLoad.postLoad( clone, session, new PostLoadEvent( (EventSource) session ) );
}
return clone;
}
/**
* @see EntityPersister#lock(Serializable, Object, Object, LockMode, SharedSessionContractImplementor)
*/
public void lock(
Serializable id,
Object version,
Object object,
LockOptions lockOptions,
SharedSessionContractImplementor session
) throws HibernateException {
throw new UnsupportedOperationException();
}
/**
* @see EntityPersister#lock(Serializable, Object, Object, LockMode, SharedSessionContractImplementor)
*/
public void lock(
Serializable id,
Object version,
Object object,
LockMode lockMode,
SharedSessionContractImplementor session
) throws HibernateException {
throw new UnsupportedOperationException();
}
public void insert(
Serializable id,
Object[] fields,
Object object,
SharedSessionContractImplementor session
) throws HibernateException {
INSTANCES.put(id, ( (Custom) object ).clone() );
}
public Serializable insert(Object[] fields, Object object, SharedSessionContractImplementor session)
throws HibernateException {
throw new UnsupportedOperationException();
}
public void delete(
Serializable id,
Object version,
Object object,
SharedSessionContractImplementor session
) throws HibernateException {
INSTANCES.remove(id);
}
/**
* @see EntityPersister
*/
public void update(
Serializable id,
Object[] fields,
int[] dirtyFields,
boolean hasDirtyCollection,
Object[] oldFields,
Object oldVersion,
Object object,
Object rowId,
SharedSessionContractImplementor session
) throws HibernateException {
INSTANCES.put( id, ( (Custom) object ).clone() );
}
private static final Type[] TYPES = new Type[] { StandardBasicTypes.STRING };
private static final String[] NAMES = new String[] { "name" };
private static final boolean[] MUTABILITY = new boolean[] { true };
private static final boolean[] GENERATION = new boolean[] { false };
/**
* @see EntityPersister#getPropertyTypes()
*/
public Type[] getPropertyTypes() {
return TYPES;
}
/**
* @see EntityPersister#getPropertyNames()
*/
public String[] getPropertyNames() {
return NAMES;
}
/**
* @see EntityPersister#getPropertyCascadeStyles()
*/
public CascadeStyle[] getPropertyCascadeStyles() {
return null;
}
/**
* @see EntityPersister#getIdentifierType()
*/
public Type getIdentifierType() {
return StandardBasicTypes.STRING;
}
/**
* @see EntityPersister#getIdentifierPropertyName()
*/
public String getIdentifierPropertyName() {
return "id";
}
public boolean hasCache() {
return false;
}
public EntityDataAccess getCacheAccessStrategy() {
return null;
}
public boolean hasNaturalIdCache() {
return false;
}
public NaturalIdDataAccess getNaturalIdCacheAccessStrategy() {
return null;
}
public String getRootEntityName() {
return "CUSTOMS";
}
public Serializable[] getPropertySpaces() {
return new String[] { "CUSTOMS" };
}
public Serializable[] getQuerySpaces() {
return new String[] { "CUSTOMS" };
}
/**
* @see EntityPersister#getClassMetadata()
*/
public ClassMetadata getClassMetadata() {
return null;
}
public boolean[] getPropertyUpdateability() {
return MUTABILITY;
}
public boolean[] getPropertyCheckability() {
return MUTABILITY;
}
/**
* @see EntityPersister#getPropertyInsertability()
*/
public boolean[] getPropertyInsertability() {
return MUTABILITY;
}
public ValueInclusion[] getPropertyInsertGenerationInclusions() {
return new ValueInclusion[0];
}
public ValueInclusion[] getPropertyUpdateGenerationInclusions() {
return new ValueInclusion[0];
}
public boolean canExtractIdOutOfEntity() {
return true;
}
public boolean isBatchLoadable() {
return false;
}
public Type getPropertyType(String propertyName) {
throw new UnsupportedOperationException();
}
public Object createProxy(Serializable id, SharedSessionContractImplementor session)
throws HibernateException {
throw new UnsupportedOperationException("no proxy for this class");
}
public Object getCurrentVersion(
Serializable id,
SharedSessionContractImplementor session)
throws HibernateException {
return INSTANCES.get(id);
}
@Override
public Object forceVersionIncrement(Serializable id, Object currentVersion, SharedSessionContractImplementor session)
throws HibernateException {
return null;
}
@Override
public boolean[] getPropertyNullability() {
return MUTABILITY;
}
@Override
public boolean isCacheInvalidationRequired() {
return false;
}
@Override
public void afterInitialize(Object entity, SharedSessionContractImplementor session) {
}
@Override
public void afterReassociate(Object entity, SharedSessionContractImplementor session) {
}
@Override
public Object[] getDatabaseSnapshot(Serializable id, SharedSessionContractImplementor session) throws HibernateException {
return null;
}
@Override
public Serializable getIdByUniqueKey(Serializable key, String uniquePropertyName, SharedSessionContractImplementor session) {
throw new UnsupportedOperationException( "not supported" );
}
@Override
public boolean[] getPropertyVersionability() {
return MUTABILITY;
}
@Override
public CacheEntryStructure getCacheEntryStructure() {
return UnstructuredCacheEntry.INSTANCE;
}
@Override
public CacheEntry buildCacheEntry(
Object entity, Object[] state, Object version, SharedSessionContractImplementor session) {
return new StandardCacheEntryImpl(
state,
this,
version,
session,
entity
);
}
@Override
public boolean hasSubselectLoadableCollections() {
return false;
}
@Override
public int[] getNaturalIdentifierProperties() {
return null;
}
@Override
public boolean hasNaturalIdentifier() {
return false;
}
@Override
public boolean hasMutableProperties() {
return false;
}
@Override
public boolean isInstrumented() {
return false;
}
@Override
public boolean hasInsertGeneratedProperties() {
return false;
}
@Override
public boolean hasUpdateGeneratedProperties() {
return false;
}
@Override
public boolean[] getPropertyLaziness() {
return null;
}
@Override
public boolean isLazyPropertiesCacheable() {
return true;
}
@Override
public boolean canReadFromCache() {
return false;
}
@Override
public boolean canWriteToCache() {
return false;
}
@Override
public boolean isVersionPropertyGenerated() {
return false;
}
@Override
public Object[] getNaturalIdentifierSnapshot(Serializable id, SharedSessionContractImplementor session) throws HibernateException {
return null;
}
@Override
public Serializable loadEntityIdByNaturalId(Object[] naturalIdValues, LockOptions lockOptions,
SharedSessionContractImplementor session) {
return null;
}
public Comparator getVersionComparator() {
return null;
}
@Override
public EntityMetamodel getEntityMetamodel() {
return entityMetamodel;
}
@Override
public EntityMode getEntityMode() {
return EntityMode.POJO;
}
@Override
public EntityTuplizer getEntityTuplizer() {
return null;
}
@Override
public BytecodeEnhancementMetadata getInstrumentationMetadata() {
return new BytecodeEnhancementMetadataNonPojoImpl( getEntityName() );
}
@Override
public FilterAliasGenerator getFilterAliasGenerator(String rootAlias) {
return new StaticFilterAliasGenerator(rootAlias);
}
@Override
public EntityPersister getEntityPersister() {
return this;
}
@Override
public EntityIdentifierDefinition getEntityKeyDefinition() {
throw new NotYetImplementedException();
}
@Override
public Iterable<AttributeDefinition> getAttributes() {
throw new NotYetImplementedException();
}
@Override
public int[] resolveAttributeIndexes(String[] attributeNames) {
return null;
}
@Override
public boolean canUseReferenceCacheEntries() {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
}