Project

General

Profile

« Previous | Next » 

Revision db8899fb

Added by Andreas Müller over 3 years ago

ref #9204 uncritical updates to AbstractPersistentCollection

View differences:

eu.etaxonomy.taxeditor.cdmlib/src/main/java/org/hibernate/collection/internal/AbstractPersistentCollection.java
34 34
import java.util.Iterator;
35 35
import java.util.List;
36 36
import java.util.ListIterator;
37
import java.util.Map;
37 38

  
38 39
import javax.naming.NamingException;
39 40

  
......
41 42
import org.hibernate.HibernateException;
42 43
import org.hibernate.LazyInitializationException;
43 44
import org.hibernate.Session;
45
import org.hibernate.collection.internal.AbstractPersistentCollection.DelayedOperation;
46
import org.hibernate.collection.internal.AbstractPersistentCollection.IteratorProxy;
47
import org.hibernate.collection.internal.AbstractPersistentCollection.ListIteratorProxy;
48
import org.hibernate.collection.internal.AbstractPersistentCollection.ValueDelayedOperation;
44 49
import org.hibernate.collection.spi.PersistentCollection;
45 50
import org.hibernate.engine.internal.ForeignKeys;
46 51
import org.hibernate.engine.spi.CollectionEntry;
......
56 61
import org.hibernate.persister.collection.CollectionPersister;
57 62
import org.hibernate.persister.entity.EntityPersister;
58 63
import org.hibernate.pretty.MessageHelper;
64
import org.hibernate.type.IntegerType;
65
import org.hibernate.type.LongType;
66
import org.hibernate.type.PostgresUUIDType;
67
import org.hibernate.type.StringType;
59 68
import org.hibernate.type.Type;
69
import org.hibernate.type.UUIDBinaryType;
70
import org.hibernate.type.UUIDCharType;
60 71
import org.jboss.logging.Logger;
61 72

  
62 73
import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteConfiguration;
......
116 127
	private String sessionFactoryUuid;
117 128
	private boolean specjLazyLoad = false;
118 129

  
130
	/**
131
	 * Not called by Hibernate, but used by non-JDK serialization,
132
	 * eg. SOAP libraries.
133
	 */
134
	public AbstractPersistentCollection() {
135
	}
136

  
137
	protected AbstractPersistentCollection(SessionImplementor session) {
138
		this.session = session;
139
	}
140

  
119 141
	@Override
