1 16 17 package org.springframework.orm.hibernate; 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 net.sf.hibernate.Criteria; 30 import net.sf.hibernate.FlushMode; 31 import net.sf.hibernate.Hibernate; 32 import net.sf.hibernate.HibernateException; 33 import net.sf.hibernate.LockMode; 34 import net.sf.hibernate.Query; 35 import net.sf.hibernate.ReplicationMode; 36 import net.sf.hibernate.Session; 37 import net.sf.hibernate.SessionFactory; 38 import net.sf.hibernate.type.Type; 39 40 import org.springframework.dao.DataAccessException; 41 import org.springframework.dao.InvalidDataAccessApiUsageException; 42 import org.springframework.util.Assert; 43 44 118 public class HibernateTemplate extends HibernateAccessor implements HibernateOperations { 119 120 private boolean allowCreate = true; 121 122 private boolean alwaysUseNewSession = false; 123 124 private boolean exposeNativeSession = false; 125 126 private boolean checkWriteOperations = true; 127 128 private boolean cacheQueries = false; 129 130 private String queryCacheRegion; 131 132 private int fetchSize = 0; 133 134 private int maxResults = 0; 135 136 137 140 public HibernateTemplate() { 141 } 142 143 147 public HibernateTemplate(SessionFactory sessionFactory) { 148 setSessionFactory(sessionFactory); 149 afterPropertiesSet(); 150 } 151 152 158 public HibernateTemplate(SessionFactory sessionFactory, boolean allowCreate) { 159 setSessionFactory(sessionFactory); 160 setAllowCreate(allowCreate); 161 afterPropertiesSet(); 162 } 163 164 165 175 public void setAllowCreate(boolean allowCreate) { 176 this.allowCreate = allowCreate; 177 } 178 179 182 public boolean isAllowCreate() { 183 return this.allowCreate; 184 } 185 186 198 public void setAlwaysUseNewSession(boolean alwaysUseNewSession) { 199 this.alwaysUseNewSession = alwaysUseNewSession; 200 } 201 202 205 public boolean isAlwaysUseNewSession() { 206 return this.alwaysUseNewSession; 207 } 208 209 221 public void setExposeNativeSession(boolean exposeNativeSession) { 222 this.exposeNativeSession = exposeNativeSession; 223 } 224 225 229 public boolean isExposeNativeSession() { 230 return this.exposeNativeSession; 231 } 232 233 243 public void setCheckWriteOperations(boolean checkWriteOperations) { 244 this.checkWriteOperations = checkWriteOperations; 245 } 246 247 251 public boolean isCheckWriteOperations() { 252 return this.checkWriteOperations; 253 } 254 255 266 public void setCacheQueries(boolean cacheQueries) { 267 this.cacheQueries = cacheQueries; 268 } 269 270 273 public boolean isCacheQueries() { 274 return this.cacheQueries; 275 } 276 277 287 public void setQueryCacheRegion(String queryCacheRegion) { 288 this.queryCacheRegion = queryCacheRegion; 289 } 290 291 294 public String getQueryCacheRegion() { 295 return this.queryCacheRegion; 296 } 297 298 305 public void setFetchSize(int fetchSize) { 306 this.fetchSize = fetchSize; 307 } 308 309 312 public int getFetchSize() { 313 return this.fetchSize; 314 } 315 316 324 public void setMaxResults(int maxResults) { 325 this.maxResults = maxResults; 326 } 327 328 331 public int getMaxResults() { 332 return this.maxResults; 333 } 334 335 336 public Object execute(HibernateCallback action) throws DataAccessException { 337 return execute(action, isExposeNativeSession()); 338 } 339 340 public List executeFind(HibernateCallback action) throws DataAccessException { 341 Object result = execute(action, isExposeNativeSession()); 342 if (result != null && !(result instanceof List )) { 343 throw new InvalidDataAccessApiUsageException( 344 "Result object returned from HibernateCallback isn't a List: [" + result + "]"); 345 } 346 return (List ) result; 347 } 348 349 357 public Object execute(HibernateCallback action, boolean exposeNativeSession) throws DataAccessException { 358 Assert.notNull(action, "Callback object must not be null"); 359 360 Session session = getSession(); 361 boolean existingTransaction = SessionFactoryUtils.isSessionTransactional(session, getSessionFactory()); 362 if (existingTransaction) { 363 logger.debug("Found thread-bound Session for HibernateTemplate"); 364 } 365 366 FlushMode previousFlushMode = null; 367 try { 368 previousFlushMode = applyFlushMode(session, existingTransaction); 369 Session sessionToExpose = (exposeNativeSession ? session : createSessionProxy(session)); 370 Object result = action.doInHibernate(sessionToExpose); 371 flushIfNecessary(session, existingTransaction); 372 return result; 373 } 374 catch (HibernateException ex) { 375 throw convertHibernateAccessException(ex); 376 } 377 catch (SQLException ex) { 378 throw convertJdbcAccessException(ex); 379 } 380 catch (RuntimeException ex) { 381 throw ex; 383 } 384 finally { 385 if (existingTransaction) { 386 logger.debug("Not closing pre-bound Hibernate Session after HibernateTemplate"); 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 return (Session) Proxy.newProxyInstance( 438 getClass().getClassLoader(), 439 new Class [] {Session.class}, 440 new CloseSuppressingInvocationHandler(session)); 441 } 442 443 444 448 public Object get(Class entityClass, Serializable id) throws DataAccessException { 449 return get(entityClass, id, null); 450 } 451 452 public Object get(final Class entityClass, final Serializable id, final LockMode lockMode) 453 throws DataAccessException { 454 455 return execute(new HibernateCallback() { 456 public Object doInHibernate(Session session) throws HibernateException { 457 if (lockMode != null) { 458 return session.get(entityClass, id, lockMode); 459 } 460 else { 461 return session.get(entityClass, id); 462 } 463 } 464 }, true); 465 } 466 467 public Object load(Class entityClass, Serializable id) throws DataAccessException { 468 return load(entityClass, id, null); 469 } 470 471 public Object load(final Class entityClass, final Serializable id, final LockMode lockMode) 472 throws DataAccessException { 473 474 return execute(new HibernateCallback() { 475 public Object doInHibernate(Session session) throws HibernateException { 476 if (lockMode != null) { 477 return session.load(entityClass, id, lockMode); 478 } 479 else { 480 return session.load(entityClass, id); 481 } 482 } 483 }, true); 484 } 485 486 public List loadAll(final Class entityClass) throws DataAccessException { 487 return (List ) execute(new HibernateCallback() { 488 public Object doInHibernate(Session session) throws HibernateException { 489 Criteria criteria = session.createCriteria(entityClass); 490 prepareCriteria(criteria); 491 return criteria.list(); 492 } 493 }, true); 494 } 495 496 public void load(final Object entity, final Serializable id) throws DataAccessException { 497 execute(new HibernateCallback() { 498 public Object doInHibernate(Session session) throws HibernateException { 499 session.load(entity, id); 500 return null; 501 } 502 }, true); 503 } 504 505 public void refresh(final Object entity) throws DataAccessException { 506 refresh(entity, null); 507 } 508 509 public void refresh(final Object entity, final LockMode lockMode) throws DataAccessException { 510 execute(new HibernateCallback() { 511 public Object doInHibernate(Session session) throws HibernateException { 512 if (lockMode != null) { 513 session.refresh(entity, lockMode); 514 } 515 else { 516 session.refresh(entity); 517 } 518 return null; 519 } 520 }, true); 521 } 522 523 public boolean contains(final Object entity) throws DataAccessException { 524 Boolean result = (Boolean ) execute(new HibernateCallback() { 525 public Object doInHibernate(Session session) { 526 return (session.contains(entity) ? Boolean.TRUE : Boolean.FALSE); 527 } 528 }, true); 529 return result.booleanValue(); 530 } 531 532 public void evict(final Object entity) throws DataAccessException { 533 execute(new HibernateCallback() { 534 public Object doInHibernate(Session session) throws HibernateException { 535 session.evict(entity); 536 return null; 537 } 538 }, true); 539 } 540 541 public void initialize(Object proxy) throws DataAccessException { 542 try { 543 Hibernate.initialize(proxy); 544 } 545 catch (HibernateException ex) { 546 throw SessionFactoryUtils.convertHibernateAccessException(ex); 547 } 548 } 549 550 551 555 public void lock(final Object entity, final LockMode lockMode) throws DataAccessException { 556 execute(new HibernateCallback() { 557 public Object doInHibernate(Session session) throws HibernateException { 558 session.lock(entity, lockMode); 559 return null; 560 } 561 }, true); 562 } 563 564 public Serializable save(final Object entity) throws DataAccessException { 565 return (Serializable ) execute(new HibernateCallback() { 566 public Object doInHibernate(Session session) throws HibernateException { 567 checkWriteOperationAllowed(session); 568 return session.save(entity); 569 } 570 }, true); 571 } 572 573 public void save(final Object entity, final Serializable id) throws DataAccessException { 574 execute(new HibernateCallback() { 575 public Object doInHibernate(Session session) throws HibernateException { 576 checkWriteOperationAllowed(session); 577 session.save(entity, id); 578 return null; 579 } 580 }, true); 581 } 582 583 public void update(Object entity) throws DataAccessException { 584 update(entity, null); 585 } 586 587 public void update(final Object entity, final LockMode lockMode) throws DataAccessException { 588 execute(new HibernateCallback() { 589 public Object doInHibernate(Session session) throws HibernateException { 590 checkWriteOperationAllowed(session); 591 session.update(entity); 592 if (lockMode != null) { 593 session.lock(entity, lockMode); 594 } 595 return null; 596 } 597 }, true); 598 } 599 600 public void saveOrUpdate(final Object entity) throws DataAccessException { 601 execute(new HibernateCallback() { 602 public Object doInHibernate(Session session) throws HibernateException { 603 checkWriteOperationAllowed(session); 604 session.saveOrUpdate(entity); 605 return null; 606 } 607 }, true); 608 } 609 610 public void saveOrUpdateAll(final Collection entities) throws DataAccessException { 611 execute(new HibernateCallback() { 612 public Object doInHibernate(Session session) throws HibernateException { 613 checkWriteOperationAllowed(session); 614 for (Iterator it = entities.iterator(); it.hasNext();) { 615 session.saveOrUpdate(it.next()); 616 } 617 return null; 618 } 619 }, true); 620 } 621 622 public Object saveOrUpdateCopy(final Object entity) throws DataAccessException { 623 return execute(new HibernateCallback() { 624 public Object doInHibernate(Session session) throws HibernateException { 625 checkWriteOperationAllowed(session); 626 return session.saveOrUpdateCopy(entity); 627 } 628 }, true); 629 } 630 631 public void replicate(final Object entity, final ReplicationMode replicationMode) 632 throws DataAccessException { 633 634 execute(new HibernateCallback() { 635 public Object doInHibernate(Session session) throws HibernateException { 636 checkWriteOperationAllowed(session); 637 session.replicate(entity, replicationMode); 638 return null; 639 } 640 }, true); 641 } 642 643 public void delete(Object entity) throws DataAccessException { 644 delete(entity, null); 645 } 646 647 public void delete(final Object entity, final LockMode lockMode) throws DataAccessException { 648 execute(new HibernateCallback() { 649 public Object doInHibernate(Session session) throws HibernateException { 650 checkWriteOperationAllowed(session); 651 if (lockMode != null) { 652 session.lock(entity, lockMode); 653 } 654 session.delete(entity); 655 return null; 656 } 657 }, true); 658 } 659 660 public void deleteAll(final Collection entities) throws DataAccessException { 661 execute(new HibernateCallback() { 662 public Object doInHibernate(Session session) throws HibernateException { 663 checkWriteOperationAllowed(session); 664 for (Iterator it = entities.iterator(); it.hasNext();) { 665 session.delete(it.next()); 666 } 667 return null; 668 } 669 }, true); 670 } 671 672 public void flush() throws DataAccessException { 673 execute(new HibernateCallback() { 674 public Object doInHibernate(Session session) throws HibernateException { 675 session.flush(); 676 return null; 677 } 678 }, true); 679 } 680 681 public void clear() throws DataAccessException { 682 execute(new HibernateCallback() { 683 public Object doInHibernate(Session session) { 684 session.clear(); 685 return null; 686 } 687 }, true); 688 } 689 690 691 695 public List find(String queryString) throws DataAccessException { 696 return find(queryString, (Object []) null, (Type[]) null); 697 } 698 699 public List find(String queryString, Object value) throws DataAccessException { 700 return find(queryString, new Object [] {value}, (Type[]) null); 701 } 702 703 public List find(String queryString, Object value, Type type) throws DataAccessException { 704 return find(queryString, new Object [] {value}, new Type[] {type}); 705 } 706 707 public List find(String queryString, Object [] values) throws DataAccessException { 708 return find(queryString, values, (Type[]) null); 709 } 710 711 public List find(final String queryString, final Object [] values, final Type[] types) 712 throws DataAccessException { 713 714 if (values != null && types != null && values.length != types.length) { 715 throw new IllegalArgumentException ("Length of values array must match length of types array"); 716 } 717 return (List ) execute(new HibernateCallback() { 718 public Object doInHibernate(Session session) throws HibernateException { 719 Query queryObject = session.createQuery(queryString); 720 prepareQuery(queryObject); 721 if (values != null) { 722 for (int i = 0; i < values.length; i++) { 723 if (types != null && types[i] != null) { 724 queryObject.setParameter(i, values[i], types[i]); 725 } 726 else { 727 queryObject.setParameter(i, values[i]); 728 } 729 } 730 } 731 return queryObject.list(); 732 } 733 }, true); 734 } 735 736 public List findByNamedParam(String queryString, String paramName, Object value) 737 throws DataAccessException { 738 739 return findByNamedParam(queryString, paramName, value, null); 740 } 741 742 public List findByNamedParam(String queryString, String paramName, Object value, Type type) 743 throws DataAccessException { 744 745 return findByNamedParam(queryString, new String [] {paramName}, new Object [] {value}, new Type[] {type}); 746 } 747 748 public List findByNamedParam(String queryString, String [] paramNames, Object [] values) 749 throws DataAccessException { 750 751 return findByNamedParam(queryString, paramNames, values, null); 752 } 753 754 public List findByNamedParam( 755 final String queryString, final String [] paramNames, final Object [] values, final Type[] types) 756 throws DataAccessException { 757 758 if (paramNames.length != values.length) { 759 throw new IllegalArgumentException ("Length of paramNames array must match length of values array"); 760 } 761 if (types != null && paramNames.length != types.length) { 762 throw new IllegalArgumentException ("Length of paramNames array must match length of types array"); 763 } 764 return (List ) execute(new HibernateCallback() { 765 public Object doInHibernate(Session session) throws HibernateException { 766 Query queryObject = session.createQuery(queryString); 767 prepareQuery(queryObject); 768 if (values != null) { 769 for (int i = 0; i < values.length; i++) { 770 applyNamedParameterToQuery(queryObject, paramNames[i], values[i], (types != null ? types[i] : null)); 771 } 772 } 773 return queryObject.list(); 774 } 775 }, true); 776 } 777 778 public List findByValueBean(final String queryString, final Object valueBean) 779 throws DataAccessException { 780 781 return (List ) execute(new HibernateCallback() { 782 public Object doInHibernate(Session session) throws HibernateException { 783 Query queryObject = session.createQuery(queryString); 784 prepareQuery(queryObject); 785 queryObject.setProperties(valueBean); 786 return queryObject.list(); 787 } 788 }, true); 789 } 790 791 792 796 public List findByNamedQuery(String queryName) throws DataAccessException { 797 return findByNamedQuery(queryName, (Object []) null, (Type[]) null); 798 } 799 800 public List findByNamedQuery(String queryName, Object value) throws DataAccessException { 801 return findByNamedQuery(queryName, new Object [] {value}, (Type[]) null); 802 } 803 804 public List findByNamedQuery(String queryName, Object value, Type type) throws DataAccessException { 805 return findByNamedQuery(queryName, new Object [] {value}, new Type[] {type}); 806 } 807 808 public List findByNamedQuery(String queryName, Object [] values) throws DataAccessException { 809 return findByNamedQuery(queryName, values, (Type[]) null); 810 } 811 812 public List findByNamedQuery(final String queryName, final Object [] values, final Type[] types) 813 throws DataAccessException { 814 815 if (values != null && types != null && values.length != types.length) { 816 throw new IllegalArgumentException ("Length of values array must match length of types array"); 817 } 818 return (List ) execute(new HibernateCallback() { 819 public Object doInHibernate(Session session) throws HibernateException { 820 Query queryObject = session.getNamedQuery(queryName); 821 prepareQuery(queryObject); 822 if (values != null) { 823 for (int i = 0; i < values.length; i++) { 824 if (types != null && types[i] != null) { 825 queryObject.setParameter(i, values[i], types[i]); 826 } 827 else { 828 queryObject.setParameter(i, values[i]); 829 } 830 } 831 } 832 return queryObject.list(); 833 } 834 }, true); 835 } 836 837 public List findByNamedQueryAndNamedParam(String queryName, String paramName, Object value) 838 throws DataAccessException { 839 840 return findByNamedQueryAndNamedParam(queryName, paramName, value, null); 841 } 842 843 public List findByNamedQueryAndNamedParam(String queryName, String paramName, Object value, Type type) 844 throws DataAccessException { 845 846 return findByNamedQueryAndNamedParam( 847 queryName, new String [] {paramName}, new Object [] {value}, new Type[] {type}); 848 } 849 850 public List findByNamedQueryAndNamedParam(String queryName, String [] paramNames, Object [] values) 851 throws DataAccessException { 852 853 return findByNamedQueryAndNamedParam(queryName, paramNames, values, null); 854 } 855 856 public List findByNamedQueryAndNamedParam( 857 final String queryName, final String [] paramNames, final Object [] values, final Type[] types) 858 throws DataAccessException { 859 860 if (paramNames != null && values != null && paramNames.length != values.length) { 861 throw new IllegalArgumentException ("Length of paramNames array must match length of values array"); 862 } 863 if (values != null && types != null && paramNames.length != types.length) { 864 throw new IllegalArgumentException ("Length of paramNames array must match length of types array"); 865 } 866 return (List ) execute(new HibernateCallback() { 867 public Object doInHibernate(Session session) throws HibernateException { 868 Query queryObject = session.getNamedQuery(queryName); 869 prepareQuery(queryObject); 870 if (values != null) { 871 for (int i = 0; i < values.length; i++) { 872 applyNamedParameterToQuery(queryObject, paramNames[i], values[i], (types != null ? types[i] : null)); 873 } 874 } 875 return queryObject.list(); 876 } 877 }, true); 878 } 879 880 public List findByNamedQueryAndValueBean(final String queryName, final Object valueBean) 881 throws DataAccessException { 882 883 return (List ) execute(new HibernateCallback() { 884 public Object doInHibernate(Session session) throws HibernateException { 885 Query queryObject = session.getNamedQuery(queryName); 886 prepareQuery(queryObject); 887 queryObject.setProperties(valueBean); 888 return queryObject.list(); 889 } 890 }, true); 891 } 892 893 894 898 public Iterator iterate(String queryString) throws DataAccessException { 899 return iterate(queryString, (Object []) null, (Type[]) null); 900 } 901 902 public Iterator iterate(String queryString, Object value) throws DataAccessException { 903 return iterate(queryString, new Object [] {value}, (Type[]) null); 904 } 905 906 public Iterator iterate(String queryString, Object value, Type type) 907 throws DataAccessException { 908 909 return iterate(queryString, new Object [] {value}, new Type[] {type}); 910 } 911 912 public Iterator iterate(String queryString, Object [] values) throws DataAccessException { 913 return iterate(queryString, values, (Type[]) null); 914 } 915 916 public Iterator iterate(final String queryString, final Object [] values, final Type[] types) 917 throws DataAccessException { 918 919 if (values != null && types != null && values.length != types.length) { 920 throw new IllegalArgumentException ("Length of values array must match length of types array"); 921 } 922 return (Iterator ) execute(new HibernateCallback() { 923 public Object doInHibernate(Session session) throws HibernateException { 924 Query queryObject = session.createQuery(queryString); 925 prepareQuery(queryObject); 926 if (values != null) { 927 for (int i = 0; i < values.length; i++) { 928 if (types != null && types[i] != null) { 929 queryObject.setParameter(i, values[i], types[i]); 930 } 931 else { 932 queryObject.setParameter(i, values[i]); 933 } 934 } 935 } 936 return queryObject.iterate(); 937 } 938 }, true); 939 } 940 941 public void closeIterator(Iterator it) throws DataAccessException { 942 try { 943 Hibernate.close(it); 944 } 945 catch (HibernateException ex) { 946 throw SessionFactoryUtils.convertHibernateAccessException(ex); 947 } 948 } 949 950 public int delete(String queryString) throws DataAccessException { 951 return delete(queryString, (Object []) null, (Type[]) null); 952 } 953 954 public int delete(String queryString, Object value, Type type) 955 throws DataAccessException { 956 957 return delete(queryString, new Object [] {value}, new Type[] {type}); 958 } 959 960 public int delete(final String queryString, final Object [] values, final Type[] types) 961 throws DataAccessException { 962 963 if (values != null && types != null && values.length != types.length) { 964 throw new IllegalArgumentException ("Length of values array must match length of types array"); 965 } 966 Integer deleteCount = (Integer ) execute(new HibernateCallback() { 967 public Object doInHibernate(Session session) throws HibernateException { 968 checkWriteOperationAllowed(session); 969 if (values != null) { 970 return new Integer (session.delete(queryString, values, types)); 971 } 972 else { 973 return new Integer (session.delete(queryString)); 974 } 975 } 976 }, true); 977 return deleteCount.intValue(); 978 } 979 980 981 985 997 protected void checkWriteOperationAllowed(Session session) throws InvalidDataAccessApiUsageException { 998 if (isCheckWriteOperations() && getFlushMode() != FLUSH_EAGER && 999 FlushMode.NEVER.equals(session.getFlushMode())) { 1000 throw new InvalidDataAccessApiUsageException( 1001 "Write operations are not allowed in read-only mode (FlushMode.NEVER): "+ 1002 "Turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition."); 1003 } 1004 } 1005 1006 1014 protected void prepareQuery(Query queryObject) { 1015 if (isCacheQueries()) { 1016 queryObject.setCacheable(true); 1017 if (getQueryCacheRegion() != null) { 1018 queryObject.setCacheRegion(getQueryCacheRegion()); 1019 } 1020 } 1021 if (getFetchSize() > 0) { 1022 queryObject.setFetchSize(getFetchSize()); 1023 } 1024 if (getMaxResults() > 0) { 1025 queryObject.setMaxResults(getMaxResults()); 1026 } 1027 SessionFactoryUtils.applyTransactionTimeout(queryObject, getSessionFactory()); 1028 } 1029 1030 1038 protected void prepareCriteria(Criteria criteria) { 1039 if (isCacheQueries()) { 1040 criteria.setCacheable(true); 1041 if (getQueryCacheRegion() != null) { 1042 criteria.setCacheRegion(getQueryCacheRegion()); 1043 } 1044 } 1045 if (getFetchSize() > 0) { 1046 criteria.setFetchSize(getFetchSize()); 1047 } 1048 if (getMaxResults() > 0) { 1049 criteria.setMaxResults(getMaxResults()); 1050 } 1051 SessionFactoryUtils.applyTransactionTimeout(criteria, getSessionFactory()); 1052 } 1053 1054 1062 protected void applyNamedParameterToQuery(Query queryObject, String paramName, Object value, Type type) 1063 throws HibernateException { 1064 1065 if (value instanceof Collection ) { 1066 if (type != null) { 1067 queryObject.setParameterList(paramName, (Collection ) value, type); 1068 } 1069 else { 1070 queryObject.setParameterList(paramName, (Collection ) value); 1071 } 1072 } 1073 else if (value instanceof Object []) { 1074 if (type != null) { 1075 queryObject.setParameterList(paramName, (Object []) value, type); 1076 } 1077 else { 1078 queryObject.setParameterList(paramName, (Object []) value); 1079 } 1080 } 1081 else { 1082 if (type != null) { 1083 queryObject.setParameter(paramName, value, type); 1084 } 1085 else { 1086 queryObject.setParameter(paramName, value); 1087 } 1088 } 1089 } 1090 1091 1092 1097 private class CloseSuppressingInvocationHandler implements InvocationHandler { 1098 1099 private final Session target; 1100 1101 public CloseSuppressingInvocationHandler(Session target) { 1102 this.target = target; 1103 } 1104 1105 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 1106 1108 if (method.getName().equals("equals")) { 1109 return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE); 1111 } 1112 else if (method.getName().equals("hashCode")) { 1113 return new Integer (hashCode()); 1115 } 1116 else if (method.getName().equals("close")) { 1117 return null; 1119 } 1120 1121 try { 1123 Object retVal = method.invoke(this.target, args); 1124 1125 if (retVal instanceof Query) { 1128 prepareQuery(((Query) retVal)); 1129 } 1130 if (retVal instanceof Criteria) { 1131 prepareCriteria(((Criteria) retVal)); 1132 } 1133 1134 return retVal; 1135 } 1136 catch (InvocationTargetException ex) { 1137 throw ex.getTargetException(); 1138 } 1139 } 1140 } 1141 1142} 1143 | Popular Tags |