Revision 3b868cb0
Added by Andreas Müller over 3 years ago
eu.etaxonomy.taxeditor.cdmlib/src/main/java/org/hibernate/collection/internal/AbstractPersistentCollection.java | ||
---|---|---|
39 | 39 |
import javax.naming.NamingException; |
40 | 40 |
|
41 | 41 |
import org.hibernate.AssertionFailure; |
42 |
import org.hibernate.FlushMode; |
|
42 | 43 |
import org.hibernate.HibernateException; |
43 | 44 |
import org.hibernate.LazyInitializationException; |
44 | 45 |
import org.hibernate.Session; |
45 | 46 |
import org.hibernate.collection.internal.AbstractPersistentCollection.DelayedOperation; |
46 | 47 |
import org.hibernate.collection.internal.AbstractPersistentCollection.IteratorProxy; |
48 |
import org.hibernate.collection.internal.AbstractPersistentCollection.LazyInitializationWork; |
|
47 | 49 |
import org.hibernate.collection.internal.AbstractPersistentCollection.ListIteratorProxy; |
48 | 50 |
import org.hibernate.collection.internal.AbstractPersistentCollection.ValueDelayedOperation; |
49 | 51 |
import org.hibernate.collection.spi.PersistentCollection; |
... | ... | |
63 | 65 |
import org.hibernate.persister.collection.CollectionPersister; |
64 | 66 |
import org.hibernate.persister.entity.EntityPersister; |
65 | 67 |
import org.hibernate.pretty.MessageHelper; |
68 |
import org.hibernate.type.CompositeType; |
|
66 | 69 |
import org.hibernate.type.IntegerType; |
67 | 70 |
import org.hibernate.type.LongType; |
68 | 71 |
import org.hibernate.type.PostgresUUIDType; |
... | ... | |
203 | 206 |
LOG.info("--> readSize, of " + getRole() + " with key " + getKey()); |
204 | 207 |
read(); |
205 | 208 |
} else { |
206 |
boolean isExtraLazy = withTemporarySessionIfNeeded( |
|
207 |
new LazyInitializationWork<Boolean>() { |
|
208 |
@Override |
|
209 |
public Boolean doWork() { |
|
210 |
CollectionEntry entry = session.getPersistenceContext().getCollectionEntry( AbstractPersistentCollection.this ); |
|
211 |
|
|
212 |
if ( entry != null ) { |
|
213 |
CollectionPersister persister = entry.getLoadedPersister(); |
|
214 |
if ( persister.isExtraLazy() ) { |
|
215 |
if ( hasQueuedOperations() ) { |
|
216 |
session.flush(); |
|
217 |
} |
|
218 |
cachedSize = persister.getSize( entry.getLoadedKey(), session ); |
|
219 |
return true; |
|
220 |
} |
|
221 |
else { |
|
222 |
read(); |
|
209 |
final boolean isExtraLazy = withTemporarySessionIfNeeded( |
|
210 |
new LazyInitializationWork<Boolean>() { |
|
211 |
@Override |
|
212 |
public Boolean doWork() { |
|
213 |
final CollectionEntry entry = session.getPersistenceContext().getCollectionEntry( AbstractPersistentCollection.this ); |
|
214 |
|
|
215 |
if ( entry != null ) { |
|
216 |
final CollectionPersister persister = entry.getLoadedPersister(); |
|
217 |
if ( persister.isExtraLazy() ) { |
|
218 |
if ( hasQueuedOperations() ) { |
|
219 |
session.flush(); |
|
223 | 220 |
} |
221 |
cachedSize = persister.getSize( entry.getLoadedKey(), session ); |
|
222 |
return true; |
|
224 | 223 |
} |
225 |
else{ |
|
226 |
throwLazyInitializationExceptionIfNotConnected();
|
|
224 |
else {
|
|
225 |
read();
|
|
227 | 226 |
} |
228 |
return false; |
|
229 | 227 |
} |
228 |
else{ |
|
229 |
throwLazyInitializationExceptionIfNotConnected(); |
|
230 |
} |
|
231 |
return false; |
|
230 | 232 |
} |
231 |
); |
|
232 |
if ( isExtraLazy ) { |
|
233 |
return true; |
|
234 |
} |
|
233 |
} |
|
234 |
); |
|
235 |
if ( isExtraLazy ) { |
|
236 |
return true; |
|
237 |
} |
|
235 | 238 |
} |
236 | 239 |
} |
237 | 240 |
} |
... | ... | |
295 | 298 |
.getTransactionFactory() |
296 | 299 |
.compatibleWithJtaSynchronization();*/ |
297 | 300 |
isJTA = session.getTransactionCoordinator().getTransactionCoordinatorBuilder().isJta(); |
301 |
|
|
298 | 302 |
if ( !isJTA ) { |
299 | 303 |
// Explicitly handle the transactions only if we're not in |
300 | 304 |
// a JTA environment. A lazy loading temporary session can |
301 | 305 |
// be created even if a current session and transaction are |
302 | 306 |
// open (ex: session.clear() was used). We must prevent |
303 | 307 |
// multiple transactions. |
304 |
( ( Session) session ).beginTransaction();
|
|
308 |
( (Session) session ).beginTransaction(); |
|
305 | 309 |
} |
306 | 310 |
|
307 | 311 |
session.getPersistenceContext().addUninitializedDetachedCollection( |
... | ... | |
770 | 774 |
return false; |
771 | 775 |
} |
772 | 776 |
|
773 |
/** |
|
774 |
* To be called internally by the session, forcing |
|
775 |
* immediate initialization. |
|
776 |
*/ |
|
777 | 777 |
@Override |
778 |
public final void forceInitialization() throws HibernateException {
|
|
778 |
public final void forceInitialization() throws HibernateException {
|
|
779 | 779 |
if ( !initialized ) { |
780 | 780 |
if ( initializing ) { |
781 | 781 |
throw new AssertionFailure( "force initialize loading collection" ); |
... | ... | |
901 | 901 |
itr.remove(); |
902 | 902 |
} |
903 | 903 |
} |
904 |
|
|
904 |
|
|
905 | 905 |
protected final class ListIteratorProxy implements ListIterator { |
906 | 906 |
protected final ListIterator itr; |
907 | 907 |
|
... | ... | |
1198 | 1198 |
|
1199 | 1199 |
public Object getOrphan(); |
1200 | 1200 |
} |
1201 |
|
|
1201 |
|
|
1202 | 1202 |
protected interface ValueDelayedOperation extends DelayedOperation { |
1203 | 1203 |
void replace(CollectionPersister collectionPersister, Map copyCache); |
1204 | 1204 |
} |
1205 |
|
|
1205 |
|
|
1206 | 1206 |
protected abstract class AbstractValueDelayedOperation implements ValueDelayedOperation { |
1207 | 1207 |
private Object addedValue; |
1208 | 1208 |
private Object orphan; |
... | ... | |
1247 | 1247 |
|
1248 | 1248 |
// short-circuit(s) |
1249 | 1249 |
if ( currentElements.size() == 0 ) { |
1250 |
return oldElements; // no new elements, the old list contains only Orphans |
|
1250 |
// no new elements, the old list contains only Orphans |
|
1251 |
return oldElements; |
|
1251 | 1252 |
} |
1252 | 1253 |
if ( oldElements.size() == 0 ) { |
1253 |
return oldElements; // no old elements, so no Orphans neither |
|
1254 |
// no old elements, so no Orphans neither |
|
1255 |
return oldElements; |
|
1254 | 1256 |
} |
1255 | 1257 |
|
1256 | 1258 |
final EntityPersister entityPersister = session.getFactory().getEntityPersister( entityName ); |
1257 | 1259 |
final Type idType = entityPersister.getIdentifierType(); |
1258 | 1260 |
|
1259 | 1261 |
// create the collection holding the Orphans |
1260 |
Collection res = new ArrayList(); |
|
1262 |
final Collection res = new ArrayList();
|
|
1261 | 1263 |
|
1262 | 1264 |
// collect EntityIdentifier(s) of the *current* elements - add them into a HashSet for fast access |
1263 |
java.util.Set currentIds = new HashSet(); |
|
1264 |
java.util.Set currentSaving = new IdentitySet(); |
|
1265 |
final java.util.Set currentIds = new HashSet();
|
|
1266 |
final java.util.Set currentSaving = new IdentitySet();
|
|
1265 | 1267 |
for ( Object current : currentElements ) { |
1266 | 1268 |
if ( current != null && ForeignKeys.isNotTransient( entityName, current, null, session ) ) { |
1267 |
EntityEntry ee = session.getPersistenceContext().getEntry( current ); |
|
1269 |
final EntityEntry ee = session.getPersistenceContext().getEntry( current );
|
|
1268 | 1270 |
if ( ee != null && ee.getStatus() == Status.SAVING ) { |
1269 | 1271 |
currentSaving.add( current ); |
1270 | 1272 |
} |
1271 | 1273 |
else { |
1272 |
Serializable currentId = ForeignKeys.getEntityIdentifierIfNotUnsaved( |
|
1274 |
final Serializable currentId = ForeignKeys.getEntityIdentifierIfNotUnsaved(
|
|
1273 | 1275 |
entityName, |
1274 | 1276 |
current, |
1275 | 1277 |
session |
... | ... | |
1301 | 1303 |
|| idType == PostgresUUIDType.INSTANCE; |
1302 | 1304 |
} |
1303 | 1305 |
|
1306 |
/** |
|
1307 |
* Removes entity entries that have an equal identifier with the incoming entity instance |
|
1308 |
* |
|
1309 |
* @param list The list containing the entity instances |
|
1310 |
* @param entityInstance The entity instance to match elements. |
|
1311 |
* @param entityName The entity name |
|
1312 |
* @param session The session |
|
1313 |
*/ |
|
1304 | 1314 |
public static void identityRemove( |
1305 | 1315 |
Collection list, |
1306 |
Object object,
|
|
1316 |
Object entityInstance,
|
|
1307 | 1317 |
String entityName, |
1308 |
SessionImplementor session) throws HibernateException {
|
|
1318 |
SessionImplementor session) { |
|
1309 | 1319 |
|
1310 |
if ( object != null && ForeignKeys.isNotTransient( entityName, object, null, session ) ) {
|
|
1320 |
if ( entityInstance != null && ForeignKeys.isNotTransient( entityName, entityInstance, null, session ) ) {
|
|
1311 | 1321 |
final EntityPersister entityPersister = session.getFactory().getEntityPersister( entityName ); |
1312 |
Type idType = entityPersister.getIdentifierType(); |
|
1322 |
final Type idType = entityPersister.getIdentifierType();
|
|
1313 | 1323 |
|
1314 |
Serializable idOfCurrent = ForeignKeys.getEntityIdentifierIfNotUnsaved( entityName, object, session );
|
|
1315 |
Iterator itr = list.iterator(); |
|
1324 |
final Serializable idOfCurrent = ForeignKeys.getEntityIdentifierIfNotUnsaved( entityName, entityInstance, session );
|
|
1325 |
final Iterator itr = list.iterator();
|
|
1316 | 1326 |
while ( itr.hasNext() ) { |
1317 |
Serializable idOfOld = ForeignKeys.getEntityIdentifierIfNotUnsaved( entityName, itr.next(), session ); |
|
1327 |
final Serializable idOfOld = ForeignKeys.getEntityIdentifierIfNotUnsaved( entityName, itr.next(), session );
|
|
1318 | 1328 |
if ( idType.isEqual( idOfCurrent, idOfOld, session.getFactory() ) ) { |
1319 | 1329 |
itr.remove(); |
1320 | 1330 |
break; |
Also available in: Unified diff
ref #9204 further uncritical updates to AbstractPersistentCollection