1 package org.hibernate.action; 3 4 import org.hibernate.AssertionFailure; 5 import org.hibernate.HibernateException; 6 import org.hibernate.cache.CacheException; 7 import org.hibernate.collection.PersistentCollection; 8 import org.hibernate.engine.SessionImplementor; 9 import org.hibernate.persister.collection.CollectionPersister; 10 import org.hibernate.pretty.MessageHelper; 11 12 import java.io.Serializable ; 13 14 public final class CollectionUpdateAction extends CollectionAction { 15 16 private final boolean emptySnapshot; 17 18 public CollectionUpdateAction( 19 final PersistentCollection collection, 20 final CollectionPersister persister, 21 final Serializable id, 22 final boolean emptySnapshot, 23 final SessionImplementor session) 24 throws CacheException { 25 super( persister, collection, id, session ); 26 this.emptySnapshot = emptySnapshot; 27 } 28 29 public void execute() throws HibernateException { 30 final Serializable id = getKey(); 31 final SessionImplementor session = getSession(); 32 final CollectionPersister persister = getPersister(); 33 final PersistentCollection collection = getCollection(); 34 boolean affectedByFilters = persister.isAffectedByEnabledFilters(session); 35 36 if ( !collection.wasInitialized() ) { 37 if ( !collection.hasQueuedAdditions() ) throw new AssertionFailure( "no queued adds" ); 38 } 40 else if ( !affectedByFilters && collection.empty() ) { 41 if ( !emptySnapshot ) persister.remove( id, session ); 42 } 43 else if ( collection.needsRecreate(persister) ) { 44 if (affectedByFilters) { 45 throw new HibernateException( 46 "cannot recreate collection while filter is enabled: " + 47 MessageHelper.collectionInfoString( persister, id, persister.getFactory() ) 48 ); 49 } 50 if ( !emptySnapshot ) persister.remove( id, session ); 51 persister.recreate( collection, id, session ); 52 } 53 else { 54 persister.deleteRows( collection, id, session ); 55 persister.updateRows( collection, id, session ); 56 persister.insertRows( collection, id, session ); 57 } 58 59 getSession().getPersistenceContext() 60 .getCollectionEntry(collection) 61 .afterAction(collection); 62 63 evict(); 64 65 if ( getSession().getFactory().getStatistics().isStatisticsEnabled() ) { 66 getSession().getFactory().getStatisticsImplementor(). 67 updateCollection( getPersister().getRole() ); 68 } 69 } 70 71 } 72 73 74 75 76 77 78 79 | Popular Tags |