120
    public final String getRole() {
142
	public final String getRole() {
121 143
		return role;
122 144
	}
123 145

  
124 146
	@Override
125
    public final Serializable getKey() {
147
	public final Serializable getKey() {
126 148
		return key;
127 149
	}
128 150

  
129 151
	@Override
130
    public final boolean isUnreferenced() {
152
	public final boolean isUnreferenced() {
131 153
		return role == null;
132 154
	}
133 155

  
134 156
	@Override
135
    public final boolean isDirty() {
157
	public final boolean isDirty() {
136 158
		return dirty;
137 159
	}
138 160

  
139 161
	@Override
140
    public final void clearDirty() {
162
	public final void clearDirty() {
141 163
		dirty = false;
142 164
	}
143 165

  
144 166
	@Override
145
    public final void dirty() {
167
	public final void dirty() {
146 168
		dirty = true;
147 169
	}
148 170

  
149 171
	@Override
150
    public final Serializable getStoredSnapshot() {
172
	public final Serializable getStoredSnapshot() {
151 173
		return storedSnapshot;
152 174
	}
153 175

  
154 176
	//Careful: these methods do not initialize the collection.
155 177

  
156
	/**
157
	 * Is the initialized collection empty?
158
	 */
159 178
	@Override
160
    public abstract boolean empty();
179
	public abstract boolean empty();
161 180

  
162 181
	/**
163 182
	 * Called by any read-only method of the collection interface
......
217 236
		return false;
218 237
	}
219 238

  
239
	/**
240
	 * TBH not sure why this is public
241
	 *
242
	 * @param <T> The java type of the return for this LazyInitializationWork
243
	 */
220 244
	public static interface LazyInitializationWork<T> {
245
		/**
246
		 * Do the represented work and return the result.
247
		 *
248
		 * @return The result
249
		 */
221 250
		public T doWork();
222 251
	}
223 252

  
......
429 458
		return cachedSize;
430 459
	}
431 460

  
432
	private boolean isConnectedToSession() {
433
		return session != null &&
434
				session.isOpen() &&
435
				session.getPersistenceContext().containsCollection( this );
461
	protected boolean isConnectedToSession() {
462
		return session != null
463
				&& session.isOpen()
464
				&& session.getPersistenceContext().containsCollection( this );
465
	}
466

  
467
	protected boolean isInitialized() {
468
		return initialized;
436 469
	}
437 470

  
438 471
	/**
......
449 482
	 */
450 483
	@SuppressWarnings({"JavaDoc"})
451 484
	protected boolean isOperationQueueEnabled() {
452
		return !initialized &&
453
				isConnectedToSession() &&
454
				isInverseCollection();
485
		return !initialized
486
				&& isConnectedToSession()
487
				&& isInverseCollection();
455 488
	}
456 489

  
457 490
	/**
......
461 494
	 */
462 495
	@SuppressWarnings({"JavaDoc"})
463 496
	protected boolean isPutQueueEnabled() {
464
		return !initialized &&
465
				isConnectedToSession() &&
466
				isInverseOneToManyOrNoOrphanDelete();
497
		return !initialized
498
				&& isConnectedToSession()
499
				&& isInverseOneToManyOrNoOrphanDelete();
467 500
	}
468 501

  
469 502
	/**
......
473 506
	 */
474 507
	@SuppressWarnings({"JavaDoc"})
475 508
	protected boolean isClearQueueEnabled() {
476
		return !initialized &&
477
				isConnectedToSession() &&
478
				isInverseCollectionNoOrphanDelete();
509
		return !initialized
510
				&& isConnectedToSession()
511
				&& isInverseCollectionNoOrphanDelete();
479 512
	}
480 513

  
481 514
	/**
482 515
	 * Is this the "inverse" end of a bidirectional association?
483 516
	 */
484 517
	@SuppressWarnings({"JavaDoc"})
485
	private boolean isInverseCollection() {
486
		CollectionEntry ce = session.getPersistenceContext().getCollectionEntry( this );
518
	protected boolean isInverseCollection() {
519
		final CollectionEntry ce = session.getPersistenceContext().getCollectionEntry( this );
487 520
		return ce != null && ce.getLoadedPersister().isInverse();
488 521
	}
489 522

  
......
492 525
	 * no orphan delete enabled?
493 526
	 */
494 527
	@SuppressWarnings({"JavaDoc"})
495
	private boolean isInverseCollectionNoOrphanDelete() {
496
		CollectionEntry ce = session.getPersistenceContext().getCollectionEntry( this );
497
		return ce != null &&
528
	protected boolean isInverseCollectionNoOrphanDelete() {
529
		final CollectionEntry ce = session.getPersistenceContext().getCollectionEntry( this );
530
		return ce != null
531
				&&
498 532
				ce.getLoadedPersister().isInverse() &&
499 533
				!ce.getLoadedPersister().hasOrphanDelete();
500 534
	}
......
504 538
	 * of a collection with no orphan delete?
505 539
	 */
506 540
	@SuppressWarnings({"JavaDoc"})
507
	private boolean isInverseOneToManyOrNoOrphanDelete() {
508
		CollectionEntry ce = session.getPersistenceContext().getCollectionEntry( this );
509
		return ce != null && ce.getLoadedPersister().isInverse() && (
510
				ce.getLoadedPersister().isOneToMany() ||
511
						!ce.getLoadedPersister().hasOrphanDelete()
512
		);
541
	protected boolean isInverseOneToManyOrNoOrphanDelete() {
542
		final CollectionEntry ce = session.getPersistenceContext().getCollectionEntry( this );
543
		return ce != null
544
				&& ce.getLoadedPersister().isInverse()
545
				&& ( ce.getLoadedPersister().isOneToMany() || !ce.getLoadedPersister().hasOrphanDelete() );
513 546
	}
514 547

  
515 548
	/**
......
521 554
			operationQueue = new ArrayList<DelayedOperation>( 10 );
522 555
		}
523 556
		operationQueue.add( operation );
524
		dirty = true; //needed so that we remove this collection from the second-level cache
557
		//needed so that we remove this collection from the second-level cache
558
		dirty = true;
559
	}
560

  
561
	/**
562
	 * Replace entity instances with copy in {@code copyCache}/.
563
	 *
564
	 * @param copyCache - mapping from entity in the process of being
565
	 *                    merged to managed copy.
566
	 */
567
	public final void replaceQueuedOperationValues(CollectionPersister persister, Map copyCache) {
568
		for ( DelayedOperation operation : operationQueue ) {
569
			if ( ValueDelayedOperation.class.isInstance( operation ) ) {
570
				( (ValueDelayedOperation) operation ).replace( persister, copyCache );
571
			}
572
		}
525 573
	}
526 574

  
527 575
	/**
......
534 582
		}
535 583
	}
536 584

  
537
	/**
538
	 * After flushing, re-init snapshot state.
539
	 */
540 585
	@Override
541
    public void setSnapshot(Serializable key, String role, Serializable snapshot) {
586
	public void setSnapshot(Serializable key, String role, Serializable snapshot) {
542 587
		this.key = key;
543 588
		this.role = role;
544 589
		this.storedSnapshot = snapshot;
545 590
	}
546 591

  
547
	/**
548
	 * After flushing, clear any "queued" additions, since the
549
	 * database state is now synchronized with the memory state.
550
	 */
551 592
	@Override
552
    public void postAction() {
593
	public void postAction() {
553 594
		operationQueue = null;
554 595
		cachedSize = -1;
555 596
		clearDirty();
556 597
	}
557 598

  
558
	/**
559
	 * Not called by Hibernate, but used by non-JDK serialization,
560
	 * eg. SOAP libraries.
561
	 */
562
	public AbstractPersistentCollection() {
563
	}
564

  
565
	protected AbstractPersistentCollection(SessionImplementor session) {
566
		this.session = session;
567
	}
568

  
569
	/**
570
	 * return the user-visible collection (or array) instance
571
	 */
572 599
	@Override
573
    public Object getValue() {
600
	public Object getValue() {
574 601
		return this;
575 602
	}
576 603

  
577
	/**
578
	 * Called just before reading any rows from the JDBC result set
579
	 */
580 604
	@Override
581
    public void beginRead() {
605
	public void beginRead() {
582 606
		// override on some subclasses
583 607
		initializing = true;
584 608
	}
585 609

  
586
	/**
587
	 * Called after reading all rows from the JDBC result set
588
	 */
589 610
	@Override
590
    public boolean endRead() {
611
	public boolean endRead() {
591 612
		//override on some subclasses
592 613
		return afterInitialize();
593 614
	}
594 615

  
595 616
	@Override
596
    public boolean afterInitialize() {
617
	public boolean afterInitialize() {
597 618
		setInitialized();
598 619
		//do this bit after setting initialized to true or it will recurse
599 620
		if ( operationQueue != null ) {
......
663 684
		this.directlyAccessible = directlyAccessible;
664 685
	}
665 686

  
666
	/**
667
	 * Could the application possibly have a direct reference to
668
	 * the underlying collection implementation?
669
	 */
670 687
	@Override
671
    public boolean isDirectlyAccessible() {
688
	public boolean isDirectlyAccessible() {
672 689
		return directlyAccessible;
673 690
	}
674 691

  
......
780 797
		return session.getPersistenceContext().getSnapshot( this );
781 798
	}
782 799

  
783
	/**
784
	 * Is this instance initialized?
785
	 */
786 800
	@Override
787
    public final boolean wasInitialized() {
801
	public final boolean wasInitialized() {
788 802
		return initialized;
789 803
	}
790 804

  
791 805
	@Override
792
    public boolean isRowUpdatePossible() {
806
	public boolean isRowUpdatePossible() {
793 807
		return true;
794 808
	}
795 809

  
796
	/**
797
	 * Does this instance have any "queued" additions?
798
	 */
799 810
	@Override
800
    public final boolean hasQueuedOperations() {
811
	public final boolean hasQueuedOperations() {
801 812
		return operationQueue != null;
802 813
	}
803 814

  
804
	/**
805
	 * Iterate the "queued" additions
806
	 */
807 815
	@Override
808
    public final Iterator queuedAdditionIterator() {
816
	public final Iterator queuedAdditionIterator() {
809 817
		if ( hasQueuedOperations() ) {
810 818
			return new Iterator() {
811
				int i = 0;
819
				private int index;
812 820

  
813 821
				@Override
814
                public Object next() {
815
					return operationQueue.get( i++ ).getAddedInstance();
822
				public Object next() {
823
					return operationQueue.get( index++ ).getAddedInstance();
816 824
				}
817 825

  
818 826
				@Override
819
                public boolean hasNext() {
820
					return i < operationQueue.size();
827
				public boolean hasNext() {
828
					return index < operationQueue.size();
821 829
				}
822 830

  
823 831
				@Override
824
                public void remove() {
832
				public void remove() {
825 833
					throw new UnsupportedOperationException();
826 834
				}
827 835
			};
......
831 839
		}
832 840
	}
833 841

  
834
	/**
835
	 * Iterate the "queued" additions
836
	 */
837 842
	@Override
838
    @SuppressWarnings({"unchecked"})
843
	@SuppressWarnings({"unchecked"})
839 844
	public final Collection getQueuedOrphans(String entityName) {
840 845
		if ( hasQueuedOperations() ) {
841
			Collection additions = new ArrayList( operationQueue.size() );
842
			Collection removals = new ArrayList( operationQueue.size() );
846
			final Collection additions = new ArrayList( operationQueue.size() );
847
			final Collection removals = new ArrayList( operationQueue.size() );
843 848
			for ( DelayedOperation operation : operationQueue ) {
844 849
				additions.add( operation.getAddedInstance() );
845 850
				removals.add( operation.getOrphan() );
......
851 856
		}
852 857
	}
853 858

  
854
	/**
855
	 * Called before inserting rows, to ensure that any surrogate keys
856
	 * are fully generated
857
	 */
858 859
	@Override
859
    public void preInsert(CollectionPersister persister) throws HibernateException {
860
	public void preInsert(CollectionPersister persister) throws HibernateException {
860 861
	}
861 862

  
862
	/**
863
	 * Called after inserting a row, to fetch the natively generated id
864
	 */
865 863
	@Override
866
    public void afterRowInsert(CollectionPersister persister, Object entry, int i) throws HibernateException {
864
	public void afterRowInsert(CollectionPersister persister, Object entry, int i) throws HibernateException {
867 865
	}
868 866

  
869
	/**
870
	 * get all "orphaned" elements
871
	 */
872 867
	@Override
873
    public abstract Collection getOrphans(Serializable snapshot, String entityName) throws HibernateException;
868
	public abstract Collection getOrphans(Serializable snapshot, String entityName) throws HibernateException;
874 869

  
875 870
	/**
876
	 * Get the current session
871
	 * Get the session currently associated with this collection.
872
	 *
873
	 * @return The session
877 874
	 */
878
	@SuppressWarnings({"JavaDoc"})
879 875
	public final SessionImplementor getSession() {
880 876
		return session;
881 877
	}
......
888 884
		}
889 885

  
890 886
		@Override
891
        public boolean hasNext() {
887
		public boolean hasNext() {
892 888
			return itr.hasNext();
893 889
		}
894 890

  
895 891
		@Override
896
        public Object next() {
892
		public Object next() {
897 893
			return itr.next();
898 894
		}
899 895

  
900 896
		@Override
901
        public void remove() {
897
		public void remove() {
902 898
			write();
903 899
			itr.remove();
904 900
		}
905

  
906 901
	}
907

  
902
	
908 903
	protected final class ListIteratorProxy implements ListIterator {
909 904
		protected final ListIterator itr;
910 905

  
......
913 908
		}
914 909

  
915 910
		@Override
916
        @SuppressWarnings({"unchecked"})
911
		@SuppressWarnings({"unchecked"})
917 912
		public void add(Object o) {
918 913
			write();
919 914
			itr.add( o );
920 915
		}
921 916

  
922 917
		@Override
923
        public boolean hasNext() {
918
		public boolean hasNext() {
924 919
			return itr.hasNext();
925 920
		}
926 921

  
927 922
		@Override
928
        public boolean hasPrevious() {
923
		public boolean hasPrevious() {
929 924
			return itr.hasPrevious();
930 925
		}
931 926

  
932 927
		@Override
933
        public Object next() {
928
		public Object next() {
934 929
			return itr.next();
935 930
		}
936 931

  
937 932
		@Override
938
        public int nextIndex() {
933
		public int nextIndex() {
939 934
			return itr.nextIndex();
940 935
		}
941 936

  
942 937
		@Override
943
        public Object previous() {
938
		public Object previous() {
944 939
			return itr.previous();
945 940
		}
946 941

  
947 942
		@Override
948
        public int previousIndex() {
943
		public int previousIndex() {
949 944
			return itr.previousIndex();
950 945
		}
951 946

  
952 947
		@Override
953
        public void remove() {
948
		public void remove() {
954 949
			write();
955 950
			itr.remove();
956 951
		}
957 952

  
958 953
		@Override
959
        @SuppressWarnings({"unchecked"})
954
		@SuppressWarnings({"unchecked"})
960 955
		public void set(Object o) {
961 956
			write();
962 957
			itr.set( o );
963 958
		}
964

  
965 959
	}
966 960

  
967 961
	protected class SetProxy implements java.util.Set {
......
972 966
		}
973 967

  
974 968
		@Override
975
        @SuppressWarnings({"unchecked"})
969
		@SuppressWarnings({"unchecked"})
976 970
		public boolean add(Object o) {
977 971
			write();
978 972
			return set.add( o );
979 973
		}
980 974

  
981 975
		@Override
982
        @SuppressWarnings({"unchecked"})
976
		@SuppressWarnings({"unchecked"})
983 977
		public boolean addAll(Collection c) {
984 978
			write();
985 979
			return set.addAll( c );
986 980
		}
987 981

  
988 982
		@Override
989
        public void clear() {
983
		public void clear() {
990 984
			write();
991 985
			set.clear();
992 986
		}
993 987

  
994 988
		@Override
995
        public boolean contains(Object o) {
989
		public boolean contains(Object o) {
996 990
			return set.contains( o );
997 991
		}
998 992

  
999 993
		@Override
1000
        public boolean containsAll(Collection c) {
994
		@SuppressWarnings("unchecked")
995
		public boolean containsAll(Collection c) {
1001 996
			return set.containsAll( c );
1002 997
		}
1003 998

  
1004 999
		@Override
1005
        public boolean isEmpty() {
1000
		public boolean isEmpty() {
1006 1001
			return set.isEmpty();
1007 1002
		}
1008 1003

  
1009 1004
		@Override
1010
        public Iterator iterator() {
1005
		public Iterator iterator() {
1011 1006
			return new IteratorProxy( set.iterator() );
1012 1007
		}
1013 1008

  
1014 1009
		@Override
1015
        public boolean remove(Object o) {
1010
		public boolean remove(Object o) {
1016 1011
			write();
1017 1012
			return set.remove( o );
1018 1013
		}
1019 1014

  
1020 1015
		@Override
1021
        public boolean removeAll(Collection c) {
1016
		@SuppressWarnings("unchecked")
1017
		public boolean removeAll(Collection c) {
1022 1018
			write();
1023 1019
			return set.removeAll( c );
1024 1020
		}
1025 1021

  
1026 1022
		@Override
1027
        public boolean retainAll(Collection c) {
1023
		@SuppressWarnings("unchecked")
1024
		public boolean retainAll(Collection c) {
1028 1025
			write();
1029 1026
			return set.retainAll( c );
1030 1027
		}
1031 1028

  
1032 1029
		@Override
1033
        public int size() {
1030
		public int size() {
1034 1031
			return set.size();
1035 1032
		}
1036 1033

  
1037 1034
		@Override
1038
        public Object[] toArray() {
1035
		public Object[] toArray() {
1039 1036
			return set.toArray();
1040 1037
		}
1041 1038

  
1042 1039
		@Override
1043
        @SuppressWarnings({"unchecked"})
1040
		@SuppressWarnings({"unchecked"})
1044 1041
		public Object[] toArray(Object[] array) {
1045 1042
			return set.toArray( array );
1046 1043
		}
1047

  
1048 1044
	}
1049 1045

  
1050 1046
	protected final class ListProxy implements java.util.List {
......
1094 1090
		}
1095 1091

  
1096 1092
		@Override
1093
		@SuppressWarnings("unchecked")
1097 1094
		public boolean containsAll(Collection c) {
1098 1095
			return list.containsAll( c );
1099 1096
		}
......
1146 1143
		}
1147 1144

  
1148 1145
		@Override
1146
		@SuppressWarnings("unchecked")
1149 1147
		public boolean removeAll(Collection c) {
1150 1148
			write();
1151 1149
			return list.removeAll( c );
1152 1150
		}
1153 1151

  
1154 1152
		@Override
1153
		@SuppressWarnings("unchecked")
1155 1154
		public boolean retainAll(Collection c) {
1156 1155
			write();
1157 1156
			return list.retainAll( c );
......
1197 1196

  
1198 1197
		public Object getOrphan();
1199 1198
	}
1199
	
1200
	protected interface ValueDelayedOperation extends DelayedOperation {
1201
		void replace(CollectionPersister collectionPersister, Map copyCache);
1202
	}
1203
	
1204
	protected abstract class AbstractValueDelayedOperation implements ValueDelayedOperation {
1205
		private Object addedValue;
1206
		private Object orphan;
1207

  
1208
		protected AbstractValueDelayedOperation(Object addedValue, Object orphan) {
1209
			this.addedValue = addedValue;
1210
			this.orphan = orphan;
1211
		}
1212

  
1213
		public void replace(CollectionPersister persister, Map copyCache) {
1214
			if ( addedValue != null ) {
1215
				addedValue = getReplacement( persister.getElementType(), addedValue, copyCache );
1216
			}
1217
		}
1218

  
1219
		protected final Object getReplacement(Type type, Object current, Map copyCache) {
1220
			return type.replace( current, null, session, owner, copyCache );
1221
		}
1222

  
1223
		@Override
1224
		public final Object getAddedInstance() {
1225
			return addedValue;
1226
		}
1227

  
1228
		@Override
1229
		public final Object getOrphan() {
1230
			return orphan;
1231
		}
1232
	}
1200 1233

  
1201 1234
	/**
1202 1235
	 * Given a collection of entity instances that used to
......
1257 1290
		return res;
1258 1291
	}
1259 1292

  
1293
	private static boolean mayUseIdDirect(Type idType) {
1294
		return idType == StringType.INSTANCE
1295
			|| idType == IntegerType.INSTANCE
1296
			|| idType == LongType.INSTANCE
1297
			|| idType == UUIDBinaryType.INSTANCE
1298
			|| idType == UUIDCharType.INSTANCE
1299
			|| idType == PostgresUUIDType.INSTANCE;
1300
	}
1301

  
1260 1302
	public static void identityRemove(
1261 1303
			Collection list,
1262 1304
			Object object,
......
1281 1323
	}
1282 1324

  
1283 1325
	@Override
1284
    public Object getIdentifier(Object entry, int i) {
1326
	public Object getIdentifier(Object entry, int i) {
1285 1327
		throw new UnsupportedOperationException();
1286 1328
	}
1287 1329

  
1288 1330
	@Override
1289
    public Object getOwner() {
1331
	public Object getOwner() {
1290 1332
		return owner;
1291 1333
	}
1292 1334

  
1293 1335
	@Override
1294
    public void setOwner(Object owner) {
1336
	public void setOwner(Object owner) {
1295 1337
		this.owner = owner;
1296 1338
	}
1297 1339

  

Also available in: Unified diff