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 DataAccessException { 749 execute(new HibernateCallback() { 750 public Object doInHibernate(Session session) throws HibernateException { 751 checkWriteOperationAllowed(session); 752 session.persist(entityName, entity); 753 return null; 754 } 755 }, true); 756 } 757 758 public Object merge(final Object entity) throws DataAccessException { 759 return execute(new HibernateCallback() { 760 public Object doInHibernate(Session session) throws HibernateException { 761 checkWriteOperationAllowed(session); 762 return session.merge(entity); 763 } 764 }, true); 765 } 766 767 public Object merge(final String entityName, final Object entity) throws DataAccessException { 768 return execute(new HibernateCallback() { 769 public Object doInHibernate(Session session) throws HibernateException { 770 checkWriteOperationAllowed(session); 771 return session.merge(entityName, entity); 772 } 773 }, true); 774 } 775 776 public void delete(Object entity) throws DataAccessException { 777 delete(entity, null); 778 } 779 780 public void delete(final Object entity, final LockMode lockMode) throws DataAccessException { 781 execute(new HibernateCallback() { 782 public Object doInHibernate(Session session) throws HibernateException { 783 checkWriteOperationAllowed(session); 784 if (lockMode != null) { 785 session.lock(entity, lockMode); 786 } 787 session.delete(entity); 788 return null; 789 } 790 }, true); 791 } 792 793 public void deleteAll(final Collection entities) throws DataAccessException { 794 execute(new HibernateCallback() { 795 public Object doInHibernate(Session session) throws HibernateException { 796 checkWriteOperationAllowed(session); 797 for (Iterator it = entities.iterator(); it.hasNext();) { 798 session.delete(it.next()); 799 } 800 return null; 801 } 802 }, true); 803 } 804 805 public void flush() throws DataAccessException { 806 execute(new HibernateCallback() { 807 public Object doInHibernate(Session session) throws HibernateException { 808 session.flush(); 809 return null; 810 } 811 }, true); 812 } 813 814 public void clear() throws DataAccessException { 815 execute(new HibernateCallback() { 816 public Object doInHibernate(Session session) { 817 session.clear(); 818 return null; 819 } 820 }, true); 821 } 822 823 824 828 public List find(String queryString) throws DataAccessException { 829 return find(queryString, (Object []) null); 830 } 831 832 public List find(String queryString, Object value) throws DataAccessException { 833 return find(queryString, new Object [] {value}); 834 } 835 836 public List find(final String queryString, final Object [] values) throws DataAccessException { 837 return (List ) execute(new HibernateCallback() { 838 public Object doInHibernate(Session session) throws HibernateException { 839 Query queryObject = session.createQuery(queryString); 840 prepareQuery(queryObject); 841 if (values != null) { 842 for (int i = 0; i < values.length; i++) { 843 queryObject.setParameter(i, values[i]); 844 } 845 } 846 return queryObject.list(); 847 } 848 }, true); 849 } 850 851 public List findByNamedParam(String queryString, String paramName, Object value) 852 throws DataAccessException { 853 854 return findByNamedParam(queryString, new String [] {paramName}, new Object [] {value}); 855 } 856 857 public List findByNamedParam(final String queryString, final String [] paramNames, final Object [] values) 858 throws DataAccessException { 859 860 if (paramNames.length != values.length) { 861 throw new IllegalArgumentException ("Length of paramNames array must match length of values array"); 862 } 863 return (List ) execute(new HibernateCallback() { 864 public Object doInHibernate(Session session) throws HibernateException { 865 Query queryObject = session.createQuery(queryString); 866 prepareQuery(queryObject); 867 if (values != null) { 868 for (int i = 0; i < values.length; i++) { 869 applyNamedParameterToQuery(queryObject, paramNames[i], values[i]); 870 } 871 } 872 return queryObject.list(); 873 } 874 }, true); 875 } 876 877 public List findByValueBean(final String queryString, final Object valueBean) 878 throws DataAccessException { 879 880 return (List ) execute(new HibernateCallback() { 881 public Object doInHibernate(Session session) throws HibernateException { 882 Query queryObject = session.createQuery(queryString); 883 prepareQuery(queryObject); 884 queryObject.setProperties(valueBean); 885 return queryObject.list(); 886 } 887 }, true); 888 } 889 890 891 895 public List findByNamedQuery(String queryName) throws DataAccessException { 896 return findByNamedQuery(queryName, (Object []) null); 897 } 898 899 public List findByNamedQuery(String queryName, Object value) throws DataAccessException { 900 return findByNamedQuery(queryName, new Object [] {value}); 901 } 902 903 public List findByNamedQuery(final String queryName, final Object [] values) throws DataAccessException { 904 return (List ) execute(new HibernateCallback() { 905 public Object doInHibernate(Session session) throws HibernateException { 906 Query queryObject = session.getNamedQuery(queryName); 907 prepareQuery(queryObject); 908 if (values != null) { 909 for (int i = 0; i < values.length; i++) { 910 queryObject.setParameter(i, values[i]); 911 } 912 } 913 return queryObject.list(); 914 } 915 }, true); 916 } 917 918 public List findByNamedQueryAndNamedParam(String queryName, String paramName, Object value) 919 throws DataAccessException { 920 921 return findByNamedQueryAndNamedParam(queryName, new String [] {paramName}, new Object [] {value}); 922 } 923 924 public List findByNamedQueryAndNamedParam( 925 final String queryName, final String [] paramNames, final Object [] values) 926 throws DataAccessException { 927 928 if (paramNames != null && values != null && paramNames.length != values.length) { 929 throw new IllegalArgumentException ("Length of paramNames array must match length of values array"); 930 } 931 return (List ) execute(new HibernateCallback() { 932 public Object doInHibernate(Session session) throws HibernateException { 933 Query queryObject = session.getNamedQuery(queryName); 934 prepareQuery(queryObject); 935 if (values != null) { 936 for (int i = 0; i < values.length; i++) { 937 applyNamedParameterToQuery(queryObject, paramNames[i], values[i]); 938 } 939 } 940 return queryObject.list(); 941 } 942 }, true); 943 } 944 945 public List findByNamedQueryAndValueBean(final String queryName, final Object valueBean) 946 throws DataAccessException { 947 948 return (List ) execute(new HibernateCallback() { 949 public Object doInHibernate(Session session) throws HibernateException { 950 Query queryObject = session.getNamedQuery(queryName); 951 prepareQuery(queryObject); 952 queryObject.setProperties(valueBean); 953 return queryObject.list(); 954 } 955 }, true); 956 } 957 958 959 963 public List findByCriteria(DetachedCriteria criteria) throws DataAccessException { 964 return findByCriteria(criteria, -1, -1); 965 } 966 967 public List findByCriteria(final DetachedCriteria criteria, final int firstResult, final int maxResults) 968 throws DataAccessException { 969 970 Assert.notNull(criteria, "DetachedCriteria must not be null"); 971 return (List ) execute(new HibernateCallback() { 972 public Object doInHibernate(Session session) throws HibernateException { 973 Criteria executableCriteria = criteria.getExecutableCriteria(session); 974 prepareCriteria(executableCriteria); 975 if (firstResult >= 0) { 976 executableCriteria.setFirstResult(firstResult); 977 } 978 if (maxResults > 0) { 979 executableCriteria.setMaxResults(maxResults); 980 } 981 return executableCriteria.list(); 982 } 983 }, true); 984 } 985 986 public List findByExample(Object exampleEntity) throws DataAccessException { 987 return findByExample(exampleEntity, -1, -1); 988 } 989 990 public List findByExample(final Object exampleEntity, final int firstResult, final int maxResults) 991 throws DataAccessException { 992 993 Assert.notNull(exampleEntity, "Example entity must not be null"); 994 return (List ) execute(new HibernateCallback() { 995 public Object doInHibernate(Session session) throws HibernateException { 996 Criteria executableCriteria = session.createCriteria(exampleEntity.getClass()); 997 executableCriteria.add(Example.create(exampleEntity)); 998 prepareCriteria(executableCriteria); 999 if (firstResult >= 0) { 1000 executableCriteria.setFirstResult(firstResult); 1001 } 1002 if (maxResults > 0) { 1003 executableCriteria.setMaxResults(maxResults); 1004 } 1005 return executableCriteria.list(); 1006 } 1007 }, true); 1008 } 1009 1010 1011 1015 public Iterator iterate(String queryString) throws DataAccessException { 1016 return iterate(queryString, (Object []) null); 1017 } 1018 1019 public Iterator iterate(String queryString, Object value) throws DataAccessException { 1020 return iterate(queryString, new Object [] {value}); 1021 } 1022 1023 public Iterator iterate(final String queryString, final Object [] values) throws DataAccessException { 1024 return (Iterator ) execute(new HibernateCallback() { 1025 public Object doInHibernate(Session session) throws HibernateException { 1026 Query queryObject = session.createQuery(queryString); 1027 prepareQuery(queryObject); 1028 if (values != null) { 1029 for (int i = 0; i < values.length; i++) { 1030 queryObject.setParameter(i, values[i]); 1031 } 1032 } 1033 return queryObject.iterate(); 1034 } 1035 }, true); 1036 } 1037 1038 public void closeIterator(Iterator it) throws DataAccessException { 1039 try { 1040 Hibernate.close(it); 1041 } 1042 catch (HibernateException ex) { 1043 throw SessionFactoryUtils.convertHibernateAccessException(ex); 1044 } 1045 } 1046 1047 public int bulkUpdate(String queryString) throws DataAccessException { 1048 return bulkUpdate(queryString, (Object []) null); 1049 } 1050 1051 public int bulkUpdate(String queryString, Object value) throws DataAccessException { 1052 return bulkUpdate(queryString, new Object [] {value}); 1053 } 1054 1055 public int bulkUpdate(final String queryString, final Object [] values) throws DataAccessException { 1056 Integer updateCount = (Integer ) execute(new HibernateCallback() { 1057 public Object doInHibernate(Session session) throws HibernateException { 1058 Query queryObject = session.createQuery(queryString); 1059 prepareQuery(queryObject); 1060 if (values != null) { 1061 for (int i = 0; i < values.length; i++) { 1062 queryObject.setParameter(i, values[i]); 1063 } 1064 } 1065 return new Integer (queryObject.executeUpdate()); 1066 } 1067 }, true); 1068 return updateCount.intValue(); 1069 } 1070 1071 1072 1076 1089 protected void checkWriteOperationAllowed(Session session) throws InvalidDataAccessApiUsageException { 1090 if (isCheckWriteOperations() && getFlushMode() != FLUSH_EAGER && 1091 session.getFlushMode().lessThan(FlushMode.COMMIT)) { 1092 throw new InvalidDataAccessApiUsageException( 1093 "Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): "+ 1094 "Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition."); 1095 } 1096 } 1097 1098 1106 protected void prepareQuery(Query queryObject) { 1107 if (isCacheQueries()) { 1108 queryObject.setCacheable(true); 1109 if (getQueryCacheRegion() != null) { 1110 queryObject.setCacheRegion(getQueryCacheRegion()); 1111 } 1112 } 1113 if (getFetchSize() > 0) { 1114 queryObject.setFetchSize(getFetchSize()); 1115 } 1116 if (getMaxResults() > 0) { 1117 queryObject.setMaxResults(getMaxResults()); 1118 } 1119 SessionFactoryUtils.applyTransactionTimeout(queryObject, getSessionFactory()); 1120 } 1121 1122 1130 protected void prepareCriteria(Criteria criteria) { 1131 if (isCacheQueries()) { 1132 criteria.setCacheable(true); 1133 if (getQueryCacheRegion() != null) { 1134 criteria.setCacheRegion(getQueryCacheRegion()); 1135 } 1136 } 1137 if (getFetchSize() > 0) { 1138 criteria.setFetchSize(getFetchSize()); 1139 } 1140 if (getMaxResults() > 0) { 1141 criteria.setMaxResults(getMaxResults()); 1142 } 1143 SessionFactoryUtils.applyTransactionTimeout(criteria, getSessionFactory()); 1144 } 1145 1146 1153 protected void applyNamedParameterToQuery(Query queryObject, String paramName, Object value) 1154 throws HibernateException { 1155 1156 if (value instanceof Collection ) { 1157 queryObject.setParameterList(paramName, (Collection ) value); 1158 } 1159 else if (value instanceof Object []) { 1160 queryObject.setParameterList(paramName, (Object []) value); 1161 } 1162 else { 1163 queryObject.setParameter(paramName, value); 1164 } 1165 } 1166 1167 1168 1173 private class CloseSuppressingInvocationHandler implements InvocationHandler { 1174 1175 private final Session target; 1176 1177 public CloseSuppressingInvocationHandler(Session target) { 1178 this.target = target; 1179 } 1180 1181 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 1182 1184 if (method.getName().equals("equals")) { 1185 return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE); 1187 } 1188 else if (method.getName().equals("hashCode")) { 1189 return new Integer (hashCode()); 1191 } 1192 else if (method.getName().equals("close")) { 1193 return null; 1195 } 1196 1197 try { 1199 Object retVal = method.invoke(this.target, args); 1200 1201 if (retVal instanceof Query) { 1204 prepareQuery(((Query) retVal)); 1205 } 1206 if (retVal instanceof Criteria) { 1207 prepareCriteria(((Criteria) retVal)); 1208 } 1209 1210 return retVal; 1211 } 1212 catch (InvocationTargetException ex) { 1213 throw ex.getTargetException(); 1214 } 1215 } 1216 } 1217 1218} 1219 | Popular Tags |