4 * Hibernate, Relational Persistence for Idiomatic Java
6 * Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as
7 * indicated by the @author tags or express copyright attribution
8 * statements applied by the authors. All third-party contributions are
9 * distributed under license by Red Hat Inc.
11 * This copyrighted material is made available to anyone wishing to use, modify,
12 * copy, or redistribute it subject to the terms and conditions of the GNU
13 * Lesser General Public License, as published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this distribution; if not, write to:
22 * Free Software Foundation, Inc.
23 * 51 Franklin Street, Fifth Floor
24 * Boston, MA 02110-1301 USA
26 package org
.hibernate
.collection
.internal
;
28 import java
.io
.Serializable
;
29 import java
.lang
.reflect
.Field
;
30 import java
.util
.ArrayList
;
31 import java
.util
.Collection
;
32 import java
.util
.Collections
;
33 import java
.util
.HashMap
;
34 import java
.util
.HashSet
;
35 import java
.util
.Iterator
;
36 import java
.util
.List
;
37 import java
.util
.ListIterator
;
40 import java
.util
.TreeMap
;
41 import java
.util
.TreeSet
;
43 import javax
.naming
.NamingException
;
45 import org
.hibernate
.AssertionFailure
;
46 import org
.hibernate
.HibernateException
;
47 import org
.hibernate
.LazyInitializationException
;
48 import org
.hibernate
.Session
;
49 import org
.hibernate
.collection
.spi
.PersistentCollection
;
50 import org
.hibernate
.engine
.internal
.ForeignKeys
;
51 import org
.hibernate
.engine
.spi
.CollectionEntry
;
52 import org
.hibernate
.engine
.spi
.EntityEntry
;
53 import org
.hibernate
.engine
.spi
.SessionFactoryImplementor
;
54 import org
.hibernate
.engine
.spi
.SessionImplementor
;
55 import org
.hibernate
.engine
.spi
.Status
;
56 import org
.hibernate
.engine
.spi
.TypedValue
;
57 import org
.hibernate
.internal
.SessionFactoryRegistry
;
58 import org
.hibernate
.internal
.util
.MarkerObject
;
59 import org
.hibernate
.internal
.util
.collections
.EmptyIterator
;
60 import org
.hibernate
.internal
.util
.collections
.IdentitySet
;
61 import org
.hibernate
.persister
.collection
.CollectionPersister
;
62 import org
.hibernate
.persister
.entity
.EntityPersister
;
63 import org
.hibernate
.pretty
.MessageHelper
;
64 import org
.hibernate
.type
.Type
;
65 import org
.jboss
.logging
.Logger
;
67 import eu
.etaxonomy
.cdm
.api
.application
.ICdmApplicationConfiguration
;
68 import eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
69 import eu
.etaxonomy
.cdm
.model
.common
.PersistentMultiLanguageText
;
72 * Base class implementing {@link org.hibernate.collection.spi.PersistentCollection}
76 public abstract class AbstractPersistentCollection
implements Serializable
, PersistentCollection
{
77 private static final Logger log
= Logger
.getLogger( AbstractPersistentCollection
.class );
79 private static final long serialVersionUID
= -7238232378593030571L;
81 private transient SessionImplementor session
;
82 private boolean initialized
;
83 private transient List
<DelayedOperation
> operationQueue
;
84 private transient boolean directlyAccessible
;
85 private transient boolean initializing
;
87 private int cachedSize
= -1;
90 private Serializable key
;
91 // collections detect changes made via their public interface and mark
92 // themselves as dirty as a performance optimization
93 private boolean dirty
;
94 private Serializable storedSnapshot
;
96 private String sessionFactoryUuid
;
97 private boolean specjLazyLoad
= false;
99 public final String
getRole() {
103 public final Serializable
getKey() {
107 public final boolean isUnreferenced() {
111 public final boolean isDirty() {
115 public final void clearDirty() {
119 public final void dirty() {
123 public final Serializable
getStoredSnapshot() {
124 return storedSnapshot
;
127 //Careful: these methods do not initialize the collection.
130 * Is the initialized collection empty?
132 public abstract boolean empty();
135 * Called by any read-only method of the collection interface
137 protected final void read() {
142 * Called by the {@link Collection#size} method
144 @SuppressWarnings({"JavaDoc"})
145 protected boolean readSize() {
146 if ( !initialized
) {
147 if ( cachedSize
!= -1 && !hasQueuedOperations() ) {
151 boolean isExtraLazy
= withTemporarySessionIfNeeded(
152 new LazyInitializationWork
<Boolean
>() {
154 public Boolean
doWork() {
155 CollectionEntry entry
= session
.getPersistenceContext().getCollectionEntry( AbstractPersistentCollection
.this );
157 if ( entry
!= null ) {
158 CollectionPersister persister
= entry
.getLoadedPersister();
159 if ( persister
.isExtraLazy() ) {
160 if ( hasQueuedOperations() ) {
163 cachedSize
= persister
.getSize( entry
.getLoadedKey(), session
);
171 throwLazyInitializationExceptionIfNotConnected();
185 public static interface LazyInitializationWork
<T
> {
189 private <T
> T
withTemporarySessionIfNeeded(LazyInitializationWork
<T
> lazyInitializationWork
) {
190 SessionImplementor originalSession
= null;
191 boolean isTempSession
= false;
192 boolean isJTA
= false;
194 if ( session
== null ) {
195 if ( specjLazyLoad
) {
196 session
= openTemporarySessionForLoading();
197 isTempSession
= true;
200 throwLazyInitializationException( "could not initialize proxy - no Session" );
203 else if ( !session
.isOpen() ) {
204 if ( specjLazyLoad
) {
205 originalSession
= session
;
206 session
= openTemporarySessionForLoading();
207 isTempSession
= true;
210 throwLazyInitializationException( "could not initialize proxy - the owning Session was closed" );
213 else if ( !session
.isConnected() ) {
214 if ( specjLazyLoad
) {
215 originalSession
= session
;
216 session
= openTemporarySessionForLoading();
217 isTempSession
= true;
220 throwLazyInitializationException( "could not initialize proxy - the owning Session is disconnected" );
224 if ( isTempSession
) {
225 // TODO: On the next major release, add an
226 // 'isJTA' or 'getTransactionFactory' method to Session.
227 isJTA
= session
.getTransactionCoordinator()
228 .getTransactionContext().getTransactionEnvironment()
229 .getTransactionFactory()
230 .compatibleWithJtaSynchronization();
233 // Explicitly handle the transactions only if we're not in
234 // a JTA environment. A lazy loading temporary session can
235 // be created even if a current session and transaction are
236 // open (ex: session.clear() was used). We must prevent
237 // multiple transactions.
238 ( ( Session
) session
).beginTransaction();
241 session
.getPersistenceContext().addUninitializedDetachedCollection(
242 session
.getFactory().getCollectionPersister( getRole() ),
248 return lazyInitializationWork
.doWork();
251 if ( isTempSession
) {
252 // make sure the just opened temp session gets closed!
255 ( ( Session
) session
).getTransaction().commit();
257 ( (Session
) session
).close();
259 catch (Exception e
) {
260 log
.warn( "Unable to close temporary session used to load lazy collection associated to no session" );
262 session
= originalSession
;
267 private SessionImplementor
openTemporarySessionForLoading() {
268 if ( sessionFactoryUuid
== null ) {
269 throwLazyInitializationException( "SessionFactory UUID not known to create temporary Session for loading" );
272 SessionFactoryImplementor sf
= (SessionFactoryImplementor
)
273 SessionFactoryRegistry
.INSTANCE
.getSessionFactory( sessionFactoryUuid
);
274 return (SessionImplementor
) sf
.openSession();
277 protected Boolean
readIndexExistence(final Object index
) {
278 if ( !initialized
) {
279 Boolean extraLazyExistenceCheck
= withTemporarySessionIfNeeded(
280 new LazyInitializationWork
<Boolean
>() {
282 public Boolean
doWork() {
283 CollectionEntry entry
= session
.getPersistenceContext().getCollectionEntry( AbstractPersistentCollection
.this );
284 CollectionPersister persister
= entry
.getLoadedPersister();
285 if ( persister
.isExtraLazy() ) {
286 if ( hasQueuedOperations() ) {
289 return persister
.indexExists( entry
.getLoadedKey(), index
, session
);
298 if ( extraLazyExistenceCheck
!= null ) {
299 return extraLazyExistenceCheck
;
305 protected Boolean
readElementExistence(final Object element
) {
306 if ( !initialized
) {
307 Boolean extraLazyExistenceCheck
= withTemporarySessionIfNeeded(
308 new LazyInitializationWork
<Boolean
>() {
310 public Boolean
doWork() {
311 CollectionEntry entry
= session
.getPersistenceContext().getCollectionEntry( AbstractPersistentCollection
.this );
312 CollectionPersister persister
= entry
.getLoadedPersister();
313 if ( persister
.isExtraLazy() ) {
314 if ( hasQueuedOperations() ) {
317 return persister
.elementExists( entry
.getLoadedKey(), element
, session
);
326 if ( extraLazyExistenceCheck
!= null ) {
327 return extraLazyExistenceCheck
;
333 protected static final Object UNKNOWN
= new MarkerObject( "UNKNOWN" );
335 protected Object
readElementByIndex(final Object index
) {
336 if ( !initialized
) {
337 class ExtraLazyElementByIndexReader
implements LazyInitializationWork
{
338 private boolean isExtraLazy
;
339 private Object element
;
342 public Object
doWork() {
343 CollectionEntry entry
= session
.getPersistenceContext().getCollectionEntry( AbstractPersistentCollection
.this );
344 CollectionPersister persister
= entry
.getLoadedPersister();
345 isExtraLazy
= persister
.isExtraLazy();
347 if ( hasQueuedOperations() ) {
350 element
= persister
.getElementByIndex( entry
.getLoadedKey(), index
, session
, owner
);
359 ExtraLazyElementByIndexReader reader
= new ExtraLazyElementByIndexReader();
360 //noinspection unchecked
361 withTemporarySessionIfNeeded( reader
);
362 if ( reader
.isExtraLazy
) {
363 return reader
.element
;
370 protected int getCachedSize() {
374 private boolean isConnectedToSession() {
375 return session
!= null &&
377 session
.getPersistenceContext().containsCollection( this );
381 * Called by any writer method of the collection interface
383 protected final void write() {
389 * Is this collection in a state that would allow us to
390 * "queue" operations?
392 @SuppressWarnings({"JavaDoc"})
393 protected boolean isOperationQueueEnabled() {
394 return !initialized
&&
395 isConnectedToSession() &&
396 isInverseCollection();
400 * Is this collection in a state that would allow us to
401 * "queue" puts? This is a special case, because of orphan
404 @SuppressWarnings({"JavaDoc"})
405 protected boolean isPutQueueEnabled() {
406 return !initialized
&&
407 isConnectedToSession() &&
408 isInverseOneToManyOrNoOrphanDelete();
412 * Is this collection in a state that would allow us to
413 * "queue" clear? This is a special case, because of orphan
416 @SuppressWarnings({"JavaDoc"})
417 protected boolean isClearQueueEnabled() {
418 return !initialized
&&
419 isConnectedToSession() &&
420 isInverseCollectionNoOrphanDelete();
424 * Is this the "inverse" end of a bidirectional association?
426 @SuppressWarnings({"JavaDoc"})
427 private boolean isInverseCollection() {
428 CollectionEntry ce
= session
.getPersistenceContext().getCollectionEntry( this );
429 return ce
!= null && ce
.getLoadedPersister().isInverse();
433 * Is this the "inverse" end of a bidirectional association with
434 * no orphan delete enabled?
436 @SuppressWarnings({"JavaDoc"})
437 private boolean isInverseCollectionNoOrphanDelete() {
438 CollectionEntry ce
= session
.getPersistenceContext().getCollectionEntry( this );
440 ce
.getLoadedPersister().isInverse() &&
441 !ce
.getLoadedPersister().hasOrphanDelete();
445 * Is this the "inverse" end of a bidirectional one-to-many, or
446 * of a collection with no orphan delete?
448 @SuppressWarnings({"JavaDoc"})
449 private boolean isInverseOneToManyOrNoOrphanDelete() {
450 CollectionEntry ce
= session
.getPersistenceContext().getCollectionEntry( this );
451 return ce
!= null && ce
.getLoadedPersister().isInverse() && (
452 ce
.getLoadedPersister().isOneToMany() ||
453 !ce
.getLoadedPersister().hasOrphanDelete()
460 @SuppressWarnings({"JavaDoc"})
461 protected final void queueOperation(DelayedOperation operation
) {
462 if ( operationQueue
== null ) {
463 operationQueue
= new ArrayList
<DelayedOperation
>( 10 );
465 operationQueue
.add( operation
);
466 dirty
= true; //needed so that we remove this collection from the second-level cache
470 * After reading all existing elements from the database,
471 * add the queued elements to the underlying collection.
473 protected final void performQueuedOperations() {
474 for ( DelayedOperation operation
: operationQueue
) {
480 * After flushing, re-init snapshot state.
482 public void setSnapshot(Serializable key
, String role
, Serializable snapshot
) {
485 this.storedSnapshot
= snapshot
;
489 * After flushing, clear any "queued" additions, since the
490 * database state is now synchronized with the memory state.
492 public void postAction() {
493 operationQueue
= null;
499 * Not called by Hibernate, but used by non-JDK serialization,
500 * eg. SOAP libraries.
502 public AbstractPersistentCollection() {
505 protected AbstractPersistentCollection(SessionImplementor session
) {
506 this.session
= session
;
510 * return the user-visible collection (or array) instance
512 public Object
getValue() {
517 * Called just before reading any rows from the JDBC result set
519 public void beginRead() {
520 // override on some subclasses
525 * Called after reading all rows from the JDBC result set
527 public boolean endRead() {
528 //override on some subclasses
529 return afterInitialize();
532 public boolean afterInitialize() {
534 //do this bit after setting initialized to true or it will recurse
535 if ( operationQueue
!= null ) {
536 performQueuedOperations();
537 operationQueue
= null;
547 * Initialize the collection, if possible, wrapping any exceptions
548 * in a runtime exception
550 * @param writing currently obsolete
552 * @throws LazyInitializationException if we cannot initialize
554 protected final void initialize(final boolean writing
) {
555 // FIXME:Remoting is it true that session is null => we are in remoting mode
556 // if(session == null) {
557 // remoteInitialize();
565 withTemporarySessionIfNeeded(
566 new LazyInitializationWork
<Object
>() {
568 public Object
doWork() {
569 session
.initializeCollection( AbstractPersistentCollection
.this, writing
);
576 private void throwLazyInitializationExceptionIfNotConnected() {
577 if ( !isConnectedToSession() ) {
578 throwLazyInitializationException( "no session or session was closed" );
580 if ( !session
.isConnected() ) {
581 throwLazyInitializationException( "session is disconnected" );
585 private void throwLazyInitializationException(String message
) {
586 throw new LazyInitializationException(
587 "failed to lazily initialize a collection" +
588 (role
== null ?
"" : " of role: " + role
) +
593 protected final void setInitialized() {
594 this.initializing
= false;
595 this.initialized
= true;
598 protected final void setDirectlyAccessible(boolean directlyAccessible
) {
599 this.directlyAccessible
= directlyAccessible
;
603 * Could the application possibly have a direct reference to
604 * the underlying collection implementation?
606 public boolean isDirectlyAccessible() {
607 return directlyAccessible
;
611 * Disassociate this collection from the given session.
613 * @return true if this was currently associated with the given session
615 public final boolean unsetSession(SessionImplementor currentSession
) {
616 prepareForPossibleSpecialSpecjInitialization();
617 if ( currentSession
== this.session
) {
626 protected void prepareForPossibleSpecialSpecjInitialization() {
627 if ( session
!= null ) {
628 specjLazyLoad
= session
.getFactory().getSettings().isInitializeLazyStateOutsideTransactionsEnabled();
630 if ( specjLazyLoad
&& sessionFactoryUuid
== null ) {
632 sessionFactoryUuid
= (String
) session
.getFactory().getReference().get( "uuid" ).getContent();
634 catch (NamingException e
) {
635 //not much we can do if this fails...
643 * Associate the collection with the given session.
645 * @return false if the collection was already associated with the session
647 * @throws HibernateException if the collection was already associated
648 * with another open session
650 public final boolean setCurrentSession(SessionImplementor session
) throws HibernateException
{
651 if ( session
== this.session
) {
655 if ( isConnectedToSession() ) {
656 CollectionEntry ce
= session
.getPersistenceContext().getCollectionEntry( this );
658 throw new HibernateException(
659 "Illegal attempt to associate a collection with two open sessions"
663 throw new HibernateException(
664 "Illegal attempt to associate a collection with two open sessions: " +
665 MessageHelper
.collectionInfoString(
666 ce
.getLoadedPersister(), this,
667 ce
.getLoadedKey(), session
673 this.session
= session
;
680 * Do we need to completely recreate this collection when it changes?
682 public boolean needsRecreate(CollectionPersister persister
) {
687 * To be called internally by the session, forcing
688 * immediate initialization.
690 public final void forceInitialization() throws HibernateException
{
691 if ( !initialized
) {
692 if ( initializing
) {
693 throw new AssertionFailure( "force initialize loading collection" );
695 if ( session
== null ) {
696 throw new HibernateException( "collection is not associated with any session" );
698 if ( !session
.isConnected() ) {
699 throw new HibernateException( "disconnected session" );
701 session
.initializeCollection( this, false );
707 * Get the current snapshot from the session
709 @SuppressWarnings({"JavaDoc"})
710 protected final Serializable
getSnapshot() {
711 return session
.getPersistenceContext().getSnapshot( this );
715 * Is this instance initialized?
717 public final boolean wasInitialized() {
721 public boolean isRowUpdatePossible() {
726 * Does this instance have any "queued" additions?
728 public final boolean hasQueuedOperations() {
729 return operationQueue
!= null;
733 * Iterate the "queued" additions
735 public final Iterator
queuedAdditionIterator() {
736 if ( hasQueuedOperations() ) {
737 return new Iterator() {
740 public Object
next() {
741 return operationQueue
.get( i
++ ).getAddedInstance();
744 public boolean hasNext() {
745 return i
< operationQueue
.size();
748 public void remove() {
749 throw new UnsupportedOperationException();
754 return EmptyIterator
.INSTANCE
;
759 * Iterate the "queued" additions
761 @SuppressWarnings({"unchecked"})
762 public final Collection
getQueuedOrphans(String entityName
) {
763 if ( hasQueuedOperations() ) {
764 Collection additions
= new ArrayList( operationQueue
.size() );
765 Collection removals
= new ArrayList( operationQueue
.size() );
766 for ( DelayedOperation operation
: operationQueue
) {
767 additions
.add( operation
.getAddedInstance() );
768 removals
.add( operation
.getOrphan() );
770 return getOrphans( removals
, additions
, entityName
, session
);
773 return Collections
.EMPTY_LIST
;
778 * Called before inserting rows, to ensure that any surrogate keys
779 * are fully generated
781 public void preInsert(CollectionPersister persister
) throws HibernateException
{
785 * Called after inserting a row, to fetch the natively generated id
787 public void afterRowInsert(CollectionPersister persister
, Object entry
, int i
) throws HibernateException
{
791 * get all "orphaned" elements
793 public abstract Collection
getOrphans(Serializable snapshot
, String entityName
) throws HibernateException
;
796 * Get the current session
798 @SuppressWarnings({"JavaDoc"})
799 public final SessionImplementor
getSession() {
803 protected final class IteratorProxy
implements Iterator
{
804 protected final Iterator itr
;
806 public IteratorProxy(Iterator itr
) {
810 public boolean hasNext() {
811 return itr
.hasNext();
814 public Object
next() {
818 public void remove() {
825 protected final class ListIteratorProxy
implements ListIterator
{
826 protected final ListIterator itr
;
828 public ListIteratorProxy(ListIterator itr
) {
832 @SuppressWarnings({"unchecked"})
833 public void add(Object o
) {
838 public boolean hasNext() {
839 return itr
.hasNext();
842 public boolean hasPrevious() {
843 return itr
.hasPrevious();
846 public Object
next() {
850 public int nextIndex() {
851 return itr
.nextIndex();
854 public Object
previous() {
855 return itr
.previous();
858 public int previousIndex() {
859 return itr
.previousIndex();
862 public void remove() {
867 @SuppressWarnings({"unchecked"})
868 public void set(Object o
) {
875 protected class SetProxy
implements java
.util
.Set
{
876 protected final Collection set
;
878 public SetProxy(Collection set
) {
882 @SuppressWarnings({"unchecked"})
883 public boolean add(Object o
) {
888 @SuppressWarnings({"unchecked"})
889 public boolean addAll(Collection c
) {
891 return set
.addAll( c
);
894 public void clear() {
899 public boolean contains(Object o
) {
900 return set
.contains( o
);
903 public boolean containsAll(Collection c
) {
904 return set
.containsAll( c
);
907 public boolean isEmpty() {
908 return set
.isEmpty();
911 public Iterator
iterator() {
912 return new IteratorProxy( set
.iterator() );
915 public boolean remove(Object o
) {
917 return set
.remove( o
);
920 public boolean removeAll(Collection c
) {
922 return set
.removeAll( c
);
925 public boolean retainAll(Collection c
) {
927 return set
.retainAll( c
);
934 public Object
[] toArray() {
935 return set
.toArray();
938 @SuppressWarnings({"unchecked"})
939 public Object
[] toArray(Object
[] array
) {
940 return set
.toArray( array
);
945 protected final class ListProxy
implements java
.util
.List
{
946 protected final List list
;
948 public ListProxy(List list
) {
953 @SuppressWarnings({"unchecked"})
954 public void add(int index
, Object value
) {
956 list
.add( index
, value
);
960 @SuppressWarnings({"unchecked"})
961 public boolean add(Object o
) {
963 return list
.add( o
);
967 @SuppressWarnings({"unchecked"})
968 public boolean addAll(Collection c
) {
970 return list
.addAll( c
);
974 @SuppressWarnings({"unchecked"})
975 public boolean addAll(int i
, Collection c
) {
977 return list
.addAll( i
, c
);
981 public void clear() {
987 public boolean contains(Object o
) {
988 return list
.contains( o
);
992 public boolean containsAll(Collection c
) {
993 return list
.containsAll( c
);
997 public Object
get(int i
) {
998 return list
.get( i
);
1002 public int indexOf(Object o
) {
1003 return list
.indexOf( o
);
1007 public boolean isEmpty() {
1008 return list
.isEmpty();
1012 public Iterator
iterator() {
1013 return new IteratorProxy( list
.iterator() );
1017 public int lastIndexOf(Object o
) {
1018 return list
.lastIndexOf( o
);
1022 public ListIterator
listIterator() {
1023 return new ListIteratorProxy( list
.listIterator() );
1027 public ListIterator
listIterator(int i
) {
1028 return new ListIteratorProxy( list
.listIterator( i
) );
1032 public Object
remove(int i
) {
1034 return list
.remove( i
);
1038 public boolean remove(Object o
) {
1040 return list
.remove( o
);
1044 public boolean removeAll(Collection c
) {
1046 return list
.removeAll( c
);
1050 public boolean retainAll(Collection c
) {
1052 return list
.retainAll( c
);
1056 @SuppressWarnings({"unchecked"})
1057 public Object
set(int i
, Object o
) {
1059 return list
.set( i
, o
);
1068 public List
subList(int i
, int j
) {
1069 return list
.subList( i
, j
);
1073 public Object
[] toArray() {
1074 return list
.toArray();
1078 @SuppressWarnings({"unchecked"})
1079 public Object
[] toArray(Object
[] array
) {
1080 return list
.toArray( array
);
1086 * Contract for operations which are part of a collection's operation queue.
1088 protected interface DelayedOperation
{
1089 public void operate();
1091 public Object
getAddedInstance();
1093 public Object
getOrphan();
1097 * Given a collection of entity instances that used to
1098 * belong to the collection, and a collection of instances
1099 * that currently belong, return a collection of orphans
1101 @SuppressWarnings({"JavaDoc", "unchecked"})
1102 protected static Collection
getOrphans(
1103 Collection oldElements
,
1104 Collection currentElements
,
1106 SessionImplementor session
) throws HibernateException
{
1109 if ( currentElements
.size() == 0 ) {
1110 return oldElements
; // no new elements, the old list contains only Orphans
1112 if ( oldElements
.size() == 0 ) {
1113 return oldElements
; // no old elements, so no Orphans neither
1116 final EntityPersister entityPersister
= session
.getFactory().getEntityPersister( entityName
);
1117 final Type idType
= entityPersister
.getIdentifierType();
1119 // create the collection holding the Orphans
1120 Collection res
= new ArrayList();
1122 // collect EntityIdentifier(s) of the *current* elements - add them into a HashSet for fast access
1123 java
.util
.Set currentIds
= new HashSet();
1124 java
.util
.Set currentSaving
= new IdentitySet();
1125 for ( Object current
: currentElements
) {
1126 if ( current
!= null && ForeignKeys
.isNotTransient( entityName
, current
, null, session
) ) {
1127 EntityEntry ee
= session
.getPersistenceContext().getEntry( current
);
1128 if ( ee
!= null && ee
.getStatus() == Status
.SAVING
) {
1129 currentSaving
.add( current
);
1132 Serializable currentId
= ForeignKeys
.getEntityIdentifierIfNotUnsaved(
1137 currentIds
.add( new TypedValue( idType
, currentId
, entityPersister
.getEntityMode() ) );
1142 // iterate over the *old* list
1143 for ( Object old
: oldElements
) {
1144 if ( !currentSaving
.contains( old
) ) {
1145 Serializable oldId
= ForeignKeys
.getEntityIdentifierIfNotUnsaved( entityName
, old
, session
);
1146 if ( !currentIds
.contains( new TypedValue( idType
, oldId
, entityPersister
.getEntityMode() ) ) ) {
1155 public static void identityRemove(
1159 SessionImplementor session
) throws HibernateException
{
1161 if ( object
!= null && ForeignKeys
.isNotTransient( entityName
, object
, null, session
) ) {
1162 final EntityPersister entityPersister
= session
.getFactory().getEntityPersister( entityName
);
1163 Type idType
= entityPersister
.getIdentifierType();
1165 Serializable idOfCurrent
= ForeignKeys
.getEntityIdentifierIfNotUnsaved( entityName
, object
, session
);
1166 Iterator itr
= list
.iterator();
1167 while ( itr
.hasNext() ) {
1168 Serializable idOfOld
= ForeignKeys
.getEntityIdentifierIfNotUnsaved( entityName
, itr
.next(), session
);
1169 if ( idType
.isEqual( idOfCurrent
, idOfOld
, session
.getFactory() ) ) {
1178 public Object
getIdentifier(Object entry
, int i
) {
1179 throw new UnsupportedOperationException();
1182 public Object
getOwner() {
1186 public void setOwner(Object owner
) {
1190 /** Below is section of code which makes remote service calls */
1192 private static ICdmApplicationConfiguration configuration
;
1195 public static void setConfiguration(ICdmApplicationConfiguration conf
) {
1196 configuration
= conf
;
1199 private void remoteInitialize() {
1201 if (getOwner() != null && !initialized
) {
1204 String role
= getRole();
1205 String fieldName
= role
.substring(role
.lastIndexOf(".") + 1);
1206 log
.info("--> Remote Lazy Initializing " + getRole() + " , field : " + fieldName
);
1207 Object owner
= getOwner();
1209 if(configuration
== null) {
1210 throw new HibernateException("CdmApplicationRemoteConfiguration not initialized (null)");
1212 ICommonService commonService
= configuration
.getCommonService();
1213 if(commonService
== null) {
1214 throw new HibernateException("commonService not initialized (null)");
1217 PersistentCollection col
= commonService
.initializeCollection(this);
1220 Class
<?
> clazz
= getClass();
1221 if (clazz
!= null) {
1222 CollectionField cf
= getCollectionField(col
);
1223 Field field
= clazz
.getDeclaredField(cf
.getFieldName());
1224 field
.setAccessible(true);
1225 field
.set(this, cf
.getCollection());
1227 } catch (Exception ex
) {
1228 log
.warn(ex
.getMessage());
1233 private CollectionField
getCollectionField(PersistentCollection pc
) {
1235 if(pc
instanceof PersistentSet
) {
1236 return new CollectionField(new HashSet((Set
)pc
), "set");
1238 if(pc
instanceof PersistentSortedSet
) {
1239 return new CollectionField(new TreeSet((Set
)pc
), "set");
1241 if(pc
instanceof PersistentList
) {
1242 return new CollectionField(new ArrayList((List
)pc
), "list");
1244 if(pc
instanceof PersistentMap
|| pc
instanceof PersistentMultiLanguageText
) {
1245 return new CollectionField(new HashMap((Map
)pc
), "map");
1247 if(pc
instanceof PersistentSortedMap
) {
1248 return new CollectionField(new TreeMap((Map
)pc
), "map");
1254 private String
getCollectionFieldName(PersistentCollection pc
) {
1256 if(pc
instanceof PersistentSet
|| pc
instanceof PersistentSortedSet
) {
1259 if(pc
instanceof PersistentList
) {
1262 if(pc
instanceof PersistentMap
|| pc
instanceof PersistentMultiLanguageText
) {
1269 private class CollectionField
{
1271 private String fieldName
;
1272 public CollectionField(Object col
, String fieldName
) {
1274 this.fieldName
= fieldName
;
1277 public Object
getCollection() {
1281 public String
getFieldName() {
1282 return this.fieldName
;