1 package org.hibernate.action; 3 4 import org.hibernate.cache.CacheConcurrencyStrategy.SoftLock; 5 import org.hibernate.cache.CacheException; 6 import org.hibernate.cache.CacheKey; 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 import org.hibernate.util.StringHelper; 12 13 import java.io.IOException ; 14 import java.io.ObjectInputStream ; 15 import java.io.Serializable ; 16 17 21 public abstract class CollectionAction implements Executable, Serializable , Comparable { 22 23 private transient CollectionPersister persister; 24 private final Serializable key; 25 private final SessionImplementor session; 26 private SoftLock lock; 27 private final String collectionRole; 28 private final PersistentCollection collection; 29 30 public CollectionAction( 31 final CollectionPersister persister, 32 final PersistentCollection collection, 33 final Serializable key, 34 final SessionImplementor session) 35 throws CacheException { 36 this.persister = persister; 37 this.session = session; 38 this.key = key; 39 this.collectionRole = persister.getRole(); 40 this.collection = collection; 41 } 42 43 protected PersistentCollection getCollection() { 44 return collection; 45 } 46 47 private void readObject(ObjectInputStream ois) throws IOException , ClassNotFoundException { 48 ois.defaultReadObject(); 49 persister = session.getFactory().getCollectionPersister( collectionRole ); 50 } 51 52 public void afterTransactionCompletion(boolean success) throws CacheException { 53 if ( persister.hasCache() ) { 54 final CacheKey ck = new CacheKey( 55 key, 56 persister.getKeyType(), 57 persister.getRole(), 58 session.getEntityMode(), 59 session.getFactory() 60 ); 61 persister.getCache().release(ck, lock); 62 } 63 } 64 65 public boolean hasAfterTransactionCompletion() { 66 return persister.hasCache(); 67 } 68 69 public Serializable [] getPropertySpaces() { 70 return persister.getCollectionSpaces(); 71 } 72 73 protected final CollectionPersister getPersister() { 74 return persister; 75 } 76 77 protected final Serializable getKey() { 78 return key; 79 } 80 81 protected final SessionImplementor getSession() { 82 return session; 83 } 84 85 public final void beforeExecutions() throws CacheException { 86 if ( persister.hasCache() ) { 93 final CacheKey ck = new CacheKey( 94 key, 95 persister.getKeyType(), 96 persister.getRole(), 97 session.getEntityMode(), 98 session.getFactory() 99 ); 100 lock = persister.getCache().lock(ck, null); 101 } 102 } 103 104 protected final void evict() throws CacheException { 105 if ( persister.hasCache() ) { 106 CacheKey ck = new CacheKey( 107 key, 108 persister.getKeyType(), 109 persister.getRole(), 110 session.getEntityMode(), 111 session.getFactory() 112 ); 113 persister.getCache().evict(ck); 114 } 115 } 116 117 public String toString() { 118 return StringHelper.unqualify( getClass().getName() ) + 119 MessageHelper.infoString( collectionRole, key ); 120 } 121 122 public int compareTo(Object other) { 123 CollectionAction action = ( CollectionAction ) other; 124 int roleComparison = collectionRole.compareTo( action.collectionRole ); 126 if ( roleComparison != 0 ) { 127 return roleComparison; 128 } 129 else { 130 return persister.getKeyType() 132 .compare( key, action.key, session.getEntityMode() ); 133 } 134 } 135 } 136 137 138 139 140 141 142 | Popular Tags |