1 package org.hibernate.impl; 3 4 import java.io.Serializable ; 5 import java.sql.Connection ; 6 import java.util.Collections ; 7 import java.util.HashSet ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 import java.util.Map ; 11 import java.util.Set ; 12 13 import org.hibernate.CacheMode; 14 import org.hibernate.ConnectionReleaseMode; 15 import org.hibernate.Criteria; 16 import org.hibernate.EmptyInterceptor; 17 import org.hibernate.EntityMode; 18 import org.hibernate.FlushMode; 19 import org.hibernate.HibernateException; 20 import org.hibernate.Interceptor; 21 import org.hibernate.LockMode; 22 import org.hibernate.MappingException; 23 import org.hibernate.Query; 24 import org.hibernate.QueryException; 25 import org.hibernate.SQLQuery; 26 import org.hibernate.ScrollMode; 27 import org.hibernate.ScrollableResults; 28 import org.hibernate.SessionException; 29 import org.hibernate.StatelessSession; 30 import org.hibernate.Transaction; 31 import org.hibernate.collection.PersistentCollection; 32 import org.hibernate.engine.EntityKey; 33 import org.hibernate.engine.PersistenceContext; 34 import org.hibernate.engine.QueryParameters; 35 import org.hibernate.engine.SessionFactoryImplementor; 36 import org.hibernate.engine.StatefulPersistenceContext; 37 import org.hibernate.engine.Versioning; 38 import org.hibernate.event.SessionEventListenerConfig; 39 import org.hibernate.hql.QueryTranslator; 40 import org.hibernate.id.IdentifierGeneratorFactory; 41 import org.hibernate.jdbc.Batcher; 42 import org.hibernate.jdbc.JDBCContext; 43 import org.hibernate.loader.criteria.CriteriaLoader; 44 import org.hibernate.loader.custom.CustomLoader; 45 import org.hibernate.loader.custom.CustomQuery; 46 import org.hibernate.persister.entity.EntityPersister; 47 import org.hibernate.persister.entity.OuterJoinLoadable; 48 import org.hibernate.proxy.HibernateProxy; 49 import org.hibernate.type.Type; 50 import org.hibernate.util.CollectionHelper; 51 52 55 public class StatelessSessionImpl extends AbstractSessionImpl 56 implements JDBCContext.Context, StatelessSession { 57 58 private JDBCContext jdbcContext; 59 private boolean closed; 60 61 StatelessSessionImpl( 62 Connection connection, 63 SessionFactoryImpl factory 64 ) { 65 super(factory); 66 this.jdbcContext = new JDBCContext( this, connection ); 67 } 68 69 public void delete(Object entity) { 70 delete(null, entity); 71 } 72 73 public Serializable insert(Object entity) { 74 return insert(null, entity); 75 } 76 77 public void update(Object entity) { 78 update(null, entity); 79 } 80 81 public Serializable insert(String entityName, Object entity) { 82 EntityPersister persister = getEntityPersister(entityName, entity); 83 Serializable id = persister.getIdentifierGenerator().generate(this, entity); 84 Object [] state = persister.getPropertyValues(entity, EntityMode.POJO); 85 if ( id == IdentifierGeneratorFactory.POST_INSERT_INDICATOR ) { 86 id = persister.insert(state, entity, this); 87 } 88 else { 89 persister.insert(id, state, entity, this); 90 } 91 return id; 92 } 93 94 public void update(String entityName, Object entity) { 95 EntityPersister persister = getEntityPersister(entityName, entity); 96 Serializable id = persister.getIdentifier(entity, EntityMode.POJO); 97 Object [] state = persister.getPropertyValues(entity, EntityMode.POJO); 98 Object oldVersion; 99 if ( persister.isVersioned() ) { 100 oldVersion = persister.getVersion(entity, EntityMode.POJO); 101 Object newVersion = Versioning.increment( oldVersion, persister.getVersionType() ); 102 Versioning.setVersion(state, newVersion, persister); 103 persister.setPropertyValues(entity, state, EntityMode.POJO); 104 } 105 else { 106 oldVersion = null; 107 } 108 persister.update(id, state, null, false, null, oldVersion, entity, null, this); 109 } 110 111 public void delete(String entityName, Object entity) { 112 EntityPersister persister = getEntityPersister(entityName, entity); 113 Serializable id = persister.getIdentifier(entity, EntityMode.POJO); 114 Object version = persister.getVersion(entity, EntityMode.POJO); 115 persister.delete(id, version, entity, this); 116 } 117 118 public void close() { 119 managedClose(); 120 } 121 122 public ConnectionReleaseMode getConnectionReleaseMode() { 123 return factory.getSettings().getConnectionReleaseMode(); 124 } 125 126 public boolean isAutoCloseSessionEnabled() { 127 return factory.getSettings().isAutoCloseSessionEnabled(); 128 } 129 130 public boolean isFlushBeforeCompletionEnabled() { 131 return true; 132 } 133 134 public boolean isFlushModeNever() { 135 return false; 136 } 137 138 public void managedClose() { 139 jdbcContext.getConnectionManager().close(); 140 closed = true; 141 } 142 143 public void managedFlush() { 144 getBatcher().executeBatch(); 145 } 146 147 public boolean shouldAutoClose() { 148 return isAutoCloseSessionEnabled() && isOpen(); 149 } 150 151 public void afterTransactionCompletion(boolean successful, Transaction tx) {} 152 153 public void beforeTransactionCompletion(Transaction tx) {} 154 155 public String bestGuessEntityName(Object object) { 156 if (object instanceof HibernateProxy) { 157 object = ( (HibernateProxy) object ).getHibernateLazyInitializer().getImplementation(); 158 } 159 return guessEntityName(object); 160 } 161 162 public Connection connection() { 163 return jdbcContext.connection(); 164 } 165 166 public int executeUpdate(String query, QueryParameters queryParameters) 167 throws HibernateException { 168 return 0; 170 } 171 172 public Batcher getBatcher() { 173 return jdbcContext.getConnectionManager() 174 .getBatcher(); 175 } 176 177 public CacheMode getCacheMode() { 178 return CacheMode.IGNORE; 179 } 180 181 public int getDontFlushFromFind() { 182 return 0; 183 } 184 185 public Map getEnabledFilters() { 186 return CollectionHelper.EMPTY_MAP; 187 } 188 189 public Serializable getContextEntityIdentifier(Object object) { 190 return null; 191 } 192 193 public EntityMode getEntityMode() { 194 return EntityMode.POJO; 195 } 196 197 public EntityPersister getEntityPersister(String entityName, Object object) 198 throws HibernateException { 199 if (entityName==null) { 200 return factory.getEntityPersister( guessEntityName(object) ); 201 } 202 else { 203 return factory.getEntityPersister( entityName ) 204 .getSubclassEntityPersister( object, getFactory(), EntityMode.POJO ); 205 } 206 } 207 208 public Object getEntityUsingInterceptor(EntityKey key) throws HibernateException { 209 return null; 210 } 211 212 public SessionFactoryImplementor getFactory() { 213 return factory; 214 } 215 216 public Type getFilterParameterType(String filterParameterName) { 217 throw new UnsupportedOperationException (); 218 } 219 220 public Object getFilterParameterValue(String filterParameterName) { 221 throw new UnsupportedOperationException (); 222 } 223 224 public FlushMode getFlushMode() { 225 return FlushMode.COMMIT; 226 } 227 228 public Interceptor getInterceptor() { 229 return EmptyInterceptor.INSTANCE; 230 } 231 232 public SessionEventListenerConfig getListeners() { 233 throw new UnsupportedOperationException (); 234 } 235 236 public PersistenceContext getPersistenceContext() { 237 return temporaryPersistenceContext; 238 } 239 240 public long getTimestamp() { 241 throw new UnsupportedOperationException (); 242 } 243 244 private PersistenceContext temporaryPersistenceContext = new StatefulPersistenceContext(this); 245 246 public Object get(Class entityClass, Serializable id) { 247 return get( entityClass.getName(), id ); 248 } 249 250 public Object get(Class entityClass, Serializable id, LockMode lockMode) { 251 return get( entityClass.getName(), id, lockMode ); 252 } 253 254 public Object get(String entityName, Serializable id) { 255 return get(entityName, id, LockMode.NONE); 256 } 257 258 public Object get(String entityName, Serializable id, LockMode lockMode) { 259 Object result = getFactory().getEntityPersister(entityName) 260 .load(id, null, lockMode, this); 261 temporaryPersistenceContext.clear(); 262 return result; 263 } 264 265 public String guessEntityName(Object entity) throws HibernateException { 266 return entity.getClass().getName(); 267 } 268 269 public Object immediateLoad(String entityName, Serializable id) throws HibernateException { 270 throw new SessionException("proxies cannot be fetched by a stateless session"); 271 } 272 273 public void initializeCollection(PersistentCollection collection, boolean writing) 274 throws HibernateException { 275 throw new SessionException("collections cannot be fetched by a stateless session"); 276 } 277 278 public Object instantiate(String entityName, Serializable id) throws HibernateException { 279 return getFactory().getEntityPersister(entityName).instantiate(id, EntityMode.POJO); 280 } 281 282 public Object internalLoad(String entityName, Serializable id, boolean eager, boolean nullable) 283 throws HibernateException { 284 EntityPersister persister = getFactory().getEntityPersister(entityName); 285 if ( !eager && persister.hasProxy() ) { 286 return persister.createProxy(id, this); 287 } 288 Object loaded = temporaryPersistenceContext.getEntity( new EntityKey(id, persister, EntityMode.POJO) ); 289 return loaded==null ? get(entityName, id) : loaded; 291 } 292 293 public boolean isConnected() { 294 return jdbcContext.getConnectionManager().isLogicallyConnected(); 295 } 296 297 public boolean isOpen() { 298 return !closed; 299 } 300 301 public boolean isTransactionInProgress() { 302 return jdbcContext.isTransactionInProgress(); 303 } 304 305 public Iterator iterate(String query, QueryParameters queryParameters) throws HibernateException { 306 throw new UnsupportedOperationException (); 307 } 308 309 public Iterator iterateFilter(Object collection, String filter, QueryParameters queryParameters) 310 throws HibernateException { 311 throw new UnsupportedOperationException (); 312 } 313 314 public List listFilter(Object collection, String filter, QueryParameters queryParameters) 315 throws HibernateException { 316 throw new UnsupportedOperationException (); 317 } 318 319 public void setAutoClear(boolean enabled) { 320 throw new UnsupportedOperationException (); 321 } 322 323 public void setCacheMode(CacheMode cm) { 324 throw new UnsupportedOperationException (); 325 } 326 327 public void setFlushMode(FlushMode fm) { 328 throw new UnsupportedOperationException (); 329 } 330 331 public Transaction beginTransaction() { 332 return jdbcContext.beginTransaction(); 333 } 334 335 public boolean isEventSource() { 336 return false; 337 } 338 339 341 343 public Query createQuery(String queryString) { 344 QueryImpl query = new QueryImpl(queryString, this); 345 query.setComment(queryString); 346 return query; 347 } 348 349 public List list(String query, QueryParameters queryParameters) throws HibernateException { 350 351 queryParameters.validateParameters(); 352 QueryTranslator[] q = getQueries(query, false); 353 354 List results = CollectionHelper.EMPTY_LIST; 355 356 boolean success = false; 358 try { 359 for ( int i = 0; i < q.length; i++ ) { 360 List currentResults = q[i].list(this, queryParameters); 361 currentResults.addAll(results); 362 results = currentResults; 363 } 364 success = true; 365 } 366 finally { 367 afterOperation(success); 368 } 369 temporaryPersistenceContext.clear(); 370 return results; 371 } 372 373 public void afterOperation(boolean success) { 374 if ( !jdbcContext.isTransactionInProgress() ) { 375 jdbcContext.afterNontransactionalQuery(success); 376 } 377 } 378 379 private QueryTranslator[] getQueries(String query, boolean scalar) throws HibernateException { 380 381 QueryTranslator[] q = factory.getQuery( query, scalar, getEnabledFilters() ); 383 return prepareQueries(q); 384 385 } 386 387 private QueryTranslator[] prepareQueries(QueryTranslator[] q) { 388 HashSet qs = new HashSet (); 389 for ( int i = 0; i < q.length; i++ ) { 390 qs.addAll( q[i].getQuerySpaces() ); 391 } 392 return q; 393 } 394 395 public Criteria createCriteria(Class persistentClass, String alias) { 396 return new CriteriaImpl( persistentClass.getName(), alias, this ); 397 } 398 399 public Criteria createCriteria(String entityName, String alias) { 400 return new CriteriaImpl(entityName, alias, this); 401 } 402 403 public Criteria createCriteria(Class persistentClass) { 404 return new CriteriaImpl( persistentClass.getName(), this ); 405 } 406 407 public Criteria createCriteria(String entityName) { 408 return new CriteriaImpl(entityName, this); 409 } 410 411 public ScrollableResults scroll(CriteriaImpl criteria, ScrollMode scrollMode) { 412 String entityName = criteria.getEntityOrClassName(); 413 CriteriaLoader loader = new CriteriaLoader( 414 getOuterJoinLoadable(entityName), 415 factory, 416 criteria, 417 entityName, 418 getEnabledFilters() 419 ); 420 return loader.scroll(this, scrollMode); 421 } 422 423 public List list(CriteriaImpl criteria) throws HibernateException { 424 425 String [] implementors = factory.getImplementors( criteria.getEntityOrClassName() ); 426 int size = implementors.length; 427 428 CriteriaLoader[] loaders = new CriteriaLoader[size]; 429 Set spaces = new HashSet (); 430 for( int i=0; i <size; i++ ) { 431 432 loaders[i] = new CriteriaLoader( 433 getOuterJoinLoadable( implementors[i] ), 434 factory, 435 criteria, 436 implementors[i], 437 getEnabledFilters() 438 ); 439 440 spaces.addAll( loaders[i].getQuerySpaces() ); 441 442 } 443 444 445 List results = Collections.EMPTY_LIST; 446 boolean success = false; 447 try { 448 for( int i=0; i<size; i++ ) { 449 final List currentResults = loaders[i].list(this); 450 currentResults.addAll(results); 451 results = currentResults; 452 } 453 success = true; 454 } 455 finally { 456 afterOperation(success); 457 } 458 temporaryPersistenceContext.clear(); 459 return results; 460 } 461 462 private OuterJoinLoadable getOuterJoinLoadable(String entityName) throws MappingException { 463 EntityPersister persister = factory.getEntityPersister(entityName); 464 if ( !(persister instanceof OuterJoinLoadable) ) { 465 throw new MappingException( "class persister is not OuterJoinLoadable: " + entityName ); 466 } 467 return ( OuterJoinLoadable ) persister; 468 } 469 470 public SQLQuery createSQLQuery(String sql) { 471 SQLQueryImpl query = new SQLQueryImpl(sql, this); 472 query.setComment("dynamic native SQL query"); 473 return query; 474 } 475 476 public List listCustomQuery(CustomQuery customQuery, QueryParameters queryParameters) 477 throws HibernateException { 478 479 CustomLoader loader = new CustomLoader( customQuery, getFactory() ); 480 481 boolean success = false; 482 List results; 483 try { 484 results = loader.list(this, queryParameters); 485 success = true; 486 } 487 finally { 488 afterOperation(success); 489 } 490 temporaryPersistenceContext.clear(); 491 return results; 492 } 493 494 public ScrollableResults scrollCustomQuery(CustomQuery customQuery, QueryParameters queryParameters) 495 throws HibernateException { 496 CustomLoader loader = new CustomLoader( customQuery, getFactory() ); 497 return loader.scroll(queryParameters, this); 498 } 499 500 public ScrollableResults scroll(String query, QueryParameters queryParameters) throws HibernateException { 501 QueryTranslator[] q = factory.getQuery( query, false, getEnabledFilters() ); 502 if ( q.length != 1 ) throw new QueryException( "implicit polymorphism not supported for scroll() queries" ); 503 return q[0].scroll(queryParameters, this); 504 } 505 506 public void afterScrollOperation() { 507 temporaryPersistenceContext.clear(); 508 } 509 } 510 | Popular Tags |