| 1 16 17 package org.springframework.orm.hibernate3; 18 19 import java.io.Serializable ; 20 import java.lang.reflect.InvocationHandler ; 21 import java.lang.reflect.InvocationTargetException ; 22 import java.lang.reflect.Method ; 23 import java.lang.reflect.Proxy ; 24 import java.sql.SQLException ; 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 import org.hibernate.Criteria; 30 import org.hibernate.Filter; 31 import org.hibernate.FlushMode; 32 import org.hibernate.Hibernate; 33 import org.hibernate.HibernateException; 34 import org.hibernate.LockMode; 35 import org.hibernate.Query; 36 import org.hibernate.ReplicationMode; 37 import org.hibernate.Session; 38 import org.hibernate.SessionFactory; 39 import org.hibernate.criterion.DetachedCriteria; 40 import org.hibernate.criterion.Example; 41 import org.hibernate.engine.SessionImplementor; 42 43 import org.springframework.dao.DataAccessException; 44 import org.springframework.dao.InvalidDataAccessApiUsageException; 45 import org.springframework.util.Assert; 46 47 115 public class HibernateTemplate extends HibernateAccessor implements HibernateOperations { 116 117 private boolean allowCreate = true; 118 119 private boolean alwaysUseNewSession = false; 120 121 private boolean exposeNativeSession = false; 122 123 private boolean checkWriteOperations = true; 124 125 private boolean cacheQueries = false; 126 127 private String queryCacheRegion; 128 129 private int fetchSize = 0; 130 131 private int maxResults = 0; 132 133 134 137 public HibernateTemplate() { 138 } 139 140 144 public HibernateTemplate(SessionFactory sessionFactory) { 145 setSessionFactory(sessionFactory); 146 afterPropertiesSet(); 147 } 148 149 155 public HibernateTemplate(SessionFactory sessionFactory, boolean allowCreate) { 156 setSessionFactory(sessionFactory); 157 setAllowCreate(allowCreate); 158 afterPropertiesSet(); 159 } 160 161 162 172 public void setAllowCreate(boolean allowCreate) { 173 this.allowCreate = allowCreate; 174 } 175 176 179 public boolean isAllowCreate() { 180 return this.allowCreate; 181 } 182 183 195 public void setAlwaysUseNewSession(boolean alwaysUseNewSession) { 196 this.alwaysUseNewSession = alwaysUseNewSession; 197 } 198 199 202 public boolean isAlwaysUseNewSession() { 203 return this.alwaysUseNewSession; 204 } 205 206 219 public void setExposeNativeSession(boolean exposeNativeSession) { 220 this.exposeNativeSession = exposeNativeSession; 221 } 222 223 227 public boolean isExposeNativeSession() { 228 return this.exposeNativeSession; 229 } 230 231 241 public void setCheckWriteOperations(boolean checkWriteOperations) { 242 this.checkWriteOperations = checkWriteOperations; 243 } 244 245 249 public boolean isCheckWriteOperations() { 250 return this.checkWriteOperations; 251 } 252 253 264 public void setCacheQueries(boolean cacheQueries) { 265 this.cacheQueries = cacheQueries; 266 } 267 268 271 public boolean isCacheQueries() { 272 return this.cacheQueries; 273 } 274 275 285 public void setQueryCacheRegion(String queryCacheRegion) { 286 this.queryCacheRegion = queryCacheRegion; 287 } 288 289 292 public String getQueryCacheRegion() { 293 return this.queryCacheRegion; 294 } 295 296 303 public void setFetchSize(int fetchSize) { 304 this.fetchSize = fetchSize; 305 } 306 307 310 public int getFetchSize() { 311 return this.fetchSize; 312 } 313 314 322 public void setMaxResults(int maxResults) { 323 this.maxResults = maxResults; 324 } 325 326 329 public int getMaxResults() { 330 return this.maxResults; 331 } 332 333 334 public Object execute(HibernateCallback action) throws DataAccessException { 335 return execute(action, isExposeNativeSession()); 336 } 337 338 public List executeFind(HibernateCallback action) throws DataAccessException { 339 Object result = execute(action, isExposeNativeSession()); 340 if (result != null && !(result instanceof List )) { 341 throw new InvalidDataAccessApiUsageException( 342 "Result object returned from HibernateCallback isn't a List: [" + result + "]"); 343 } 344 return (List ) result; 345 } 346 347 355 public Object execute(HibernateCallback action, boolean exposeNativeSession) throws DataAccessException { 356 Assert.notNull(action, "Callback object must not be null"); 357 358 Session session = getSession(); 359 boolean existingTransaction = SessionFactoryUtils.isSessionTransactional(session, getSessionFactory()); 360 if (existingTransaction) { 361 logger.debug("Found thread-bound Session for HibernateTemplate"); 362 } 363 364 FlushMode previousFlushMode = null; 365 try { 366 previousFlushMode = applyFlushMode(session, existingTransaction); 367 enableFilters(session); 368 Session sessionToExpose = (exposeNativeSession ? session : createSessionProxy(session)); 369 Object result = action.doInHibernate(sessionToExpose); 370 flushIfNecessary(session, existingTransaction); 371 return result; 372 } 373 catch (HibernateException ex) { 374 throw convertHibernateAccessException(ex); 375 } 376 catch (SQLException ex) { 377 throw convertJdbcAccessException(ex); 378 } 379 catch (RuntimeException ex) { 380 throw ex; 382 } 383 finally { 384 if (existingTransaction) { 385 logger.debug("Not closing pre-bound Hibernate Session after HibernateTemplate"); 386 disableFilters(session); 387 if (previousFlushMode != null) { 388 session.setFlushMode(previousFlushMode); 389 } 390 } 391 else { 392 if (isAlwaysUseNewSession()) { 394 SessionFactoryUtils.closeSession(session); 395 } 396 else { 397 SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, getSessionFactory()); 398 } 399 } 400 } 401 } 402 403 414 protected Session getSession() { 415 if (isAlwaysUseNewSession()) { 416 return SessionFactoryUtils.getNewSession(getSessionFactory(), getEntityInterceptor()); 417 } 418 else if (!isAllowCreate()) { 419 return SessionFactoryUtils.getSession(getSessionFactory(), false); 420 } 421 else { 422 return SessionFactoryUtils.getSession( 423 getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator()); 424 } 425 } 426 427 436 protected Session createSessionProxy(Session session) { 437 Class [] sessionIfcs = null; 438 if (session instanceof SessionImplementor) { 439 sessionIfcs = new Class [] {Session.class, SessionImplementor.class}; 440 } 441 else { 442 sessionIfcs = new Class [] {Session.class}; 443 } 444 return (Session) Proxy.newProxyInstance( 445 getClass().getClassLoader(), sessionIfcs, 446 new CloseSuppressingInvocationHandler(session)); 447 } 448 449 450 454 public Object get(Class entityClass, Serializable id) throws DataAccessException { 455 return get(entityClass, id, null); 456 } 457 458 public Object get(final Class entityClass, final Serializable id, final LockMode lockMode) 459 throws DataAccessException { 460 461 return execute(new HibernateCallback() { 462 public Object doInHibernate(Session session) throws HibernateException { 463 if (lockMode != null) { 464 return session.get(entityClass, id, lockMode); 465 } 466 else { 467 return session.get(entityClass, id); 468 } 469 } 470 }, true); 471 } 472 473 public Object get(String entityName, Serializable id) throws DataAccessException { 474 return get(entityName, id, null); 475 } 476 477 public Object get(final String entityName, final Serializable id, final LockMode lockMode) 478 throws DataAccessException { 479 480 return execute(new HibernateCallback() { 481 public Object doInHibernate(Session session) throws HibernateException { 482 if (lockMode != null) { 483 return session.get(entityName, id, lockMode); 484 } 485 else { 486 return session.get(entityName, id); 487 } 488 } 489 }, true); 490 } 491 492 public Object load(Class entityClass, Serializable id) throws DataAccessException { 493 return load(entityClass, id, null); 494 } 495 496 public Object load(final Class entityClass, final Serializable id, final LockMode lockMode) 497 throws DataAccessException { 498 499 return execute(new HibernateCallback() { 500 public Object doInHibernate(Session session) throws HibernateException { 501 if (lockMode != null) { 502 return session.load(entityClass, id, lockMode); 503 } 504 else { 505 return session.load(entityClass, id); 506 } 507 } 508 }, true); 509 } 510 511 public Object load(String entityName, Serializable id) throws DataAccessException { 512 return load(entityName, id, null); 513 } 514 515 public Object load(final String entityName, final Serializable id, final LockMode lockMode) 516 throws DataAccessException { 517 518 return execute(new HibernateCallback() { 519 public Object doInHibernate(Session session) throws HibernateException { 520 if (lockMode != null) { 521 return session.load(entityName, id, lockMode); 522 } 523 else { 524 return session.load(entityName, id); 525 } 526 } 527 }, true); 528 } 529 530 public List loadAll(final Class entityClass) throws DataAccessException { 531 return (List ) execute(new HibernateCallback() { 532 public Object doInHibernate(Session session) throws HibernateException { 533 Criteria criteria = session.createCriteria(entityClass); 534 prepareCriteria(criteria); 535 return criteria.list(); 536 } 537 }, true); 538 } 539 540 public void load(final Object entity, final Serializable id) throws DataAccessException { 541 execute(new HibernateCallback() { 542 public Object doInHibernate(Session session) throws HibernateException { 543 session.load(entity, id); 544 return null; 545 } 546 }, true); 547 } 548 549 public void refresh(final Object entity) throws DataAccessException { 550 refresh(entity, null); 551 } 552 553 public void refresh(final Object entity, final LockMode lockMode) throws DataAccessException { 554 execute(new HibernateCallback() { 555 public Object doInHibernate(Session session) throws HibernateException { 556 if (lockMode != null) { 557 session.refresh(entity, lockMode); 558 } 559 else { 560 session.refresh(entity); 561 } 562 return null; 563 } 564 }, true); 565 } 566 567 public boolean contains(final Object entity) throws DataAccessException { 568 Boolean result = (Boolean ) execute(new HibernateCallback() { 569 public Object doInHibernate(Session session) { 570 return (session.contains(entity) ? Boolean.TRUE : Boolean.FALSE); 571 } 572 }, true); 573 return result.booleanValue(); 574 } 575 576 public void evict(final Object entity) throws DataAccessException { 577 execute(new HibernateCallback() { 578 public Object doInHibernate(Session session) throws HibernateException { 579 session.evict(entity); 580 return null; 581 } 582 }, true); 583 } 584 585 public void initialize(Object proxy) throws DataAccessException { 586 try { 587 Hibernate.initialize(proxy); 588 } 589 catch (HibernateException ex) { 590 throw SessionFactoryUtils.convertHibernateAccessException(ex); 591 } 592 } 593 594 public Filter enableFilter(String filterName) throws IllegalStateException { 595 Session session = SessionFactoryUtils.getSession(getSessionFactory(), false); 596 Filter filter = session.getEnabledFilter(filterName); 597 if (filter == null) { 598 filter = session.enableFilter(filterName); 599 } 600 return filter; 601 } 602 603 604 608 public void lock(final Object entity, final LockMode lockMode) throws DataAccessException { 609 execute(new HibernateCallback() { 610 public Object doInHibernate(Session session) throws HibernateException { 611 session.lock(entity, lockMode); 612 return null; 613 } 614 }, true); 615 } 616 617 public void lock(final String entityName, final Object entity, final LockMode lockMode) 618 throws DataAccessException { 619 620 execute(new HibernateCallback() { 621 public Object doInHibernate(Session session) throws HibernateException { 622 session.lock(entityName, entity, lockMode); 623 return null; 624 } 625 }, true); 626 } 627 628 public Serializable save(final Object entity) throws DataAccessException { 629 return (Serializable ) execute(new HibernateCallback() { 630 public Object doInHibernate(Session session) throws HibernateException { 631 checkWriteOperationAllowed(session); 632 return session.save(entity); 633 } 634 }, true); 635 } 636 637 public Serializable save(final String entityName, final Object entity) throws DataAccessException { 638 return (Serializable ) execute(new HibernateCallback() { 639 public Object doInHibernate(Session session) throws HibernateException { 640 checkWriteOperationAllowed(session); 641 return session.save(entityName, entity); 642 } 643 }, true); 644 } 645 646 public void update(Object entity) throws DataAccessException { 647 update(entity, null); 648 } 649 650 public void update(final Object entity, final LockMode lockMode) throws DataAccessException { 651 execute(new HibernateCallback() { 652 public Object doInHibernate(Session session) throws HibernateException { 653 checkWriteOperationAllowed(session); 654 session.update(entity); 655 if (lockMode != null) { 656 session.lock(entity, lockMode); 657 } 658 return null; 659 } 660 }, true); 661 } 662 663 public void update(String entityName, Object entity) throws DataAccessException { 664 update(entityName, entity, null); 665 } 666 667 public void update(final String entityName, final Object entity, final LockMode lockMode) 668 throws DataAccessException { 669 670 execute(new HibernateCallback() { 671 public Object doInHibernate(Session session) throws HibernateException { 672 checkWriteOperationAllowed(session); 673 session.update(entityName, entity); 674 if (lockMode != null) { 675 session.lock(entity, lockMode); 676 } 677 return null; 678 } 679 }, true); 680 } 681 682 public void saveOrUpdate(final Object entity) throws DataAccessException { 683 execute(new HibernateCallback() { 684 public Object doInHibernate(Session session) throws HibernateException { 685 checkWriteOperationAllowed(session); 686 session.saveOrUpdate(entity); 687 return null; 688 } 689 }, true); 690 } 691 692 public void saveOrUpdate(final String entityName, final Object entity) throws DataAccessException { 693 execute(new HibernateCallback() { 694 public Object doInHibernate(Session session) throws HibernateException { 695 checkWriteOperationAllowed(session); 696 session.saveOrUpdate(entityName, entity); 697 return null; 698 } 699 }, true); 700 } 701 702 public void saveOrUpdateAll(final Collection entities) throws DataAccessException { 703 execute(new HibernateCallback() { 704 public Object doInHibernate(Session session) throws HibernateException { 705 checkWriteOperationAllowed(session); 706 for (Iterator it = entities.iterator(); it.hasNext();) { 707 session.saveOrUpdate(it.next()); 708 } 709 return null; 710 } 711 }, true); 712 } 713 714 public void replicate(final Object entity, final ReplicationMode replicationMode) 715 throws DataAccessException { 716 717 execute(new HibernateCallback() { 718 public Object doInHibernate(Session session) throws HibernateException { 719 checkWriteOperationAllowed(session); 720 session.replicate(entity, replicationMode); 721 return null; 722 } 723 }, true); 724 } 725 726 public void replicate(final String entityName, final Object entity, final ReplicationMode replicationMode) 727 throws DataAccessException { 728 729 execute(new HibernateCallback() { 730 public Object doInHibernate(Session session) throws HibernateException { 731 checkWriteOperationAllowed(session); 732 session.replicate(entityName, entity, replicationMode); 733 return null; 734 } 735 }, true); 736 } 737 738 public void persist(final Object entity) throws DataAccessException { 739 execute(new HibernateCallback() { 740 public Object doInHibernate(Session session) throws HibernateException { 741 checkWriteOperationAllowed(session); 742 session.persist(entity); 743 return null; 744 } 745 }, true); 746 } 747 748 public void persist(final String entityName, final Object entity) throws |