1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package edu.internet2.middleware.grouper.hibernate;
20
21 import java.sql.SQLException;
22 import java.sql.Savepoint;
23 import java.util.LinkedHashMap;
24 import java.util.LinkedHashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.commons.lang.exception.ExceptionUtils;
31 import org.apache.commons.logging.Log;
32 import org.hibernate.HibernateException;
33 import org.hibernate.Query;
34 import org.hibernate.SQLQuery;
35 import org.hibernate.Session;
36 import org.hibernate.StaleObjectStateException;
37 import org.hibernate.StaleStateException;
38 import org.hibernate.Transaction;
39 import org.hibernate.internal.SessionImpl;
40 import org.hibernate.resource.transaction.spi.TransactionStatus;
41 import org.hibernate.type.StandardBasicTypes;
42 import org.hibernate.type.Type;
43
44 import edu.internet2.middleware.grouper.cfg.GrouperConfig;
45 import edu.internet2.middleware.grouper.ddl.GrouperDdlUtils;
46 import edu.internet2.middleware.grouper.exception.GrouperReadonlyException;
47 import edu.internet2.middleware.grouper.exception.GrouperStaleObjectStateException;
48 import edu.internet2.middleware.grouper.exception.GrouperStaleStateException;
49 import edu.internet2.middleware.grouper.exception.GrouperValidationException;
50 import edu.internet2.middleware.grouper.hooks.logic.HookVeto;
51 import edu.internet2.middleware.grouper.internal.dao.GrouperDAOException;
52 import edu.internet2.middleware.grouper.misc.GrouperDAOFactory;
53 import edu.internet2.middleware.grouper.util.GrouperUtil;
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public class HibernateSession {
70
71
72
73
74 private static final String READONLY_ERROR = "Grouper is in readonly mode (perhaps due to maintenance), you cannot perform an operation which changes the data!";
75
76
77 private static final Log LOG = GrouperUtil.getLog(HibernateSession.class);
78
79
80
81
82
83 public ByObject byObject(){
84 return new ByObject(this);
85 }
86
87
88 static int savePointCount = 0;
89
90
91 private static ThreadLocal<Boolean> threadlocalReadonly = new ThreadLocal<Boolean>();
92
93
94
95
96 public static Boolean internal_retrieveThreadlocalReadonly() {
97 return threadlocalReadonly.get();
98 }
99
100
101
102
103
104 public static void internal_assignThreadlocalReadonly(Boolean internal_threadlocalReadonly) {
105 if (internal_threadlocalReadonly == null) {
106 threadlocalReadonly.remove();
107 } else {
108 threadlocalReadonly.set(internal_threadlocalReadonly);
109 }
110 }
111
112
113
114
115
116 public static boolean isReadonlyMode() {
117 if (GrouperConfig.retrieveConfig().propertyValueBoolean("grouper.api.readonly", false)) {
118 return true;
119 }
120
121 Boolean threadlocalBoolean = threadlocalReadonly.get();
122
123 return threadlocalBoolean != null && threadlocalBoolean;
124
125 }
126
127
128
129
130
131 public static void threadLocalReadonlyAssign() {
132 threadlocalReadonly.set(true);
133 }
134
135
136
137
138 public static void threadLocalReadonlyClear() {
139 threadlocalReadonly.remove();
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155 @SuppressWarnings("deprecation")
156 private HibernateSession../../../../../edu/internet2/middleware/grouper/hibernate/HibernateSession.html#HibernateSession">HibernateSession(HibernateSession parentHibernateSession,
157 GrouperTransactionType grouperTransactionType) throws GrouperDAOException {
158
159 Map<String, Object> debugMap = LOG.isDebugEnabled() ? new LinkedHashMap<String, Object>() : null;
160 GrouperTransactionType originalGrouperTransactionType = grouperTransactionType;
161 try {
162
163 if (LOG.isDebugEnabled()) {
164 debugMap.put("grouperTransactionType", grouperTransactionType);
165 }
166
167 boolean okToUseHibernate = GrouperDdlUtils.okToUseHibernate();
168
169 if (LOG.isDebugEnabled()) {
170 debugMap.put("okToUseHibernate", okToUseHibernate);
171 }
172
173 if (!okToUseHibernate) {
174 if (GrouperConfig.retrieveConfig().propertyValueBoolean("ddlutils.failIfNotRightVersion", true)) {
175 throw new RuntimeException("Database schema ddl is not up to date, or has issues, check logs and config ddl in grouper.properties and run: gsh -registry -check");
176 }
177 }
178
179 if (grouperTransactionType != null && grouperTransactionType.isTransactional()) {
180
181 boolean readonlyMode = isReadonlyMode();
182
183 if (LOG.isDebugEnabled()) {
184 debugMap.put("readonlyMode", readonlyMode);
185 }
186
187 if (readonlyMode) {
188 if (grouperTransactionType != null && grouperTransactionType.isTransactional()) {
189 grouperTransactionType = GrouperTransactionType.READONLY_OR_USE_EXISTING;
190
191 if (LOG.isDebugEnabled() && grouperTransactionType != originalGrouperTransactionType) {
192 debugMap.put("readonlyGrouperTransactionTypeChangedTo", grouperTransactionType);
193 }
194 originalGrouperTransactionType = grouperTransactionType;
195 }
196 }
197 }
198
199 boolean parentSessionExists = parentHibernateSession != null;
200 if (LOG.isDebugEnabled()) {
201 debugMap.put("parentSessionExists", parentSessionExists);
202 }
203
204 this.immediateGrouperTransactionTypeDeclared = grouperTransactionType;
205
206
207 if (parentSessionExists) {
208
209 this.cachingEnabled = parentHibernateSession.cachingEnabled;
210
211 }
212
213
214 if (parentSessionExists && !grouperTransactionType.isNewAutonomous() &&
215 parentHibernateSession.activeHibernateSession().immediateGrouperTransactionTypeUsed.isTransactional()) {
216
217
218 this.parentSession = parentHibernateSession;
219
220
221 this.immediateGrouperTransactionTypeDeclared.checkCompatibility(
222 this.parentSession.getGrouperTransactionType());
223 }
224
225 boolean newHibernateSession = this.isNewHibernateSession();
226
227 if (LOG.isDebugEnabled()) {
228 debugMap.put("newHibernateSession", newHibernateSession);
229 }
230
231 if (newHibernateSession) {
232
233 if (grouperTransactionType == null) {
234 throw new NullPointerException("transaction type is null in hibernate session");
235 }
236
237 this.immediateGrouperTransactionTypeUsed = grouperTransactionType
238 .grouperTransactionTypeToUse();
239
240
241 if (!GrouperTransactionType.NONE.equals(grouperTransactionType)) {
242 this.immediateSession = GrouperDAOFactory.getFactory().getSession();
243 }
244
245 if (LOG.isDebugEnabled()) {
246 debugMap.put("immediateGrouperTransactionTypeUsed", this.immediateGrouperTransactionTypeUsed);
247 debugMap.put("immediateGrouperTransactionTypeReadonly", this.immediateGrouperTransactionTypeUsed.isReadonly());
248 }
249
250
251 if (!this.immediateGrouperTransactionTypeUsed.isReadonly()) {
252 if (LOG.isDebugEnabled()) {
253 debugMap.put("beginTransaction", "true");
254 }
255 this.immediateTransaction = this.immediateSession.beginTransaction();
256
257 String useSavepointsString = GrouperConfig.retrieveConfig().propertyValueString("jdbc.useSavePoints");
258 boolean useSavepoints;
259 if (StringUtils.isBlank(useSavepointsString)) {
260 useSavepoints = !GrouperDdlUtils.isHsql();
261 } else {
262 useSavepoints = GrouperUtil.booleanValue(useSavepointsString);
263 }
264
265 if (LOG.isDebugEnabled()) {
266 debugMap.put("useSavepoints", useSavepoints);
267 }
268
269 if (useSavepoints && (parentSessionExists
270 || GrouperConfig.retrieveConfig().propertyValueBoolean("jdbc.useSavePointsOnAllNewTransactions", false))) {
271 try {
272 this.savepoint = ((SessionImpl)this.activeHibernateSession().getSession()).connection().setSavepoint();
273 savePointCount++;
274 } catch (SQLException sqle) {
275 throw new RuntimeException("Problem setting save point for transaction type: "
276 + grouperTransactionType, sqle);
277 }
278 } else if (GrouperDdlUtils.isHsql() && parentSessionExists) {
279
280 savePointCount++;
281 }
282 }
283 }
284
285
286 addStaticHibernateSession(this);
287 } finally {
288 if (LOG.isDebugEnabled()) {
289 debugMap.put("hibernateSession", this.toString());
290 LOG.debug(GrouperUtil.stack());
291 LOG.debug(GrouperUtil.mapToString(debugMap));
292 }
293 }
294 }
295
296
297 private HibernateSession parentSession = null;
298
299
300
301
302
303 private Session immediateSession = null;
304
305
306
307
308
309 private Transaction immediateTransaction = null;
310
311
312
313
314 private Savepoint savepoint = null;
315
316
317
318
319
320 private GrouperTransactionType immediateGrouperTransactionTypeUsed = null;
321
322
323
324
325
326 private GrouperTransactionType immediateGrouperTransactionTypeDeclared = null;
327
328
329
330
331 private boolean cachingEnabled = true;
332
333
334
335
336
337 public boolean isCachingEnabled() {
338 return this.cachingEnabled;
339 }
340
341
342
343
344
345
346
347 public void setCachingEnabled(boolean enabledCaching1) {
348 this.cachingEnabled = enabledCaching1;
349 }
350
351
352
353
354 private static ThreadLocal<Set<HibernateSession>> staticSessions = new ThreadLocal<Set<HibernateSession>>();
355
356
357
358
359
360 public static Set<HibernateSession> _internal_staticSessions() {
361 return getHibernateSessionSet();
362 }
363
364
365
366
367
368
369 private static Set<HibernateSession> getHibernateSessionSet() {
370 Set<HibernateSession> hibSet = staticSessions.get();
371 if (hibSet == null) {
372
373 hibSet = new LinkedHashSet<HibernateSession>();
374 staticSessions.set(hibSet);
375 }
376 return hibSet;
377 }
378
379
380
381
382
383
384
385 private static void removeStaticHibernateSession(HibernateSession hibernateSession) {
386 getHibernateSessionSet().remove(hibernateSession);
387 }
388
389
390
391
392
393 public static void resetAllThreadLocals() {
394 getHibernateSessionSet().clear();
395 }
396
397
398
399
400
401
402 private static void addStaticHibernateSession(HibernateSession hibernateSession) {
403 Set<HibernateSession> hibSet = getHibernateSessionSet();
404 hibSet.add(hibernateSession);
405
406 if (hibSet.size() > 20) {
407 hibSet.clear();
408 throw new RuntimeException(
409 "There is probably a problem that there are 20 nested new HibernateSessions called!");
410 }
411 }
412
413
414
415
416
417 public static HibernateSession _internal_hibernateSession() {
418 return staticHibernateSession();
419 }
420
421
422
423
424
425
426
427 private static HibernateSession staticHibernateSession() {
428 Set<HibernateSession> hibSet = getHibernateSessionSet();
429 int size = hibSet.size();
430 if (size == 0) {
431 return null;
432 }
433
434 return (HibernateSession) GrouperUtil.get(hibSet, size - 1);
435 }
436
437
438
439
440
441 public static void _internal_closeAllHibernateSessions(Throwable t) {
442
443 try {
444 for (HibernateSession hibernateSession : HibernateSession._internal_staticSessions()) {
445 try {
446 HibernateSession._internal_hibernateSessionCatch(hibernateSession, t);
447 } catch (Exception e) {
448
449 LOG.debug("Error handling hibernate error", e);
450 } finally {
451 try {
452 HibernateSession._internal_hibernateSessionFinally(hibernateSession);
453 } catch (Throwable t3) {
454 LOG.debug("Error in finally for hibernate session", t3);
455 }
456 }
457 }
458 } catch (Throwable t2) {
459 LOG.debug("Problem closing sessions", t2);
460 }
461 }
462
463
464
465
466
467
468
469 public static HibernateSession _internal_hibernateSession(GrouperTransactionType grouperTransactionType) throws GrouperDAOException {
470 HibernateSessionbernateSession.html#HibernateSession">HibernateSession hibernateSession = new HibernateSession(staticHibernateSession(), grouperTransactionType);
471 return hibernateSession;
472 }
473
474
475
476
477
478
479 @SuppressWarnings("deprecation")
480 public static void _internal_hibernateSessionEnd(HibernateSession hibernateSession) throws SQLException {
481
482
483
484 Session session = hibernateSession.activeHibernateSession().immediateSession;
485
486
487 if (hibernateSession.isReadonly()
488 && session != null && session.isDirty()) {
489 ((SessionImpl)session).connection().rollback();
490
491
492 }
493
494
495
496
497 if (hibernateSession.isNewHibernateSession() && !hibernateSession.isReadonly()
498 && hibernateSession.immediateTransaction.getStatus().isOneOf(TransactionStatus.ACTIVE)) {
499
500 LOG.debug("endTransactionAutoCommit");
501
502 assertNotGrouperReadonly();
503
504 hibernateSession.immediateTransaction.commit();
505
506 } else {
507
508
509 if (session != null && !hibernateSession.isNewHibernateSession()) {
510
511 session.flush();
512
513
514 session.clear();
515 }
516 }
517
518 }
519
520
521
522
523 public static void assertNotGrouperReadonly() {
524 if (GrouperConfig.retrieveConfig().propertyValueBoolean("grouper.api.readonly", false)) {
525 throw new GrouperReadonlyException(READONLY_ERROR);
526 }
527 }
528
529
530
531
532
533
534
535 @SuppressWarnings("deprecation")
536 public static void _internal_hibernateSessionCatch(HibernateSession hibernateSession, Throwable e) throws GrouperDAOException {
537
538 try {
539
540 if (hibernateSession != null && hibernateSession.savepoint != null) {
541 try {
542 ((SessionImpl)hibernateSession.activeHibernateSession().getSession()).connection().rollback(hibernateSession.savepoint);
543 } catch (SQLException sqle) {
544 throw new RuntimeException("Problem rolling back savepoint", sqle);
545 }
546 }
547 } catch (RuntimeException re) {
548
549 if (!GrouperUtil.injectInException(e, "Exception rolling back savepoint in exception catch: " + ExceptionUtils.getFullStackTrace(re))) {
550 LOG.error("Error", e);
551 }
552 }
553
554 try {
555
556
557
558
559 if (hibernateSession != null && hibernateSession.isNewHibernateSession() && !hibernateSession.isReadonly()) {
560 if (hibernateSession.immediateTransaction.getStatus().isOneOf(TransactionStatus.ACTIVE)) {
561 LOG.debug("endTransactionRollback");
562 hibernateSession.immediateTransaction.rollback();
563 }
564 }
565 } catch (RuntimeException re) {
566
567 if (!GrouperUtil.injectInException(e, "Exception rolling back in exception catch: " + ExceptionUtils.getFullStackTrace(re))) {
568 LOG.error("Error", e);
569 }
570 }
571
572
573 GrouperUtil.logErrorNextException(LOG, e, 100);
574
575 String errorString = "Problem in HibernateSession: " + hibernateSession;
576
577 if (e instanceof GrouperDAOException) {
578 if (!GrouperUtil.injectInException(e, errorString)) {
579 LOG.error(errorString);
580 }
581 throw (GrouperDAOException) e;
582 }
583 if (e instanceof StaleObjectStateException) {
584 throw new GrouperStaleObjectStateException(errorString, e);
585 }
586 if (e instanceof StaleStateException) {
587 throw new GrouperStaleStateException(errorString, e);
588 }
589
590 if (e instanceof HibernateException) {
591 throw new GrouperDAOException(errorString, e);
592 }
593 if (e instanceof HookVeto) {
594 throw (HookVeto)e;
595 }
596 if (e instanceof GrouperValidationException) {
597 throw (GrouperValidationException)e;
598 }
599
600 if (e instanceof RuntimeException) {
601 if (!GrouperUtil.injectInException(e, errorString)) {
602 LOG.error(errorString);
603 }
604 throw (RuntimeException) e;
605 }
606
607 throw new GrouperDAOException(errorString, e);
608
609 }
610
611
612
613
614
615
616
617 public static boolean _internal_hibernateSessionFinally(HibernateSession hibernateSession) {
618 if (hibernateSession != null) {
619
620 removeStaticHibernateSession(hibernateSession);
621
622 if (hibernateSession.isNewHibernateSession()) {
623
624
625
626 return closeSessionIfNotClosed(hibernateSession.immediateSession);
627 }
628 }
629 return false;
630 }
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646 public static Object callbackHibernateSession(
647 GrouperTransactionType grouperTransactionType, AuditControl auditControl, HibernateHandler hibernateHandler)
648 throws GrouperDAOException {
649
650 Map<String, Object> debugMap = LOG.isDebugEnabled() ? new LinkedHashMap<String, Object>() : null;
651
652 Object ret = null;
653 HibernateSession hibernateSession = null;
654
655 try {
656
657 if (LOG.isDebugEnabled()) {
658 debugMap.put("grouperTransactionType", grouperTransactionType == null ? null : grouperTransactionType.name());
659 debugMap.put("auditControl", auditControl == null ? null : auditControl);
660 }
661
662 hibernateSession = _internal_hibernateSession(grouperTransactionType);
663
664 if (LOG.isDebugEnabled()) {
665
666 debugMap.put("hibernateSession", hibernateSession.toString());
667
668 StringBuilder sessionsThreadLocal = new StringBuilder();
669 boolean first = true;
670 for (HibernateSession theHibernateSession : getHibernateSessionSet()) {
671 if (!first) {
672 sessionsThreadLocal.append(", ");
673 }
674 sessionsThreadLocal.append(Integer.toHexString(theHibernateSession.hashCode()));
675 first = false;
676 }
677 debugMap.put("hibernateSessionsInThreadLocal", sessionsThreadLocal.toString());
678 }
679
680 HibernateHandlerBeanandlerBean.html#HibernateHandlerBean">HibernateHandlerBean hibernateHandlerBean = new HibernateHandlerBean();
681 boolean willCreateAudit = AuditControl.WILL_AUDIT.equals(auditControl);
682 if (LOG.isDebugEnabled()) {
683 debugMap.put("willCreateAudit", willCreateAudit);
684 }
685 hibernateHandlerBean.setCallerWillCreateAudit(willCreateAudit);
686
687
688 boolean callerWillAudit = GrouperContext.contextExistsInner();
689
690
691 boolean createdContext = willCreateAudit ? GrouperContext.createNewInnerContextIfNotExist() : false;
692
693 if (LOG.isDebugEnabled()) {
694 debugMap.put("createdContext", createdContext);
695 }
696
697 try {
698 hibernateHandlerBean.setCallerWillCreateAudit(callerWillAudit);
699 hibernateHandlerBean.setNewContext(createdContext);
700
701 hibernateHandlerBean.setHibernateSession(hibernateSession);
702
703 ret = hibernateHandler.callback(hibernateHandlerBean);
704 } finally {
705
706 if (createdContext) {
707 GrouperContext.deleteInnerContext();
708 }
709 }
710 _internal_hibernateSessionEnd(hibernateSession);
711
712 } catch (Throwable e) {
713 _internal_hibernateSessionCatch(hibernateSession, e);
714 } finally {
715 boolean closed = _internal_hibernateSessionFinally(hibernateSession);
716 if (LOG.isDebugEnabled()) {
717 debugMap.put("closedSession", closed);
718 LOG.debug(GrouperUtil.stack());
719 LOG.debug(GrouperUtil.mapToString(debugMap));
720 }
721 }
722 return ret;
723
724 }
725
726
727
728
729
730
731 public static ByHqlStatic byHqlStatic() {
732 return new ByHqlStatic();
733 }
734
735
736
737
738
739
740 public static BySqlStatic bySqlStatic() {
741 return new BySqlStatic();
742 }
743
744
745
746
747
748
749 public static ByCriteriaStatic byCriteriaStatic() {
750 return new ByCriteriaStatic();
751 }
752
753
754
755
756
757
758
759 public static ByObjectStatic byObjectStatic() {
760 return new ByObjectStatic();
761 }
762
763
764
765
766
767
768
769
770
771 private static boolean closeSessionIfNotClosed(Session session) {
772
773 if (session != null) {
774
775 try {
776
777 if (session.isConnected() && session.isOpen()) {
778 session.close();
779 return true;
780 }
781 } catch (Exception e) {
782
783 }
784 }
785 return false;
786 }
787
788
789
790
791 @Override
792 public String toString() {
793 try {
794 return "HibernateSession (" + Integer.toHexString(this.hashCode()) + "): "
795 + (this.isNewHibernateSession() ? "new" : "notNew") + ", "
796 + (this.isReadonly() ? "readonly" : "notReadonly") + ", "
797 + (this.getGrouperTransactionType() == null ? null : this.getGrouperTransactionType().name()) + ", "
798 + (this.isTransactionActive() ? "activeTransaction" : "notActiveTransaction" )
799 + ", session (" + (this.getSession() == null ? null : Integer.toHexString(this.getSession().hashCode())) + ")"
800 ;
801 } catch (NullPointerException npe) {
802 throw npe;
803 }
804 }
805
806
807
808
809
810
811 public Session getSession() {
812 return this.activeHibernateSession().immediateSession;
813 }
814
815
816
817
818
819 public HibernateMisc misc(){
820 return new HibernateMisc(this);
821 }
822
823
824
825
826
827 public ByHql byHql(){
828 return new ByHql(this);
829 }
830
831
832
833
834
835 public BySql bySql(){
836 return new BySql(this);
837 }
838
839
840
841
842
843
844 public boolean isNewHibernateSession() {
845
846
847 return this.parentSession == null;
848 }
849
850
851
852
853
854
855 public boolean isReadonly() {
856 return this.getGrouperTransactionType().isReadonly();
857 }
858
859
860
861
862
863
864 private HibernateSession activeHibernateSession() {
865 return this.parentSession == null ? this : this.parentSession
866 .activeHibernateSession();
867 }
868
869
870
871
872
873
874
875 public GrouperTransactionType getGrouperTransactionType() {
876 return this.activeHibernateSession().immediateGrouperTransactionTypeUsed;
877 }
878
879
880
881
882
883
884 public boolean commit(GrouperCommitType grouperCommitType) {
885
886 assertNotGrouperReadonly();
887
888 switch (grouperCommitType) {
889 case COMMIT_IF_NEW_TRANSACTION:
890 if (this.isNewHibernateSession()) {
891 LOG.debug("endTransactionCommitIfNew");
892 this.activeHibernateSession().immediateTransaction.commit();
893 this.activeHibernateSession().savepoint = null;
894 return true;
895 }
896 break;
897 case COMMIT_NOW:
898 LOG.debug("endTransactionCommitNow");
899 this.activeHibernateSession().immediateTransaction.commit();
900 this.activeHibernateSession().savepoint = null;
901 return true;
902 }
903 return false;
904 }
905
906
907
908
909
910
911 public boolean isTransactionActive() {
912 if (this.isReadonly()) {
913 return false;
914 }
915 return this.activeHibernateSession().immediateTransaction == null ? false : this
916 .activeHibernateSession().immediateTransaction.getStatus().isOneOf(TransactionStatus.ACTIVE);
917 }
918
919
920
921
922
923
924 public boolean rollback(GrouperRollbackType grouperRollbackType) {
925 switch (grouperRollbackType) {
926 case ROLLBACK_IF_NEW_TRANSACTION:
927 if (this.isNewHibernateSession()) {
928 LOG.debug("endTransactionRollbackIfNew");
929 this.activeHibernateSession().immediateTransaction.rollback();
930 return true;
931 }
932 break;
933 case ROLLBACK_NOW:
934 if (this.activeHibernateSession() != null && this.activeHibernateSession().immediateTransaction != null) {
935 LOG.debug("endTransactionRollbackNow");
936 this.activeHibernateSession().immediateTransaction.rollback();
937 }
938 return true;
939 }
940 return false;
941 }
942
943
944
945
946
947
948
949
950
951
952
953
954 List retrieveListBySql(String query, String aliasOfObject, Class types,
955 Object params, Object paramTypes) {
956
957 List list = null;
958
959 Query hibQuery = retrieveQueryBySql(query, aliasOfObject, types, params, paramTypes);
960
961 list = hibQuery.list();
962 if (list != null) {
963
964
965 if (types != null) {
966 HibUtils.evict(this, list, true);
967 }
968 }
969 return list;
970 }
971
972
973
974
975
976
977
978
979
980
981
982
983 @SuppressWarnings("unchecked") Query retrieveQueryBySql(String query, String aliasOfObject, Class types,
984 Object params, Object paramTypes) {
985
986 Query hibQuery = null;
987
988 try {
989
990 hibQuery = this.getSession().createSQLQuery(query);
991
992 if (aliasOfObject != null && types != null) {
993 hibQuery = ((SQLQuery)hibQuery).addEntity(aliasOfObject, types);
994 } else if (types != null) {
995
996
997 hibQuery = ((SQLQuery)hibQuery).addEntity(types);
998 }
999
1000 attachParams(hibQuery, params, paramTypes);
1001
1002
1003 } catch (Throwable he) {
1004 throw new RuntimeException("Error querying for array of objects, query: " + query + ", "
1005 + HibUtils.paramsToString(params, paramTypes), he);
1006 }
1007
1008 return hibQuery;
1009
1010 }
1011
1012
1013
1014
1015
1016
1017
1018
1019 @SuppressWarnings("deprecation")
1020 static void attachParams(Query query, Object params, Object types) {
1021
1022
1023 if (GrouperUtil.length(params) == 0 && GrouperUtil.length(types) == 0) {
1024 return;
1025 }
1026
1027 if (GrouperUtil.length(params) != GrouperUtil.length(types)) {
1028 throw new RuntimeException("The params length must equal the types length and params " +
1029 "and types must either both or neither be null");
1030 }
1031
1032 int paramLength = -1;
1033
1034
1035 List paramList = GrouperUtil.toList(params);
1036 List typeList = GrouperUtil.toList(types);
1037
1038
1039 paramLength = paramList.size();
1040
1041 if (paramLength != typeList.size()) {
1042 throw new RuntimeException("The params length " + paramLength
1043 + " must equal the types length " + typeList.size());
1044 }
1045
1046
1047 for (int i = 0; i < paramLength; i++) {
1048
1049 Object param = paramList.get(i);
1050 Type type = (Type) typeList.get(i);
1051
1052
1053 if (type.equals(StandardBasicTypes.DATE)) {
1054 type = StandardBasicTypes.TIMESTAMP;
1055
1056 param = GrouperUtil.toTimestamp(param);
1057 }
1058
1059 query.setParameter(i, param, type);
1060 }
1061
1062
1063 }
1064
1065 }