1 21 package oracle.toplink.essentials.queryframework; 23 24 import java.util.*; 25 import oracle.toplink.essentials.exceptions.*; 26 import oracle.toplink.essentials.expressions.*; 27 import oracle.toplink.essentials.internal.expressions.*; 28 import oracle.toplink.essentials.internal.helper.*; 29 import oracle.toplink.essentials.internal.queryframework.*; 30 import oracle.toplink.essentials.mappings.*; 31 import oracle.toplink.essentials.querykeys.*; 32 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 33 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 34 import oracle.toplink.essentials.internal.sessions.AbstractSession; 35 import oracle.toplink.essentials.descriptors.ClassDescriptor; 36 37 38 48 public abstract class ObjectLevelReadQuery extends ObjectBuildingQuery { 49 50 51 protected ExpressionBuilder defaultBuilder; 52 53 54 protected boolean shouldRefreshIdentityMapResult; 55 56 57 protected int cacheUsage; 58 59 public static final int UseDescriptorSetting = -1; 62 public static final int DoNotCheckCache = 0; 63 public static final int CheckCacheByExactPrimaryKey = 1; 64 public static final int CheckCacheByPrimaryKey = 2; 65 public static final int CheckCacheThenDatabase = 3; 66 public static final int CheckCacheOnly = 4; 67 public static final int ConformResultsInUnitOfWork = 5; 68 69 70 protected boolean shouldRegisterResultsInUnitOfWork = true; 71 72 73 protected Vector additionalFields; 74 75 76 protected boolean shouldIncludeData; 77 78 79 protected boolean shouldProcessResultsInUnitOfWork = true; 80 81 82 protected short distinctState; 83 public static final short UNCOMPUTED_DISTINCT = 0; 84 public static final short USE_DISTINCT = 1; 85 public static final short DONT_USE_DISTINCT = 2; 86 87 93 protected InMemoryQueryIndirectionPolicy inMemoryQueryIndirectionPolicy; 94 95 99 protected long executionTime = 0; 100 101 105 public static final String LOCK_RESULT_PROPERTY = "LOCK_RESULT"; 106 107 108 protected FetchGroup fetchGroup; 109 110 111 protected String fetchGroupName; 112 113 114 protected boolean shouldUseDefaultFetchGroup = true; 115 116 117 protected boolean wasDefaultLockMode = false; 118 119 121 protected Vector nonFetchJoinAttributeExpressions; 122 123 124 protected JoinedAttributeManager joinedAttributeManager; 125 126 130 public ObjectLevelReadQuery() { 131 this.shouldRefreshIdentityMapResult = false; 132 this.distinctState = UNCOMPUTED_DISTINCT; 133 this.joinedAttributeManager = new JoinedAttributeManager(getDescriptor(), getExpressionBuilder(), this); 134 this.additionalFields = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(1); 135 this.cacheUsage = UseDescriptorSetting; 136 this.shouldIncludeData = false; 137 this.inMemoryQueryIndirectionPolicy = new InMemoryQueryIndirectionPolicy(); 138 } 139 140 144 protected boolean wasDefaultLockMode() { 145 return wasDefaultLockMode; 146 } 147 148 152 protected void setWasDefaultLockMode(boolean wasDefaultLockMode) { 153 this.wasDefaultLockMode = wasDefaultLockMode; 154 } 155 156 160 public Object clone() { 161 ObjectLevelReadQuery cloneQuery = (ObjectLevelReadQuery)super.clone(); 162 163 cloneQuery.joinedAttributeManager = (JoinedAttributeManager)cloneQuery.getJoinedAttributeManager().clone(); 166 if (hasNonFetchJoinedAttributeExpressions()){ 167 cloneQuery.setNonFetchJoinAttributeExpressions((Vector)this.nonFetchJoinAttributeExpressions.clone()); 168 } 169 cloneQuery.joinedAttributeManager.setBaseQuery(cloneQuery); 170 return cloneQuery; 171 } 172 173 180 public Object deepClone() { 181 ObjectLevelReadQuery clone = (ObjectLevelReadQuery)clone(); 182 if (getSelectionCriteria() != null) { 183 clone.setSelectionCriteria((Expression)getSelectionCriteria().clone()); 184 } else if (defaultBuilder != null) { 185 clone.defaultBuilder = (ExpressionBuilder)defaultBuilder.clone(); 186 } 187 return clone; 188 } 189 190 194 public void acquireLocks() { 195 setLockMode(LOCK); 196 setIsPrepared(false); 198 } 199 200 204 public void acquireLocksWithoutWaiting() { 205 setLockMode(LOCK_NOWAIT); 206 setIsPrepared(false); 208 } 209 210 214 public void addAdditionalField(DatabaseField field) { 215 getAdditionalFields().addElement(field); 216 setIsPrepared(false); 218 } 219 220 224 public void addAdditionalField(Expression fieldExpression) { 225 getAdditionalFields().addElement(fieldExpression); 226 setIsPrepared(false); 228 } 229 230 247 public void addJoinedAttribute(String attributeName) { 248 addJoinedAttribute(getExpressionBuilder().get(attributeName)); 249 } 250 251 285 public void addJoinedAttribute(Expression attributeExpression) { 286 getJoinedAttributeManager().addJoinedAttributeExpression(attributeExpression); 287 setIsPrePrepared(false); 290 } 291 292 314 public void addNonFetchJoinedAttribute(String attributeName) { 315 addNonFetchJoinedAttribute(getExpressionBuilder().get(attributeName)); 316 } 317 318 340 public void addNonFetchJoinedAttribute(Expression attributeExpression) { 341 getNonFetchJoinAttributeExpressions().add(attributeExpression); 342 343 setIsPrePrepared(false); 346 } 347 348 353 protected void addSelectionFieldsForJoinedExpressions(List fields, List joinedExpressions) { 354 for (int index = 0; index < joinedExpressions.size(); index++) { 355 ObjectExpression objectExpression = (ObjectExpression)joinedExpressions.get(index); 356 357 objectExpression.getBuilder().setSession(getSession()); 359 objectExpression.getBuilder().setQueryClass(getReferenceClass()); 360 ClassDescriptor descriptor = objectExpression.getMapping().getReferenceDescriptor(); 361 fields.addAll(descriptor.getFields()); 362 } 363 } 364 365 370 public Object buildObject(AbstractRecord row) { 371 return getDescriptor().getObjectBuilder().buildObject(this, row, this.getJoinedAttributeManager()); 372 } 373 374 381 public void checkCacheOnly() { 382 setCacheUsage(CheckCacheOnly); 383 } 384 385 389 public void checkDescriptor(AbstractSession session) throws QueryException { 390 if (getReferenceClass() == null) { 391 throw QueryException.referenceClassMissing(this); 392 } 393 394 if (getDescriptor() == null) { 395 ClassDescriptor referenceDescriptor = session.getDescriptor(getReferenceClass()); 396 if (referenceDescriptor == null) { 397 throw QueryException.descriptorIsMissing(getReferenceClass(), this); 398 } 399 setDescriptor(referenceDescriptor); 400 } 401 } 402 403 407 protected abstract Object checkEarlyReturnImpl(AbstractSession session, AbstractRecord translationRow); 408 409 413 public Object checkEarlyReturn(AbstractSession session, AbstractRecord translationRow) { 414 checkPrePrepare(session); 419 420 if (!session.isUnitOfWork()) { 421 return checkEarlyReturnImpl(session, translationRow); 422 } 423 UnitOfWorkImpl unitOfWork = (UnitOfWorkImpl)session; 424 425 if (shouldCheckCacheOnly() || shouldConformResultsInUnitOfWork() || getDescriptor().shouldAlwaysConformResultsInUnitOfWork() || (getLockMode() != ObjectBuildingQuery.NO_LOCK)) { 429 Object result = checkEarlyReturnImpl(unitOfWork, translationRow); 430 if (result != null) { 431 return result; 432 } 433 } 434 435 if (isReadObjectQuery() && isLockQuery()) { 440 return null; 441 } 442 443 AbstractSession parentSession = unitOfWork.getParentIdentityMapSession(this); 445 446 Object result = checkEarlyReturn(parentSession, translationRow); 448 449 if (result != null) { 450 if (result == InvalidObject.instance) { 453 return result; 454 } 455 return registerResultInUnitOfWork(result, unitOfWork, translationRow, false); 456 } else { 457 return null; 458 } 459 } 460 461 466 public void checkPrepare(AbstractSession session, AbstractRecord translationRow) { 467 checkPrePrepare(session); 469 super.checkPrepare(session, translationRow); 470 } 471 472 478 protected void checkPrePrepare(AbstractSession session) { 479 checkDescriptor(session); 480 if (!isPrePrepared()) { synchronized (this) { 483 if (!isPrePrepared()) { 484 AbstractSession alreadySetSession = getSession(); 485 setSession(session); prePrepare(); 487 setSession(alreadySetSession); 488 setIsPrePrepared(true); } 490 } 491 } 492 } 493 494 501 public void changeDescriptor(AbstractSession theSession) { 502 setDescriptor(null); 503 checkDescriptor(theSession); 504 } 505 506 528 protected Object conformIndividualResult(Object result, UnitOfWorkImpl unitOfWork, AbstractRecord arguments, Expression selectionCriteriaClone, IdentityHashtable alreadyReturned, boolean buildDirectlyFromRows) { 529 Object clone = registerIndividualResult(result, unitOfWork, buildDirectlyFromRows, null); 533 534 if (getDescriptor().hasWrapperPolicy() && getDescriptor().getWrapperPolicy().isWrapped(clone)) { 535 return clone; 540 } 541 if (unitOfWork.isObjectDeleted(clone) ) { 545 return null; 546 } 547 if (!isExpressionQuery() || (selectionCriteriaClone == null)) { 548 if (alreadyReturned != null) { 549 alreadyReturned.remove(clone); 550 } 551 return clone; 552 } 553 try { 554 InMemoryQueryIndirectionPolicy policy = getInMemoryQueryIndirectionPolicy(); 561 if (!policy.shouldTriggerIndirection()) { 562 policy = new InMemoryQueryIndirectionPolicy(InMemoryQueryIndirectionPolicy.SHOULD_IGNORE_EXCEPTION_RETURN_CONFORMED); 563 } 564 if (selectionCriteriaClone.doesConform(clone, unitOfWork, arguments, policy)) { 565 if (alreadyReturned != null) { 566 alreadyReturned.remove(clone); 567 } 568 return clone; 569 } 570 } catch (QueryException exception) { 571 if ((unitOfWork.getShouldThrowConformExceptions() == UnitOfWorkImpl.THROW_ALL_CONFORM_EXCEPTIONS) && (exception.getErrorCode() != QueryException.MUST_INSTANTIATE_VALUEHOLDERS)) { 573 throw exception; 574 } 575 if (alreadyReturned != null) { 576 alreadyReturned.remove(clone); 577 } 578 return clone; 579 } 580 return null; 581 } 582 583 592 public void conformResultsInUnitOfWork() { 593 setCacheUsage(ConformResultsInUnitOfWork); 594 } 595 596 600 public void dontAcquireLocks() { 601 setLockMode(NO_LOCK); 602 setIsPrepared(false); 604 } 605 606 612 public void dontCheckCache() { 613 setCacheUsage(DoNotCheckCache); 614 } 615 616 620 public void dontRefreshIdentityMapResult() { 621 setShouldRefreshIdentityMapResult(false); 622 } 623 624 630 public void dontUseDistinct() { 631 setDistinctState(DONT_USE_DISTINCT); 632 setIsPrepared(false); 634 } 635 636 646 public DatabaseQuery prepareOutsideUnitOfWork(AbstractSession session) { 647 if (isLockQuery(session) && getLockingClause().isForUpdateOfClause()) { 653 ObjectLevelReadQuery clone = (ObjectLevelReadQuery)clone(); 654 clone.dontAcquireLocks(); 655 clone.setIsPrepared(false); 656 clone.checkPrePrepare(session); 657 return clone; 658 } 659 return this; 660 } 661 662 672 public Object execute(AbstractSession session, AbstractRecord translationRow) throws DatabaseException, OptimisticLockException { 673 if (shouldRefreshIdentityMapResult() && shouldCheckCacheOnly()) { 675 throw QueryException.refreshNotPossibleWithCheckCacheOnly(this); 676 } 677 return super.execute(session, translationRow); 678 } 679 680 683 public Object executeDatabaseQuery() throws DatabaseException { 684 if (getSession().isUnitOfWork()) { 685 UnitOfWorkImpl unitOfWork = (UnitOfWorkImpl)getSession(); 686 687 if (isLockQuery()) { 690 if ((!unitOfWork.getCommitManager().isActive()) && (!unitOfWork.wasTransactionBegunPrematurely())) { 691 unitOfWork.beginTransaction(); 692 unitOfWork.setWasTransactionBegunPrematurely(true); 693 } 694 } 695 if (unitOfWork.isNestedUnitOfWork()) { 696 UnitOfWorkImpl nestedUnitOfWork = (UnitOfWorkImpl)getSession(); 697 setSession(nestedUnitOfWork.getParent()); 698 Object result = executeDatabaseQuery(); 699 setSession(nestedUnitOfWork); 700 return registerResultInUnitOfWork(result, nestedUnitOfWork, getTranslationRow(), false); 701 } 702 } 703 session.validateQuery(this); 705 if (getQueryId() == 0) { 706 setQueryId(getSession().getNextQueryId()); 707 } 708 709 return executeObjectLevelReadQuery(); 710 } 711 712 715 protected abstract Object executeObjectLevelReadQuery() throws DatabaseException; 716 717 727 public Object executeInUnitOfWork(UnitOfWorkImpl unitOfWork, AbstractRecord translationRow) throws DatabaseException, OptimisticLockException { 728 if (!shouldMaintainCache()) { 729 return unitOfWork.getParent().executeQuery(this, translationRow); 730 } 731 Object result = execute(unitOfWork, translationRow); 732 733 if (result == InvalidObject.instance) { 736 return null; 737 } 738 return result; 739 } 740 741 745 public Vector getAdditionalFields() { 746 return additionalFields; 747 } 748 749 767 public int getCacheUsage() { 768 return cacheUsage; 769 } 770 771 777 public short getDistinctState() { 778 return distinctState; 779 } 780 781 786 public ExpressionBuilder getExpressionBuilder() { 787 if (defaultBuilder == null) { 788 initializeDefaultBuilder(); 789 } 790 791 return defaultBuilder; 792 } 793 794 798 public void setExpressionBuilder(ExpressionBuilder builder) { 799 this.defaultBuilder = builder; 800 } 801 802 806 public InMemoryQueryIndirectionPolicy getInMemoryQueryIndirectionPolicy() { 807 return this.inMemoryQueryIndirectionPolicy; 808 } 809 810 815 public JoinedAttributeManager getJoinedAttributeManager() { 816 return this.joinedAttributeManager; 817 } 818 819 824 public Vector getNonFetchJoinAttributeExpressions() { 825 if (this.nonFetchJoinAttributeExpressions == null){ 826 this.nonFetchJoinAttributeExpressions = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(); 827 } 828 return nonFetchJoinAttributeExpressions; 829 } 830 831 836 public DatabaseMapping getLeafMappingFor(Expression expression, ClassDescriptor rootDescriptor) throws QueryException { 837 if ((expression == null) || (expression.isFieldExpression())) { 839 return null; 840 } 841 842 if (!(expression.isQueryKeyExpression())) { 843 return null; 844 } 845 846 QueryKeyExpression qkExpression = (QueryKeyExpression)expression; 847 Expression baseExpression = qkExpression.getBaseExpression(); 848 849 ClassDescriptor descriptor = getLeafDescriptorFor(baseExpression, rootDescriptor); 850 return descriptor.getMappingForAttributeName(qkExpression.getName()); 851 } 852 853 861 public ClassDescriptor getLeafDescriptorFor(Expression expression, ClassDescriptor rootDescriptor) throws QueryException { 862 if (expression.isExpressionBuilder()) { 864 Class queryClass = ((ExpressionBuilder)expression).getQueryClass(); 867 if ((queryClass != null) && (queryClass != getReferenceClass())) { 868 return getSession().getDescriptor(queryClass); 869 } 870 return rootDescriptor; 871 } 872 Expression baseExpression = ((QueryKeyExpression)expression).getBaseExpression(); 873 ClassDescriptor baseDescriptor = getLeafDescriptorFor(baseExpression, rootDescriptor); 874 ClassDescriptor descriptor = null; 875 String attributeName = expression.getName(); 876 877 DatabaseMapping mapping = baseDescriptor.getMappingForAttributeName(attributeName); 878 879 if (mapping == null) { 880 QueryKey queryKey = baseDescriptor.getQueryKeyNamed(attributeName); 881 if (queryKey != null) { 882 if (queryKey.isForeignReferenceQueryKey()) { 883 descriptor = getSession().getDescriptor(((ForeignReferenceQueryKey)queryKey).getReferenceClass()); 884 } else { 886 descriptor = queryKey.getDescriptor(); 887 } 888 } 889 if (descriptor == null) { 890 throw QueryException.invalidExpressionForQueryItem(expression, this); 891 } 892 } else if (mapping.isAggregateObjectMapping()) { 893 descriptor = ((AggregateObjectMapping)mapping).getReferenceDescriptor(); 894 } else if (mapping.isForeignReferenceMapping()) { 895 descriptor = ((ForeignReferenceMapping)mapping).getReferenceDescriptor(); 896 } 897 return descriptor; 898 } 899 900 904 public short getLockMode() { 905 if (lockingClause == null) { 906 return DEFAULT_LOCK_MODE; 907 } else { 908 return lockingClause.getLockMode(); 909 } 910 } 911 912 918 public ForUpdateClause getLockingClause() { 919 return lockingClause; 920 } 921 922 926 public long getExecutionTime() { 927 return executionTime; 928 } 929 930 934 public Class getReferenceClass() { 937 return super.getReferenceClass(); 938 } 939 940 944 public String getReferenceClassName() { 947 return super.getReferenceClassName(); 948 } 949 950 955 public boolean hasAsOfClause() { 956 return ((defaultBuilder != null) && defaultBuilder.hasAsOfClause()); 957 } 958 959 963 public boolean hasNonFetchJoinedAttributeExpressions() { 964 return this.nonFetchJoinAttributeExpressions != null && !this.nonFetchJoinAttributeExpressions.isEmpty(); 965 } 966 967 971 public boolean hasPartialAttributeExpressions() { 972 return false; 973 } 974 975 981 public Vector getSelectionFields() { 982 if ((!hasPartialAttributeExpressions()) && (!getJoinedAttributeManager().hasJoinedAttributes()) && (!hasFetchGroupAttributeExpressions())) { 983 return getDescriptor().getAllFields(); 984 } 985 Vector fields; 986 if (hasFetchGroupAttributeExpressions()) { List fetchGroupAttrExps = getFetchGroup().getFetchGroupAttributeExpressions(); 988 fields = new Vector(fetchGroupAttrExps.size()); 989 for (int index = 0; index < fetchGroupAttrExps.size(); index++) { 990 Expression expression = (Expression)fetchGroupAttrExps.get(index); 991 992 expression.getBuilder().setSession(getSession()); 994 expression.getBuilder().setQueryClass(getReferenceClass()); 995 Helper.addAllToVector(fields, expression.getFields()); 996 } 997 } else { 998 fields = new Vector(getDescriptor().getAllFields().size() + getJoinedAttributeManager().getJoinedAttributeExpressions().size() + getJoinedAttributeManager().getJoinedMappingExpressions().size()); 999 Helper.addAllToVector(fields, getDescriptor().getAllFields()); 1000 addSelectionFieldsForJoinedExpressions(fields, getJoinedAttributeManager().getJoinedAttributeExpressions()); 1001 addSelectionFieldsForJoinedExpressions(fields, getJoinedAttributeManager().getJoinedMappingExpressions()); 1002 } 1003 return fields; 1004 } 1005 1006 1011 protected void initializeDefaultBuilder() { 1012 DatabaseQueryMechanism mech = getQueryMechanism(); 1013 if (mech.isExpressionQueryMechanism() && ((ExpressionQueryMechanism)mech).getExpressionBuilder() != null) { 1014 this.defaultBuilder = ((ExpressionQueryMechanism)mech).getExpressionBuilder(); 1015 return; 1016 } 1017 this.defaultBuilder = new ExpressionBuilder(); 1018 } 1019 1020 1024 public boolean isDistinctComputed() { 1025 return getDistinctState() != UNCOMPUTED_DISTINCT; 1026 } 1027 1028 1039 public boolean isLockQuery() { 1040 return getLockMode() > NO_LOCK; 1041 } 1042 1043 1052 public boolean isLockQuery(oracle.toplink.essentials.sessions.Session session) { 1053 checkPrePrepare((AbstractSession)session); 1054 return isLockQuery(); 1055 } 1056 1057 1061 public boolean isObjectLevelReadQuery() { 1062 return true; 1063 } 1064 1065 1069 protected boolean isPrePrepared() { 1070 return isPrePrepared; 1071 } 1072 1073 1079 protected boolean isRegisteringResults() { 1080 return ((shouldRegisterResultsInUnitOfWork() && getDescriptor().shouldRegisterResultsInUnitOfWork()) || isLockQuery()); 1081 } 1082 1083 1093 protected void setIsPrePrepared(boolean isPrePrepared) { 1094 if (this.isPrePrepared && !isPrePrepared) { 1096 setIsPrepared(false); 1097 this.getJoinedAttributeManager().reset(); 1098 } 1099 this.isPrePrepared = isPrePrepared; 1100 } 1101 1102 1106 protected void prepare() throws QueryException { 1107 super.prepare(); 1108 prepareQuery(); 1109 } 1110 1111 1115 protected void prePrepare() throws QueryException { 1116 buildSelectionCriteria(session); 1119 checkDescriptor(session); 1120 1121 if (getQueryMechanism().isExpressionQueryMechanism()) { 1123 getJoinedAttributeManager().processJoinedMappings(); 1124 } 1125 1126 if (isDefaultLock()) { 1128 setWasDefaultLockMode(true); 1129 ForUpdateOfClause lockingClause = null; 1130 if (getJoinedAttributeManager().hasJoinedExpressions()) { 1131 lockingClause = getJoinedAttributeManager().setupLockingClauseForJoinedExpressions(lockingClause, getSession()); 1132 } 1133 if (lockingClause == null) { 1134 this.lockingClause = ForUpdateClause.newInstance(NO_LOCK); 1135 } else { 1136 this.lockingClause = lockingClause; 1137 dontUseDistinct(); 1139 } 1140 } else if (getLockMode() == NO_LOCK) { 1141 setWasDefaultLockMode(true); 1142 } 1143 } 1144 1145 1149 protected void prepareQuery() throws QueryException { 1150 if ((!shouldMaintainCache()) && shouldRefreshIdentityMapResult() && (!descriptor.isAggregateCollectionDescriptor())) { 1151 throw QueryException.refreshNotPossibleWithoutCache(this); 1152 } 1153 if (shouldMaintainCache() && hasPartialAttributeExpressions()) { 1154 throw QueryException.cannotCachePartialObjects(this); 1155 } 1156 1157 if (descriptor.isAggregateDescriptor()) { 1158 throw QueryException.aggregateObjectCannotBeDeletedOrWritten(descriptor, this); 1160 } 1161 1162 if ((!getDescriptor().hasFetchGroupManager()) && ((getFetchGroup() != null) || (getFetchGroupName() != null))) { 1164 throw QueryException.fetchGroupValidOnlyIfFetchGroupManagerInDescriptor(getDescriptor().getJavaClassName(), getName()); 1165 } 1166 1167 if (getDescriptor().hasFetchGroupManager()) { 1169 getDescriptor().getFetchGroupManager().prepareQueryWithFetchGroup(this); 1170 } 1171 1172 if (getJoinedAttributeManager().hasJoinedExpressions()) { 1174 getJoinedAttributeManager().prepareJoinExpressions(getSession()); 1175 } else { 1176 getJoinedAttributeManager().reset(); 1178 } 1179 } 1180 1181 1186 public void refreshIdentityMapResult() { 1187 setShouldRefreshIdentityMapResult(true); 1188 } 1189 1190 1210 public abstract Object registerResultInUnitOfWork(Object result, UnitOfWorkImpl unitOfWork, AbstractRecord arguments, boolean buildDirectlyFromRows); 1211 1212 1218 public void resetDistinct() { 1219 setDistinctState(UNCOMPUTED_DISTINCT); 1220 setIsPrepared(false); 1222 } 1223 1224 1228 public void setAdditionalFields(Vector additionalFields) { 1229 this.additionalFields = additionalFields; 1230 } 1231 1232 1249 public void setCacheUsage(int cacheUsage) { 1250 this.cacheUsage = cacheUsage; 1251 } 1252 1253 1257 public void setDescriptor(ClassDescriptor descriptor) { 1258 super.setDescriptor(descriptor); 1259 if (this.joinedAttributeManager != null){ 1260 this.joinedAttributeManager.setDescriptor(descriptor); 1261 } 1262 } 1263 1264 1270 public void setDistinctState(short distinctState) { 1271 this.distinctState = distinctState; 1272 } 1273 1274 1278 public void setExecutionTime(long executionTime) { 1279 this.executionTime = executionTime; 1280 } 1281 1282 1286 public void setInMemoryQueryIndirectionPolicy(InMemoryQueryIndirectionPolicy inMemoryQueryIndirectionPolicy) { 1288 if (inMemoryQueryIndirectionPolicy != null) { 1290 this.inMemoryQueryIndirectionPolicy = inMemoryQueryIndirectionPolicy; 1291 } 1292 } 1293 1294 1316 public void setLockMode(short lockMode) { 1317 if ((lockMode == LOCK) || (lockMode == LOCK_NOWAIT)) { 1318 lockingClause = ForUpdateClause.newInstance(lockMode); 1319 setShouldRefreshIdentityMapResult(true); 1320 } else if (lockMode == NO_LOCK) { 1321 lockingClause = ForUpdateClause.newInstance(lockMode); 1322 } else { 1323 lockingClause = null; 1324 setIsPrePrepared(false); 1325 } 1326 setIsPrepared(false); 1327 } 1328 1329 1334 protected void setNonFetchJoinAttributeExpressions(Vector nonFetchJoinExpressions) { 1335 this.nonFetchJoinAttributeExpressions = nonFetchJoinExpressions; 1336 } 1337 1338 1346 public void setLockingClause(ForUpdateClause clause) { 1347 if (clause.isForUpdateOfClause()) { 1348 this.lockingClause = clause; 1349 setIsPrePrepared(false); 1350 } else { 1351 setLockMode(clause.getLockMode()); 1352 } 1353 } 1354 1355 public void setEJBQLString(String ejbqlString) { 1356 super.setEJBQLString(ejbqlString); 1357 setIsPrePrepared(false); 1358 } 1359 1360 1364 public void setReferenceClass(Class aClass) { 1367 super.setReferenceClass(aClass); 1368 } 1369 1370 1374 public void setReferenceClassName(String aClass) { 1377 super.setReferenceClassName(aClass); 1378 } 1379 1380 public void setSelectionCriteria(Expression expression) { 1381 super.setSelectionCriteria(expression); 1382 if ((expression != null) && (defaultBuilder != null)) { 1383 ExpressionBuilder newBuilder = expression.getBuilder(); 1385 if ( (newBuilder != defaultBuilder) && (newBuilder.getQueryClass() == null || newBuilder.getQueryClass().equals(defaultBuilder.getQueryClass()) )){ 1386 defaultBuilder = newBuilder; 1387 } 1388 } 1389 } 1390 1391 1396 public void setShouldIncludeData(boolean shouldIncludeData) { 1397 this.shouldIncludeData = shouldIncludeData; 1398 } 1399 1400 1405 public void setShouldRefreshIdentityMapResult(boolean shouldRefreshIdentityMapResult) { 1406 this.shouldRefreshIdentityMapResult = shouldRefreshIdentityMapResult; 1407 } 1408 1409 1416 public void setShouldRegisterResultsInUnitOfWork(boolean shouldRegisterResultsInUnitOfWork) { 1417 this.shouldRegisterResultsInUnitOfWork = shouldRegisterResultsInUnitOfWork; 1418 } 1419 1420 1424 public boolean shouldCheckCacheOnly() { 1425 return getCacheUsage() == CheckCacheOnly; 1426 } 1427 1428 1433 public boolean shouldCheckDescriptorForCacheUsage() { 1434 return getCacheUsage() == UseDescriptorSetting; 1435 } 1436 1437 1441 public boolean shouldConformResultsInUnitOfWork() { 1442 return getCacheUsage() == ConformResultsInUnitOfWork; 1443 } 1444 1445 1449 public boolean shouldDistinctBeUsed() { 1450 return getDistinctState() == USE_DISTINCT; 1451 } 1452 1453 1458 public boolean shouldIncludeData() { 1459 return shouldIncludeData; 1460 } 1461 1462 1477 public boolean shouldRegisterResultsInUnitOfWork() { 1478 return shouldRegisterResultsInUnitOfWork; 1479 } 1480 1481 1485 public boolean shouldReadAllMappings() { 1486 return (!hasPartialAttributeExpressions()) && (!hasFetchGroupAttributeExpressions()); 1487 } 1488 1489 1493 public boolean shouldReadMapping(DatabaseMapping mapping) { 1494 if ((!hasPartialAttributeExpressions()) && (!hasFetchGroupAttributeExpressions())) { 1495 return true; 1496 } 1497 1498 if (hasFetchGroupAttributeExpressions()) { 1500 return isFetchGroupAttribute(mapping.getAttributeName()); 1501 } 1502 1503 return true; 1504 } 1505 1506 1511 public boolean shouldRefreshIdentityMapResult() { 1512 return shouldRefreshIdentityMapResult; 1513 } 1514 1515 public String toString() { 1516 if (getReferenceClass() == null) { 1517 return super.toString(); 1518 } 1519 return Helper.getShortClassName(getClass()) + "(" + getReferenceClass().getName() + ")"; 1520 } 1521 1522 1539 public void setShouldProcessResultsInUnitOfWork(boolean processResultsInUnitOfWork) { 1540 this.shouldProcessResultsInUnitOfWork = processResultsInUnitOfWork; 1541 } 1542 1543 1560 public boolean shouldProcessResultsInUnitOfWork() { 1561 return this.shouldProcessResultsInUnitOfWork; 1562 } 1563 1564 1570 public void useDistinct() { 1571 setDistinctState(USE_DISTINCT); 1572 setIsPrepared(false); 1574 } 1575 1576 1580 public boolean isClonePessimisticLocked(Object clone, UnitOfWorkImpl uow) { 1581 return false; 1582 } 1583 1584 1588 public void recordCloneForPessimisticLocking(Object clone, UnitOfWorkImpl uow) { 1589 if ((isLockQuery()) && lockingClause.isReferenceClassLocked()) { 1590 uow.addPessimisticLockedClone(clone); 1591 } 1592 } 1593 1594 1598 public boolean isDefaultLock() { 1599 return (lockingClause == null); 1600 } 1601 1602 1607 public FetchGroup getFetchGroup() { 1608 return fetchGroup; 1609 } 1610 1611 1615 public void initializeFetchGroup() { 1616 if (fetchGroup != null) { 1617 return; 1619 } 1620 1621 if (fetchGroupName != null) { fetchGroup = getDescriptor().getFetchGroupManager().getFetchGroup(fetchGroupName); 1624 if (fetchGroup == null) { 1625 throw QueryException.fetchGroupNotDefinedInDescriptor(fetchGroupName); 1627 } 1628 } else { if (shouldUseDefaultFetchGroup()) { 1631 fetchGroup = getDescriptor().getDefaultFetchGroup(); 1632 } 1633 } 1634 } 1635 1636 1639 public void setFetchGroup(FetchGroup newFetchGroup) { 1640 fetchGroup = newFetchGroup; 1641 } 1642 1643 1646 public void setFetchGroupName(String groupName) { 1647 fetchGroup = null; 1649 fetchGroupName = groupName; 1650 } 1651 1652 1655 public String getFetchGroupName() { 1656 return fetchGroupName; 1657 } 1658 1659 1662 public boolean shouldUseDefaultFetchGroup() { 1663 return shouldUseDefaultFetchGroup; 1664 } 1665 1666 1669 public void setShouldUseDefaultFetchGroup(boolean shouldUseDefaultFetchGroup) { 1670 this.shouldUseDefaultFetchGroup = shouldUseDefaultFetchGroup; 1671 } 1672 1673 1677 public boolean hasFetchGroupAttributeExpressions() { 1678 return (getFetchGroup() != null) && (getFetchGroup().hasFetchGroupAttributeExpressions()); 1679 } 1680 1681 1685 public boolean isFetchGroupAttribute(String attributeName) { 1686 if (getFetchGroup() == null) { 1687 return true; 1689 } 1690 return getFetchGroup().getAttributes().contains(attributeName); 1691 } 1692} 1693 | Popular Tags |