Source Code Cross Referenced for ObjectLevelReadQuery.java in  » Database-ORM » toplink » oracle » toplink » essentials » queryframework » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » toplink » oracle.toplink.essentials.queryframework 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003:         * 
0004:         * // Copyright (c) 1998, 2007, Oracle. All rights reserved.
0005:         * 
0006:         *
0007:         * The contents of this file are subject to the terms of either the GNU
0008:         * General Public License Version 2 only ("GPL") or the Common Development
0009:         * and Distribution License("CDDL") (collectively, the "License").  You
0010:         * may not use this file except in compliance with the License. You can obtain
0011:         * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
0012:         * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
0013:         * language governing permissions and limitations under the License.
0014:         * 
0015:         * When distributing the software, include this License Header Notice in each
0016:         * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
0017:         * Sun designates this particular file as subject to the "Classpath" exception
0018:         * as provided by Sun in the GPL Version 2 section of the License file that
0019:         * accompanied this code.  If applicable, add the following below the License
0020:         * Header, with the fields enclosed by brackets [] replaced by your own
0021:         * identifying information: "Portions Copyrighted [year]
0022:         * [name of copyright owner]"
0023:         * 
0024:         * Contributor(s):
0025:         * 
0026:         * If you wish your version of this file to be governed by only the CDDL or
0027:         * only the GPL Version 2, indicate your decision by adding "[Contributor]
0028:         * elects to include this software in this distribution under the [CDDL or GPL
0029:         * Version 2] license."  If you don't indicate a single choice of license, a
0030:         * recipient has the option to distribute your version of this file under
0031:         * either the CDDL, the GPL Version 2 or to extend the choice of license to
0032:         * its licensees as provided above.  However, if you add GPL Version 2 code
0033:         * and therefore, elected the GPL Version 2 license, then the option applies
0034:         * only if the new code is made subject to such option by the copyright
0035:         * holder.
0036:         */
0037:        package oracle.toplink.essentials.queryframework;
0038:
0039:        import java.util.*;
0040:        import oracle.toplink.essentials.exceptions.*;
0041:        import oracle.toplink.essentials.expressions.*;
0042:        import oracle.toplink.essentials.internal.expressions.*;
0043:        import oracle.toplink.essentials.internal.helper.*;
0044:        import oracle.toplink.essentials.internal.queryframework.*;
0045:        import oracle.toplink.essentials.mappings.*;
0046:        import oracle.toplink.essentials.querykeys.*;
0047:        import oracle.toplink.essentials.internal.sessions.AbstractRecord;
0048:        import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl;
0049:        import oracle.toplink.essentials.internal.sessions.AbstractSession;
0050:        import oracle.toplink.essentials.descriptors.ClassDescriptor;
0051:
0052:        /**
0053:         * <p><b>Purpose</b>:
0054:         * Abstract class for all read queries using objects.
0055:         *
0056:         * <p><b>Description</b>:
0057:         * Contains common behavior for all read queries using objects.
0058:         *
0059:         * @author Yvon Lavoie
0060:         * @since TOPLink/Java 1.0
0061:         */
0062:        public abstract class ObjectLevelReadQuery extends ObjectBuildingQuery {
0063:
0064:            /** Provide a default builder so that it's easier to be consistent */
0065:            protected ExpressionBuilder defaultBuilder;
0066:
0067:            /** Allows for the resulting objects to be refresh with the data from the database. */
0068:            protected boolean shouldRefreshIdentityMapResult;
0069:
0070:            /** Allow for the cache usage to be specified to enable in-memory querying. */
0071:            protected int cacheUsage;
0072:
0073:            // Note: UseDescriptorSetting will result in CheckCacheByPrimaryKey for most cases
0074:            // it simply allows the Descriptor's disable cache hits to be used
0075:            public static final int UseDescriptorSetting = -1;
0076:            public static final int DoNotCheckCache = 0;
0077:            public static final int CheckCacheByExactPrimaryKey = 1;
0078:            public static final int CheckCacheByPrimaryKey = 2;
0079:            public static final int CheckCacheThenDatabase = 3;
0080:            public static final int CheckCacheOnly = 4;
0081:            public static final int ConformResultsInUnitOfWork = 5;
0082:
0083:            /** INTERNAL: for bug 2612601 allow ability not to register results in UOW. */
0084:            protected boolean shouldRegisterResultsInUnitOfWork = true;
0085:
0086:            /** Allow for additional fields to be selected, used for m-m batch reading. */
0087:            protected Vector additionalFields;
0088:
0089:            /** Allow for a complex result to be return including the rows and objects, used for m-m batch reading. */
0090:            protected boolean shouldIncludeData;
0091:
0092:            /** CMP only. Allow users to configure whether finder should be executed in a uow or not. */
0093:            protected boolean shouldProcessResultsInUnitOfWork = true;
0094:
0095:            /** Indicates if distinct should be used or not. */
0096:            protected short distinctState;
0097:            public static final short UNCOMPUTED_DISTINCT = 0;
0098:            public static final short USE_DISTINCT = 1;
0099:            public static final short DONT_USE_DISTINCT = 2;
0100:
0101:            /**
0102:             * CR 3677
0103:             * Used to determine behaviour of indirection in InMemoryQuerying
0104:             * This should have been just a constant similar to distinct  locking, etc. instead of an object that just has the state and no behavoir,
0105:             * the object instantiation adds un-needed overhead, but too late now.
0106:             */
0107:            protected InMemoryQueryIndirectionPolicy inMemoryQueryIndirectionPolicy;
0108:
0109:            /**
0110:             * Used to set the read time on objects that use this query.
0111:             * Should be set to the time the query returned from the database.
0112:             */
0113:            protected long executionTime = 0;
0114:
0115:            /**
0116:             * INTERNAL:  This is the key for accessing unregistered and locked result in the query's properties.
0117:             * The uow and  QueryBaseValueHolder use this property to record amd to retreive the result respectively.
0118:             */
0119:            public static final String LOCK_RESULT_PROPERTY = "LOCK_RESULT";
0120:
0121:            /** Allow for a query level fetch group to be set. */
0122:            protected FetchGroup fetchGroup;
0123:
0124:            /** The pre-defined fetch group name. */
0125:            protected String fetchGroupName;
0126:
0127:            /** Flag to turn on/off the use of the default fetch group. */
0128:            protected boolean shouldUseDefaultFetchGroup = true;
0129:
0130:            /** PERF: Store if the query originally used the default lock mode. */
0131:            protected boolean wasDefaultLockMode = false;
0132:
0133:            /** Stores the non fetchjoin attributes, these are joins that will be
0134:             * represented in the where clause but not in the select */
0135:            protected Vector nonFetchJoinAttributeExpressions;
0136:
0137:            /** Stores the helper object for dealing with joined attributes */
0138:            protected JoinedAttributeManager joinedAttributeManager;
0139:
0140:            /**
0141:             * INTERNAL:
0142:             * Initialize the state of the query
0143:             */
0144:            public ObjectLevelReadQuery() {
0145:                this .shouldRefreshIdentityMapResult = false;
0146:                this .distinctState = UNCOMPUTED_DISTINCT;
0147:                this .joinedAttributeManager = new JoinedAttributeManager(
0148:                        getDescriptor(), getExpressionBuilder(), this );
0149:                this .additionalFields = oracle.toplink.essentials.internal.helper.NonSynchronizedVector
0150:                        .newInstance(1);
0151:                this .cacheUsage = UseDescriptorSetting;
0152:                this .shouldIncludeData = false;
0153:                this .inMemoryQueryIndirectionPolicy = new InMemoryQueryIndirectionPolicy();
0154:            }
0155:
0156:            /**
0157:             * INTERNAL:
0158:             * Return if this query originally used the default lock mode.
0159:             */
0160:            protected boolean wasDefaultLockMode() {
0161:                return wasDefaultLockMode;
0162:            }
0163:
0164:            /**
0165:             * INTERNAL:
0166:             * Set if this query originally used the default lock mode.
0167:             */
0168:            protected void setWasDefaultLockMode(boolean wasDefaultLockMode) {
0169:                this .wasDefaultLockMode = wasDefaultLockMode;
0170:            }
0171:
0172:            /**
0173:             * INTERNAL:
0174:             * Clone the query
0175:             */
0176:            public Object clone() {
0177:                ObjectLevelReadQuery cloneQuery = (ObjectLevelReadQuery) super 
0178:                        .clone();
0179:
0180:                //CR#... must also clone the joined expressions as always joined attribute will be added
0181:                // don't use setters as this will trigger unprepare.
0182:                cloneQuery.joinedAttributeManager = (JoinedAttributeManager) cloneQuery
0183:                        .getJoinedAttributeManager().clone();
0184:                if (hasNonFetchJoinedAttributeExpressions()) {
0185:                    cloneQuery
0186:                            .setNonFetchJoinAttributeExpressions((Vector) this .nonFetchJoinAttributeExpressions
0187:                                    .clone());
0188:                }
0189:                cloneQuery.joinedAttributeManager.setBaseQuery(cloneQuery);
0190:                return cloneQuery;
0191:            }
0192:
0193:            /**
0194:             * INTERNAL:
0195:             * Clone the query, including its selection criteria.
0196:             * <p>
0197:             * Normally selection criteria are not cloned here as they are cloned
0198:             * later on during prepare.
0199:             */
0200:            public Object deepClone() {
0201:                ObjectLevelReadQuery clone = (ObjectLevelReadQuery) clone();
0202:                if (getSelectionCriteria() != null) {
0203:                    clone
0204:                            .setSelectionCriteria((Expression) getSelectionCriteria()
0205:                                    .clone());
0206:                } else if (defaultBuilder != null) {
0207:                    clone.defaultBuilder = (ExpressionBuilder) defaultBuilder
0208:                            .clone();
0209:                }
0210:                return clone;
0211:            }
0212:
0213:            /**
0214:             * PUBLIC:
0215:             * Set the query to lock, this will also turn refreshCache on.
0216:             */
0217:            public void acquireLocks() {
0218:                setLockMode(LOCK);
0219:                //Bug2804042 Must un-prepare if prepared as the SQL may change.
0220:                setIsPrepared(false);
0221:            }
0222:
0223:            /**
0224:             * PUBLIC:
0225:             * Set the query to lock without waiting (blocking), this will also turn refreshCache on.
0226:             */
0227:            public void acquireLocksWithoutWaiting() {
0228:                setLockMode(LOCK_NOWAIT);
0229:                //Bug2804042 Must un-prepare if prepared as the SQL may change.
0230:                setIsPrepared(false);
0231:            }
0232:
0233:            /**
0234:             * INTERNAL:
0235:             * Additional fields can be added to a query.  This is used in m-m bacth reading to bring back the key from the join table.
0236:             */
0237:            public void addAdditionalField(DatabaseField field) {
0238:                getAdditionalFields().addElement(field);
0239:                //Bug2804042 Must un-prepare if prepared as the SQL may change.
0240:                setIsPrepared(false);
0241:            }
0242:
0243:            /**
0244:             * INTERNAL:
0245:             * Additional fields can be added to a query.  This is used in m-m bacth reading to bring back the key from the join table.
0246:             */
0247:            public void addAdditionalField(Expression fieldExpression) {
0248:                getAdditionalFields().addElement(fieldExpression);
0249:                //Bug2804042 Must un-prepare if prepared as the SQL may change.
0250:                setIsPrepared(false);
0251:            }
0252:
0253:            /**
0254:             * PUBLIC:
0255:             * Specify the one-to-one mapped attribute to be optimized in this query.
0256:             * The query will join the object(s) being read with the one-to-one attribute,
0257:             * this allows all of the data required for the object(s) to be read in a single query instead of (n) queries.
0258:             * This should be used when the application knows that it requires the part for all of the objects being read.
0259:             * This can be used only for one-to-one mappings where the target is not the same class as the source,
0260:             * either directly or through inheritance.  Also two joins cannot be done to the same class.
0261:             *
0262:             * <p>Note: This cannot be used for objects where it is possible not to have a part,
0263:             * as these objects will be ommited from the result set,
0264:             * unless an outer join is used through passing and expression using "getAllowingNull".
0265:             *
0266:             * <p>Example: query.addJoinedAttribute("address")
0267:             *
0268:             * @see #addJoinedAttribute(Expression)
0269:             */
0270:            public void addJoinedAttribute(String attributeName) {
0271:                addJoinedAttribute(getExpressionBuilder().get(attributeName));
0272:            }
0273:
0274:            /**
0275:             * PUBLIC:
0276:             * Specify the to-one or to-many mapped attribute to be optimized in this query.
0277:             * The query will join the object(s) being read with the specified attribute,
0278:             * this allows all of the data required for the object(s) to be read in a single query instead of (n) queries.
0279:             * This should be used when the application knows that it requires the part for all of the objects being read.
0280:             *
0281:             * <p>Note: This cannot be used for objects where it is possible not to have a part,
0282:             * as these objects will be ommited from the result set,
0283:             * unless an outer join is used through passing and expression using "getAllowingNull".
0284:             *
0285:             * <p>Example: 
0286:             * The following will fetch along with Employee(s) "Jones" all projects they participate in
0287:             * along with teamLeaders and their addresses, teamMembers and their phones.
0288:             * 
0289:             * query.setSelectionCriteria(query.getExpressionBuilder().get("lastName").equal("Jones"));
0290:             * Expression projects = query.getExpressionBuilder().anyOf("projects");
0291:             * query.addJoinedAttribute(projects);
0292:             * Expression teamLeader = projects.get("teamLeader");
0293:             * query.addJoinedAttribute(teamLeader);
0294:             * Expression teamLeaderAddress = teamLeader.getAllowingNull("address");
0295:             * query.addJoinedAttribute(teamLeaderAddress);
0296:             * Expression teamMembers = projects.anyOf("teamMembers");
0297:             * query.addJoinedAttribute(teamMembers);
0298:             * Expression teamMembersPhones = teamMembers.anyOfAllowingNone("phoneNumbers");
0299:             * query.addJoinedAttribute(teamMembersPhones);
0300:             * 
0301:             * Note that:
0302:             * the order is essential: an expression should be added before any expression derived from it;
0303:             * the object is built once - it won't be rebuilt if it to be read again as a joined attribute:
0304:             * in the example the query won't get phones for "Jones" - 
0305:             * even though they are among teamMembers (for whom phones are read).
0306:             *
0307:             */
0308:            public void addJoinedAttribute(Expression attributeExpression) {
0309:                getJoinedAttributeManager().addJoinedAttributeExpression(
0310:                        attributeExpression);
0311:                //Bug2804042 Must un-prepare if prepared as the SQL may change.
0312:                // Joined attributes are now calculated in prePrepare.
0313:                setIsPrePrepared(false);
0314:            }
0315:
0316:            /**
0317:             * PUBLIC:
0318:             * Specify the one-to-one mapped attribute to be optimized in this query.
0319:             * The query will join the object(s) being read with the one-to-one 
0320:             * attribute. The difference between this and a joined attribute is that
0321:             * it allows data to be retrieved based on a join, but will not populate
0322:             * the joined attribute. It also allows all of the data required for the 
0323:             * object(s) to be read in a single query instead of (n) queries. This 
0324:             * should be used when the application knows that it requires the part for 
0325:             * all of the objects being read. This can be used only for one-to-one 
0326:             * mappings where the target is not the same class as the source, either 
0327:             * directly or through inheritance.  Also two joins cannot be done to the 
0328:             * same class.
0329:             *
0330:             * <p>Note: This cannot be used for objects where it is possible not to have 
0331:             * a part, as these objects will be ommited from the result set, unless an 
0332:             * outer join is used through passing and expression using "getAllowingNull".
0333:             *
0334:             * <p>Example: query.addNonFetchJoinedAttribute("address")
0335:             *
0336:             * @see #addNonFetchJoinedAttribute(Expression)
0337:             */
0338:            public void addNonFetchJoinedAttribute(String attributeName) {
0339:                addNonFetchJoinedAttribute(getExpressionBuilder().get(
0340:                        attributeName));
0341:            }
0342:
0343:            /**
0344:             * PUBLIC:
0345:             * Specify the one-to-one mapped attribute to be optimized in this query.
0346:             * The query will join the object(s) being read with the one-to-one 
0347:             * attribute. The difference between this and a joined attribute is that
0348:             * it allows data to be retrieved based on a join, but will not populate
0349:             * the joined attribute. It also allows all of the data required for the 
0350:             * object(s) to be read in a single query instead of (n) queries. This 
0351:             * should be used when the application knows that it requires the part for 
0352:             * all of the objects being read. This can be used only for one-to-one 
0353:             * mappings where the target is not the same class as the source, either 
0354:             * directly or through inheritance.  Also two joins cannot be done to the 
0355:             * same class.
0356:             *
0357:             * <p>Note: This cannot be used for objects where it is possible not to have 
0358:             * a part, as these objects will be ommited from the result set, unless an 
0359:             * outer join is used through passing and expression using "getAllowingNull".
0360:             *
0361:             * <p>Example: query.addNonFetchJoinedAttribute(query.getExpressionBuilder().get("teamLeader").get("address"))
0362:             *
0363:             * @see #addNonFetchJoinedAttribute(Expression)
0364:             */
0365:            public void addNonFetchJoinedAttribute(
0366:                    Expression attributeExpression) {
0367:                getNonFetchJoinAttributeExpressions().add(attributeExpression);
0368:
0369:                // Bug 2804042 Must un-prepare if prepared as the SQL may change.
0370:                // Joined attributes are now calculated in prePrepare.
0371:                setIsPrePrepared(false);
0372:            }
0373:
0374:            /**
0375:             * INTERNAL:
0376:             * Iterate through a list of joined expressions and add the fields they represent to a list
0377:             * of fields.
0378:             */
0379:            protected void addSelectionFieldsForJoinedExpressions(List fields,
0380:                    List joinedExpressions) {
0381:                for (int index = 0; index < joinedExpressions.size(); index++) {
0382:                    ObjectExpression objectExpression = (ObjectExpression) joinedExpressions
0383:                            .get(index);
0384:
0385:                    // Expression may not have been initialized.
0386:                    objectExpression.getBuilder().setSession(
0387:                            getSession().getRootSession(null));
0388:                    objectExpression.getBuilder().setQueryClass(
0389:                            getReferenceClass());
0390:                    ClassDescriptor descriptor = objectExpression.getMapping()
0391:                            .getReferenceDescriptor();
0392:                    fields.addAll(descriptor.getFields());
0393:                }
0394:            }
0395:
0396:            /**
0397:             * INTERNAL:
0398:             * Called by CursoredStream to construct objects from rows.
0399:             * Subclasses which build other results objects (ReportQuery, & PartialObjects) may override
0400:             */
0401:            public Object buildObject(AbstractRecord row) {
0402:                return getDescriptor().getObjectBuilder().buildObject(this ,
0403:                        row, this .getJoinedAttributeManager());
0404:            }
0405:
0406:            /**
0407:             * PUBLIC:
0408:             * The cache will checked completely, if the object is not found null will be returned or an error if the query is too complex.
0409:             * Queries can be configured to use the cache at several levels.
0410:             * Other caching option are available.
0411:             * @see #setCacheUsage(int)
0412:             */
0413:            public void checkCacheOnly() {
0414:                setCacheUsage(CheckCacheOnly);
0415:            }
0416:
0417:            /**
0418:             * INTERNAL:
0419:             * Ensure that the descriptor has been set.
0420:             */
0421:            public void checkDescriptor(AbstractSession session)
0422:                    throws QueryException {
0423:                if (getReferenceClass() == null) {
0424:                    throw QueryException.referenceClassMissing(this );
0425:                }
0426:
0427:                if (getDescriptor() == null) {
0428:                    ClassDescriptor referenceDescriptor = session
0429:                            .getDescriptor(getReferenceClass());
0430:                    if (referenceDescriptor == null) {
0431:                        throw QueryException.descriptorIsMissing(
0432:                                getReferenceClass(), this );
0433:                    }
0434:                    setDescriptor(referenceDescriptor);
0435:                }
0436:            }
0437:
0438:            /**
0439:             * INTERNAL:
0440:             * Contains the body of the check early return call, implemented by subclasses.
0441:             */
0442:            protected abstract Object checkEarlyReturnImpl(
0443:                    AbstractSession session, AbstractRecord translationRow);
0444:
0445:            /**
0446:             * INTERNAL:
0447:             * Check to see if this query already knows the return vale without preforming any further work.
0448:             */
0449:            public Object checkEarlyReturn(AbstractSession session,
0450:                    AbstractRecord translationRow) {
0451:                // For bug 3136413/2610803 building the selection criteria from an EJBQL string or
0452:                // an example object is done just in time.
0453:                // Also calls checkDescriptor here.
0454:                //buildSelectionCriteria(session);
0455:                checkPrePrepare(session);
0456:
0457:                if (!session.isUnitOfWork()) {
0458:                    return checkEarlyReturnImpl(session, translationRow);
0459:                }
0460:                UnitOfWorkImpl unitOfWork = (UnitOfWorkImpl) session;
0461:
0462:                // The cache check must happen on the UnitOfWork in these cases either
0463:                // to access transient state or for pessimistic locking, as only the 
0464:                // UOW knows which objects it has locked.
0465:                if (shouldCheckCacheOnly()
0466:                        || shouldConformResultsInUnitOfWork()
0467:                        || getDescriptor()
0468:                                .shouldAlwaysConformResultsInUnitOfWork()
0469:                        || (getLockMode() != ObjectBuildingQuery.NO_LOCK)) {
0470:                    Object result = checkEarlyReturnImpl(unitOfWork,
0471:                            translationRow);
0472:                    if (result != null) {
0473:                        return result;
0474:                    }
0475:                }
0476:
0477:                // don't bother trying to get a cache hit on the parent session
0478:                // as if not in UnitOfWork it is not yet pessimistically locked
0479:                // on the database for sure.
0480:                // Note for ReadObjectQueries we totally ignore shouldCheckCacheOnly.
0481:                if (isReadObjectQuery() && isLockQuery()) {
0482:                    return null;
0483:                }
0484:
0485:                // follow the execution path in looking for the object.
0486:                AbstractSession parentSession = unitOfWork
0487:                        .getParentIdentityMapSession(this );
0488:
0489:                // assert parentSession != unitOfWork;
0490:                Object result = checkEarlyReturn(parentSession, translationRow);
0491:
0492:                if (result != null) {
0493:                    // Optimization: If find deleted object by exact primary key
0494:                    // treat this as cache hit but return null.  Bug 2782991.
0495:                    if (result == InvalidObject.instance) {
0496:                        return result;
0497:                    }
0498:                    return registerResultInUnitOfWork(result, unitOfWork,
0499:                            translationRow, false);
0500:                } else {
0501:                    return null;
0502:                }
0503:            }
0504:
0505:            /**
0506:             * INTERNAL:
0507:             * Check to see if this query needs to be prepare and prepare it.
0508:             * The prepare is done on the original query to ensure that the work is not repeated.
0509:             */
0510:            public void checkPrepare(AbstractSession session,
0511:                    AbstractRecord translationRow) {
0512:                // CR#3823735 For custom queries the prePrepare may not have been called yet.
0513:                checkPrePrepare(session);
0514:                super .checkPrepare(session, translationRow);
0515:            }
0516:
0517:            /**
0518:             * INTERNAL:
0519:             * ObjectLevelReadQueries now have an explicit pre-prepare stage, which
0520:             * is for checking for pessimistic locking, and computing any joined
0521:             * attributes declared on the descriptor.
0522:             */
0523:            protected void checkPrePrepare(AbstractSession session) {
0524:                checkDescriptor(session);
0525:                // This query is first prepared for global common state, this must be synced.
0526:                if (!isPrePrepared()) {// Avoid the monitor is already prePrepare, must check again for concurrency.
0527:                    synchronized (this ) {
0528:                        if (!isPrePrepared()) {
0529:                            AbstractSession alreadySetSession = getSession();
0530:                            setSession(session);// Session is required for some init stuff.
0531:                            prePrepare();
0532:                            setSession(alreadySetSession);
0533:                            setIsPrePrepared(true);// MUST not set prepare until done as other thread may hit before finishing the prePrepare.
0534:                        }
0535:                    }
0536:                }
0537:            }
0538:
0539:            /**
0540:             * INTERNAL:
0541:             * The reference class has been changed, need to reset the
0542:             * descriptor. Null out the current descriptor and call
0543:             * checkDescriptor
0544:             * Added Feb 27, 2001 JED for EJBQL feature
0545:             */
0546:            public void changeDescriptor(AbstractSession theSession) {
0547:                setDescriptor(null);
0548:                checkDescriptor(theSession);
0549:            }
0550:
0551:            /**
0552:             * INTERNAL:
0553:             * Conforms and registers an individual result.  This instance could be one
0554:             * of the elements returned from a read all query, the result of a Read Object
0555:             * query, or an element read from a cursor.
0556:             * <p>
0557:             * A result needs to be registered before it can be conformed, so
0558:             * registerIndividualResult is called here.
0559:             * <p>
0560:             * Conforming on a result from the database is lenient.  Since the object
0561:             * matched the query on the database we assume it matches here unless we can
0562:             * determine for sure that it was changed in this UnitOfWork not to conform.
0563:             * @param result may be an original, or a raw database row
0564:             * @param arguments the parameters this query was executed with
0565:             * @param selectionCriteriaClone the expression to conform to.  If was a
0566:             * selection object or key, null (which all conform to) is used
0567:             * @param alreadyReturned a hashtable of objects already found by scanning
0568:             * the UnitOfWork cache for conforming instances.  Prevents duplicates.
0569:             * @param buildDirectlyFromRows whether result is an original or a raw database
0570:             * row
0571:             * @return a clone, or null if result does not conform.
0572:             */
0573:            protected Object conformIndividualResult(Object result,
0574:                    UnitOfWorkImpl unitOfWork, AbstractRecord arguments,
0575:                    Expression selectionCriteriaClone,
0576:                    IdentityHashtable alreadyReturned,
0577:                    boolean buildDirectlyFromRows) {
0578:                // First register the object.  Since object is presently unwrapped the
0579:                // exact objects stored in the cache will be returned.
0580:                // The object is known to exist.
0581:                Object clone = registerIndividualResult(result, unitOfWork,
0582:                        buildDirectlyFromRows, null);
0583:
0584:                if (getDescriptor().hasWrapperPolicy()
0585:                        && getDescriptor().getWrapperPolicy().isWrapped(clone)) {
0586:                    // The only time the clone could be wrapped is if we are not registering
0587:                    // results in the unitOfWork and we are ready to return a final
0588:                    // (unregistered) result now.  Any further processing may accidently
0589:                    // cause it to get registered.
0590:                    return clone;
0591:                }
0592:                //bug 4459976 in order to maintain backward compatibility on ordering
0593:                // lets use the result as a guild for the final result not the hashtable
0594:                // of found objects.
0595:                if (unitOfWork.isObjectDeleted(clone)) {
0596:                    return null;
0597:                }
0598:                if (!isExpressionQuery() || (selectionCriteriaClone == null)) {
0599:                    if (alreadyReturned != null) {
0600:                        alreadyReturned.remove(clone);
0601:                    }
0602:                    return clone;
0603:                }
0604:                try {
0605:                    // pass in the policy to assume that the object conforms if indirection is not triggered.  This
0606:                    // is valid because the query returned the object and we should trust the query that the object
0607:                    // matches the selection criteria, and because the indirection is not triggered then the customer
0608:                    //has not changed the value.
0609:                    // bug 2637555
0610:                    // unless for bug 3568141 use the painstaking shouldTriggerIndirection if set
0611:                    InMemoryQueryIndirectionPolicy policy = getInMemoryQueryIndirectionPolicy();
0612:                    if (!policy.shouldTriggerIndirection()) {
0613:                        policy = new InMemoryQueryIndirectionPolicy(
0614:                                InMemoryQueryIndirectionPolicy.SHOULD_IGNORE_EXCEPTION_RETURN_CONFORMED);
0615:                    }
0616:                    if (selectionCriteriaClone.doesConform(clone, unitOfWork,
0617:                            arguments, policy)) {
0618:                        if (alreadyReturned != null) {
0619:                            alreadyReturned.remove(clone);
0620:                        }
0621:                        return clone;
0622:                    }
0623:                } catch (QueryException exception) {
0624:                    // bug 3570561: mask all-pervasive valueholder exceptions while conforming
0625:                    if ((unitOfWork.getShouldThrowConformExceptions() == UnitOfWorkImpl.THROW_ALL_CONFORM_EXCEPTIONS)
0626:                            && (exception.getErrorCode() != QueryException.MUST_INSTANTIATE_VALUEHOLDERS)) {
0627:                        throw exception;
0628:                    }
0629:                    if (alreadyReturned != null) {
0630:                        alreadyReturned.remove(clone);
0631:                    }
0632:                    return clone;
0633:                }
0634:                return null;
0635:            }
0636:
0637:            /**
0638:             * PUBLIC:
0639:             * The cache will checked completely, if the object is not found the database will be queried,
0640:             * and the database result will be verified with what is in the cache and/or unit of work including new objects.
0641:             * This can lead to poor performance so it is recomended that only the database be queried in most cases.
0642:             * Queries can be configured to use the cache at several levels.
0643:             * Other caching option are available.
0644:             * @see #setCacheUsage(int)
0645:             */
0646:            public void conformResultsInUnitOfWork() {
0647:                setCacheUsage(ConformResultsInUnitOfWork);
0648:            }
0649:
0650:            /**
0651:             * PUBLIC:
0652:             * Set the query not to lock.
0653:             */
0654:            public void dontAcquireLocks() {
0655:                setLockMode(NO_LOCK);
0656:                //Bug2804042 Must un-prepare if prepared as the SQL may change.
0657:                setIsPrepared(false);
0658:            }
0659:
0660:            /**
0661:             * PUBLIC:
0662:             * This can be used to explicitly disable the cache hit.
0663:             * The cache hit may not be desired in some cases, such as
0664:             * stored procedures that accept the primary key but do not query on it.
0665:             */
0666:            public void dontCheckCache() {
0667:                setCacheUsage(DoNotCheckCache);
0668:            }
0669:
0670:            /**
0671:             * PUBLIC:
0672:             * When unset means perform read normally and dont do refresh.
0673:             */
0674:            public void dontRefreshIdentityMapResult() {
0675:                setShouldRefreshIdentityMapResult(false);
0676:            }
0677:
0678:            /**
0679:             * ADVANCED:
0680:             * If a distinct has been set the DISTINCT clause will be printed.
0681:             * This is used internally by TopLink for batch reading but may also be
0682:             * used directly for advanced queries or report queries.
0683:             */
0684:            public void dontUseDistinct() {
0685:                setDistinctState(DONT_USE_DISTINCT);
0686:                //Bug2804042 Must un-prepare if prepared as the SQL may change.
0687:                setIsPrepared(false);
0688:            }
0689:
0690:            /**
0691:             * INTERNAL:
0692:             * There is a very special case where a query may be a bean-level
0693:             * pessimistic locking query.
0694:             * <p>
0695:             * If that is so, only queries executed inside of a UnitOfWork should
0696:             * have a locking clause.  In the extremely rare case that we execute
0697:             * a locking query outside of a UnitOfWork, must disable locking so that
0698:             * we do not get a fetch out of sequence error.
0699:             */
0700:            public DatabaseQuery prepareOutsideUnitOfWork(
0701:                    AbstractSession session) {
0702:                // Implementation is complicated because: if locking refresh will be
0703:                // auto set to true preventing cache hit.
0704:                // Must prepare this query from scratch if outside uow but locking
0705:                // Must not reprepare this query as a NO_LOCK, but create a clone first
0706:                // Must not cloneAndUnPrepare unless really have to
0707:                if (isLockQuery(session)
0708:                        && getLockingClause().isForUpdateOfClause()) {
0709:                    ObjectLevelReadQuery clone = (ObjectLevelReadQuery) clone();
0710:                    clone.dontAcquireLocks();
0711:                    clone.setIsPrepared(false);
0712:                    clone.checkPrePrepare(session);
0713:                    return clone;
0714:                }
0715:                return this ;
0716:            }
0717:
0718:            /**
0719:             * INTERNAL:
0720:             * Execute the query. If there are objects in the cache  return the results
0721:             * of the cache lookup.
0722:             *
0723:             * @param session - the session in which the receiver will be executed.
0724:             * @exception  DatabaseException - an error has occurred on the database.
0725:             * @exception  OptimisticLockException - an error has occurred using the optimistic lock feature.
0726:             * @return An object, the result of executing the query.
0727:             */
0728:            public Object execute(AbstractSession session,
0729:                    AbstractRecord translationRow) throws DatabaseException,
0730:                    OptimisticLockException {
0731:                //Bug#2839852  Refreshing is not possible if the query uses checkCacheOnly.
0732:                if (shouldRefreshIdentityMapResult() && shouldCheckCacheOnly()) {
0733:                    throw QueryException
0734:                            .refreshNotPossibleWithCheckCacheOnly(this );
0735:                }
0736:                return super .execute(session, translationRow);
0737:            }
0738:
0739:            /*
0740:             * Executes the prepared query on the datastore.
0741:             */
0742:            public Object executeDatabaseQuery() throws DatabaseException {
0743:                if (getSession().isUnitOfWork()) {
0744:                    UnitOfWorkImpl unitOfWork = (UnitOfWorkImpl) getSession();
0745:
0746:                    // Note if a nested unit of work this will recursively start a
0747:                    // transaction early on the parent also.
0748:                    if (isLockQuery()) {
0749:                        if ((!unitOfWork.getCommitManager().isActive())
0750:                                && (!unitOfWork
0751:                                        .wasTransactionBegunPrematurely())) {
0752:                            unitOfWork.beginTransaction();
0753:                            unitOfWork.setWasTransactionBegunPrematurely(true);
0754:                        }
0755:                    }
0756:                    if (unitOfWork.isNestedUnitOfWork()) {
0757:                        UnitOfWorkImpl nestedUnitOfWork = (UnitOfWorkImpl) getSession();
0758:                        setSession(nestedUnitOfWork.getParent());
0759:                        Object result = executeDatabaseQuery();
0760:                        setSession(nestedUnitOfWork);
0761:                        return registerResultInUnitOfWork(result,
0762:                                nestedUnitOfWork, getTranslationRow(), false);
0763:                    }
0764:                }
0765:                session.validateQuery(this );// this will update the query with any settings
0766:
0767:                if (getQueryId() == 0) {
0768:                    setQueryId(getSession().getNextQueryId());
0769:                }
0770:
0771:                return executeObjectLevelReadQuery();
0772:            }
0773:
0774:            /*
0775:             * Executes the prepared query on the datastore.
0776:             */
0777:            protected abstract Object executeObjectLevelReadQuery()
0778:                    throws DatabaseException;
0779:
0780:            /**
0781:             * INTERNAL:
0782:             * At this point only the code has been copied over from UnitOfWork
0783:             * internalExecuteQuery.  No work has been done.
0784:             * @param unitOfWork
0785:             * @param translationRow
0786:             * @return
0787:             * @throws oracle.toplink.essentials.exceptions.DatabaseException
0788:             * @throws oracle.toplink.essentials.exceptions.OptimisticLockException
0789:             */
0790:            public Object executeInUnitOfWork(UnitOfWorkImpl unitOfWork,
0791:                    AbstractRecord translationRow) throws DatabaseException,
0792:                    OptimisticLockException {
0793:                if (!shouldMaintainCache()) {
0794:                    return unitOfWork.getParent().executeQuery(this ,
0795:                            translationRow);
0796:                }
0797:                Object result = execute(unitOfWork, translationRow);
0798:
0799:                // Optimization: If find deleted object on uow by exact primary key
0800:                // treat this as cache hit but return null.  Bug 2782991.
0801:                if (result == InvalidObject.instance) {
0802:                    return null;
0803:                }
0804:                return result;
0805:            }
0806:
0807:            /**
0808:             * INTERNAL:
0809:             * Additional fields can be added to a query.  This is used in m-m bacth reading to bring back the key from the join table.
0810:             */
0811:            public Vector getAdditionalFields() {
0812:                return additionalFields;
0813:            }
0814:
0815:            /**
0816:             * PUBLIC:
0817:             * Return the cache usage.
0818:             * By default only primary key read object queries will first check the cache before accessing the database.
0819:             * Any query can be configure to query against the cache completely, by key or ignore the cache check.
0820:             * <p>Valid values are:
0821:             * <ul>
0822:             * <li> DoNotCheckCache
0823:             * <li> CheckCacheByExactPrimaryKey
0824:             * <li> CheckCacheByPrimaryKey
0825:             * <li> CheckCacheThenDatabase
0826:             * <li> CheckCacheOnly
0827:             * <li> ConformResultsInUnitOfWork
0828:             * <li> UseDescriptorSetting
0829:             * Note: UseDescriptorSetting functions like CheckCacheByPrimaryKey, except checks the appropriate descriptor's
0830:             * shouldDisableCacheHits setting when querying on the cache.
0831:             * </lu>
0832:             */
0833:            public int getCacheUsage() {
0834:                return cacheUsage;
0835:            }
0836:
0837:            /**
0838:             * ADVANCED:
0839:             * If a distinct has been set the DISTINCT clause will be printed.
0840:             * This is used internally by TopLink for batch reading but may also be
0841:             * used directly for advanced queries or report queries.
0842:             */
0843:            public short getDistinctState() {
0844:                return distinctState;
0845:            }
0846:
0847:            /**
0848:             * REQUIRED:
0849:             * Get the expression builder which should be used for this query.
0850:             * This expression builder should be used to build all expressions used by this query.
0851:             */
0852:            public ExpressionBuilder getExpressionBuilder() {
0853:                if (defaultBuilder == null) {
0854:                    initializeDefaultBuilder();
0855:                }
0856:
0857:                return defaultBuilder;
0858:            }
0859:
0860:            /**
0861:             * INTERNAL
0862:             * Sets the default expression builder for this query.
0863:             */
0864:            public void setExpressionBuilder(ExpressionBuilder builder) {
0865:                this .defaultBuilder = builder;
0866:            }
0867:
0868:            /**
0869:             * PUBLIC:
0870:             * Returns the InMemoryQueryIndirectionPolicy for this query
0871:             */
0872:            public InMemoryQueryIndirectionPolicy getInMemoryQueryIndirectionPolicy() {
0873:                return this .inMemoryQueryIndirectionPolicy;
0874:            }
0875:
0876:            /**
0877:             * INTERNAL:
0878:             * Set the list of expressions that represent elements that are joined because of their
0879:             * mapping for this query.
0880:             */
0881:            public JoinedAttributeManager getJoinedAttributeManager() {
0882:                return this .joinedAttributeManager;
0883:            }
0884:
0885:            /**
0886:             * INTERNAL:
0887:             * Return the attributes that must be joined, but not fetched, that is,
0888:             * do not trigger the value holder.
0889:             */
0890:            public Vector getNonFetchJoinAttributeExpressions() {
0891:                if (this .nonFetchJoinAttributeExpressions == null) {
0892:                    this .nonFetchJoinAttributeExpressions = oracle.toplink.essentials.internal.helper.NonSynchronizedVector
0893:                            .newInstance();
0894:                }
0895:                return nonFetchJoinAttributeExpressions;
0896:            }
0897:
0898:            /**
0899:             * INTERNAL:
0900:             * Lookup the mapping for this item by traversing its expression recursively.
0901:             * If an aggregate of foreign mapping is found it is traversed.
0902:             */
0903:            public DatabaseMapping getLeafMappingFor(Expression expression,
0904:                    ClassDescriptor rootDescriptor) throws QueryException {
0905:                // Check for database field expressions or place holder
0906:                if ((expression == null) || (expression.isFieldExpression())) {
0907:                    return null;
0908:                }
0909:
0910:                if (!(expression.isQueryKeyExpression())) {
0911:                    return null;
0912:                }
0913:
0914:                QueryKeyExpression qkExpression = (QueryKeyExpression) expression;
0915:                Expression baseExpression = qkExpression.getBaseExpression();
0916:
0917:                ClassDescriptor descriptor = getLeafDescriptorFor(
0918:                        baseExpression, rootDescriptor);
0919:                return descriptor.getMappingForAttributeName(qkExpression
0920:                        .getName());
0921:            }
0922:
0923:            /**
0924:             * INTERNAL:
0925:             * Lookup the descriptor for this item by traversing its expression recursively.
0926:             * @param expression
0927:             * @param rootDescriptor
0928:             * @return
0929:             * @throws oracle.toplink.essentials.exceptions.QueryException
0930:             */
0931:            public ClassDescriptor getLeafDescriptorFor(Expression expression,
0932:                    ClassDescriptor rootDescriptor) throws QueryException {
0933:                // The base case
0934:                if (expression.isExpressionBuilder()) {
0935:                    // The following special case is where there is a parallel builder
0936:                    // which has a different reference class as the primary builder.
0937:                    Class queryClass = ((ExpressionBuilder) expression)
0938:                            .getQueryClass();
0939:                    if ((queryClass != null)
0940:                            && (queryClass != getReferenceClass())) {
0941:                        return getSession().getDescriptor(queryClass);
0942:                    }
0943:                    return rootDescriptor;
0944:                }
0945:                Expression baseExpression = ((QueryKeyExpression) expression)
0946:                        .getBaseExpression();
0947:                ClassDescriptor baseDescriptor = getLeafDescriptorFor(
0948:                        baseExpression, rootDescriptor);
0949:                ClassDescriptor descriptor = null;
0950:                String attributeName = expression.getName();
0951:
0952:                DatabaseMapping mapping = baseDescriptor
0953:                        .getMappingForAttributeName(attributeName);
0954:
0955:                if (mapping == null) {
0956:                    QueryKey queryKey = baseDescriptor
0957:                            .getQueryKeyNamed(attributeName);
0958:                    if (queryKey != null) {
0959:                        if (queryKey.isForeignReferenceQueryKey()) {
0960:                            descriptor = getSession().getDescriptor(
0961:                                    ((ForeignReferenceQueryKey) queryKey)
0962:                                            .getReferenceClass());
0963:                        } else// if (queryKey.isDirectQueryKey())
0964:                        {
0965:                            descriptor = queryKey.getDescriptor();
0966:                        }
0967:                    }
0968:                    if (descriptor == null) {
0969:                        throw QueryException.invalidExpressionForQueryItem(
0970:                                expression, this );
0971:                    }
0972:                } else if (mapping.isAggregateObjectMapping()) {
0973:                    descriptor = ((AggregateObjectMapping) mapping)
0974:                            .getReferenceDescriptor();
0975:                } else if (mapping.isForeignReferenceMapping()) {
0976:                    descriptor = ((ForeignReferenceMapping) mapping)
0977:                            .getReferenceDescriptor();
0978:                }
0979:                return descriptor;
0980:            }
0981:
0982:            /**
0983:             * PUBLIC:
0984:             * Return the current locking mode.
0985:             */
0986:            public short getLockMode() {
0987:                if (lockingClause == null) {
0988:                    return DEFAULT_LOCK_MODE;
0989:                } else {
0990:                    return lockingClause.getLockMode();
0991:                }
0992:            }
0993:
0994:            /**
0995:             * INTERNAL:
0996:             * It is not exactly as simple as a query being either locking or not.
0997:             * Any combination of the reference class object and joined attributes
0998:             * may be locked.
0999:             */
1000:            public ForUpdateClause getLockingClause() {
1001:                return lockingClause;
1002:            }
1003:
1004:            /**
1005:             * INTERNAL:
1006:             * Return the time this query actually went to the database
1007:             */
1008:            public long getExecutionTime() {
1009:                return executionTime;
1010:            }
1011:
1012:            /**
1013:             * PUBLIC:
1014:             * Return the reference class of the query.
1015:             */
1016:            // TODO This method is left in so the javadoc remains, but is actually implemented on 
1017:            // super.  We should reconsider this.
1018:            public Class getReferenceClass() {
1019:                return super .getReferenceClass();
1020:            }
1021:
1022:            /**
1023:             * INTERNAL:
1024:             * Return the reference class of the query.
1025:             */
1026:            // TODO This method is left in so the javadoc remains, but is actually implemented on 
1027:            // super.  We should reconsider this.
1028:            public String getReferenceClassName() {
1029:                return super .getReferenceClassName();
1030:            }
1031:
1032:            /**
1033:             * PUBLIC:
1034:             * Answers if the domain objects are to be read as of a past time.
1035:             * @see #getAsOfClause()
1036:             */
1037:            public boolean hasAsOfClause() {
1038:                return ((defaultBuilder != null) && defaultBuilder
1039:                        .hasAsOfClause());
1040:            }
1041:
1042:            /**
1043:             * INTERNAL:
1044:             * Return the attributes that must be joined.
1045:             */
1046:            public boolean hasNonFetchJoinedAttributeExpressions() {
1047:                return this .nonFetchJoinAttributeExpressions != null
1048:                        && !this .nonFetchJoinAttributeExpressions.isEmpty();
1049:            }
1050:
1051:            /**
1052:             * INTERNAL:
1053:             * Return if partial attributes.
1054:             */
1055:            public boolean hasPartialAttributeExpressions() {
1056:                return false;
1057:            }
1058:
1059:            /**
1060:             * INTERNAL:
1061:             * Return the fields selected by the query.
1062:             * This includes the partial or joined fields.
1063:             * This is used for custom SQL executions.
1064:             */
1065:            public Vector getSelectionFields() {
1066:                if ((!hasPartialAttributeExpressions())
1067:                        && (!getJoinedAttributeManager().hasJoinedAttributes())
1068:                        && (!hasFetchGroupAttributeExpressions())) {
1069:                    return getDescriptor().getAllFields();
1070:                }
1071:                Vector fields;
1072:                if (hasFetchGroupAttributeExpressions()) {//fetch group support
1073:                    List fetchGroupAttrExps = getFetchGroup()
1074:                            .getFetchGroupAttributeExpressions();
1075:                    fields = new Vector(fetchGroupAttrExps.size());
1076:                    for (int index = 0; index < fetchGroupAttrExps.size(); index++) {
1077:                        Expression expression = (Expression) fetchGroupAttrExps
1078:                                .get(index);
1079:
1080:                        // Expression may not have been initialized.
1081:                        expression.getBuilder().setSession(
1082:                                getSession().getRootSession(null));
1083:                        expression.getBuilder().setQueryClass(
1084:                                getReferenceClass());
1085:                        Helper.addAllToVector(fields, expression.getFields());
1086:                    }
1087:                } else {
1088:                    fields = new Vector(getDescriptor().getAllFields().size()
1089:                            + getJoinedAttributeManager()
1090:                                    .getJoinedAttributeExpressions().size()
1091:                            + getJoinedAttributeManager()
1092:                                    .getJoinedMappingExpressions().size());
1093:                    Helper.addAllToVector(fields, getDescriptor()
1094:                            .getAllFields());
1095:                    addSelectionFieldsForJoinedExpressions(fields,
1096:                            getJoinedAttributeManager()
1097:                                    .getJoinedAttributeExpressions());
1098:                    addSelectionFieldsForJoinedExpressions(fields,
1099:                            getJoinedAttributeManager()
1100:                                    .getJoinedMappingExpressions());
1101:                }
1102:                return fields;
1103:            }
1104:
1105:            /**
1106:             * Initialize the expression builder which should be used for this query. If
1107:             * there is a where clause, use its expression builder, otherwise
1108:             * generate one and cache it. This helps avoid unnecessary rebuilds.
1109:             */
1110:            protected void initializeDefaultBuilder() {
1111:                DatabaseQueryMechanism mech = getQueryMechanism();
1112:                if (mech.isExpressionQueryMechanism()
1113:                        && ((ExpressionQueryMechanism) mech)
1114:                                .getExpressionBuilder() != null) {
1115:                    this .defaultBuilder = ((ExpressionQueryMechanism) mech)
1116:                            .getExpressionBuilder();
1117:                    return;
1118:                }
1119:                this .defaultBuilder = new ExpressionBuilder();
1120:            }
1121:
1122:            /**
1123:             * INTERNAL:
1124:             * return true if this query has computed its distinct value already
1125:             */
1126:            public boolean isDistinctComputed() {
1127:                return getDistinctState() != UNCOMPUTED_DISTINCT;
1128:            }
1129:
1130:            /**
1131:             * PUBLIC:
1132:             * Answers if the query lock mode is known to be LOCK or LOCK_NOWAIT.
1133:             *
1134:             * In the case of DEFAULT_LOCK_MODE and the query reference class being a CMP entity bean,
1135:             * at execution time LOCK, LOCK_NOWAIT, or NO_LOCK will be decided.
1136:             * <p>
1137:             * If a single joined attribute was configured for pessimistic locking then
1138:             * this will return true (after first execution) as the SQL contained a
1139:             * FOR UPDATE OF clause.
1140:             */
1141:            public boolean isLockQuery() {
1142:                return getLockMode() > NO_LOCK;
1143:            }
1144:
1145:            /**
1146:             * ADVANCED:
1147:             * Answers if this query will issue any pessimistic locks.
1148:             * <p>
1149:             * If the lock mode is not known (DEFAULT_LOCK_MODE / descriptor specified
1150:             * fine-grained locking) the lock mode will be determined now, to be either
1151:             * LOCK, LOCK_NOWAIT, or NO_LOCK.
1152:             * @see #isLockQuery()
1153:             */
1154:            public boolean isLockQuery(
1155:                    oracle.toplink.essentials.sessions.Session session) {
1156:                checkPrePrepare((AbstractSession) session);
1157:                return isLockQuery();
1158:            }
1159:
1160:            /**
1161:             * PUBLIC:
1162:             * Return if this is an object level read query.
1163:             */
1164:            public boolean isObjectLevelReadQuery() {
1165:                return true;
1166:            }
1167:
1168:            /**
1169:             * PUBLIC:
1170:             * Queries prepare common stated in themselves.
1171:             */
1172:            protected boolean isPrePrepared() {
1173:                return isPrePrepared;
1174:            }
1175:
1176:            /**
1177:             * INTERNAL:
1178:             * Answers if we are executing through a UnitOfWork and registering results.
1179:             * This is only ever false if using the conforming without registering
1180:             * feature.
1181:             */
1182:            protected boolean isRegisteringResults() {
1183:                return ((shouldRegisterResultsInUnitOfWork() && getDescriptor()
1184:                        .shouldRegisterResultsInUnitOfWork()) || isLockQuery());
1185:            }
1186:
1187:            /**
1188:             * INTERNAL:
1189:             * If changes are made to the query that affect the derived SQL or Call
1190:             * parameters the query needs to be prepared again.
1191:             * <p>
1192:             * Automatically called internally.
1193:             * <p>
1194:             * The early phase of preparation is to check if this is a pessimistic
1195:             * locking query.
1196:             */
1197:            protected void setIsPrePrepared(boolean isPrePrepared) {
1198:                // Only unprepare if was prepared to begin with, prevent unpreparing during prepare.
1199:                if (this .isPrePrepared && !isPrePrepared) {
1200:                    setIsPrepared(false);
1201:                    this .getJoinedAttributeManager().reset();
1202:                }
1203:                this .isPrePrepared = isPrePrepared;
1204:            }
1205:
1206:            /**
1207:             * INTERNAL:
1208:             * Prepare the receiver for execution in a session.
1209:             */
1210:            protected void prepare() throws QueryException {
1211:                super .prepare();
1212:                prepareQuery();
1213:            }
1214:
1215:            /**
1216:             * INTERNAL:
1217:             * Prepare the receiver for execution in a session.
1218:             */
1219:            protected void prePrepare() throws QueryException {
1220:                // For bug 3136413/2610803 building the selection criteria from an EJBQL string or
1221:                // an example object is done just in time.
1222:                buildSelectionCriteria(session);
1223:                checkDescriptor(session);
1224:
1225:                // Add mapping joined attributes.
1226:                if (getQueryMechanism().isExpressionQueryMechanism()) {
1227:                    getJoinedAttributeManager().processJoinedMappings();
1228:                }
1229:
1230:                // modify query for locking only if locking has not been configured
1231:                if (isDefaultLock()) {
1232:                    setWasDefaultLockMode(true);
1233:                    ForUpdateOfClause lockingClause = null;
1234:                    if (getJoinedAttributeManager().hasJoinedExpressions()) {
1235:                        lockingClause = getJoinedAttributeManager()
1236:                                .setupLockingClauseForJoinedExpressions(
1237:                                        lockingClause, getSession());
1238:                    }
1239:                    if (lockingClause == null) {
1240:                        this .lockingClause = ForUpdateClause
1241:                                .newInstance(NO_LOCK);
1242:                    } else {
1243:                        this .lockingClause = lockingClause;
1244:                        // SPECJ: Locking not compatible with distinct for batch reading.
1245:                        dontUseDistinct();
1246:                    }
1247:                } else if (getLockMode() == NO_LOCK) {
1248:                    setWasDefaultLockMode(true);
1249:                }
1250:            }
1251:
1252:            /**
1253:             * INTERNAL:
1254:             * Prepare the receiver for execution in a session.
1255:             */
1256:            protected void prepareQuery() throws QueryException {
1257:                if ((!shouldMaintainCache())
1258:                        && shouldRefreshIdentityMapResult()
1259:                        && (!descriptor.isAggregateCollectionDescriptor())) {
1260:                    throw QueryException.refreshNotPossibleWithoutCache(this );
1261:                }
1262:                if (shouldMaintainCache() && hasPartialAttributeExpressions()) {
1263:                    throw QueryException.cannotCachePartialObjects(this );
1264:                }
1265:
1266:                if (descriptor.isAggregateDescriptor()) {
1267:                    // Not allowed
1268:                    throw QueryException
1269:                            .aggregateObjectCannotBeDeletedOrWritten(
1270:                                    descriptor, this );
1271:                }
1272:
1273:                // If fetch group manager is not set in the descriptor and the user attempts to use fetch group in the query dynamiclly, throw exception here.
1274:                if ((!getDescriptor().hasFetchGroupManager())
1275:                        && ((getFetchGroup() != null) || (getFetchGroupName() != null))) {
1276:                    throw QueryException
1277:                            .fetchGroupValidOnlyIfFetchGroupManagerInDescriptor(
1278:                                    getDescriptor().getJavaClassName(),
1279:                                    getName());
1280:                }
1281:
1282:                // Prepare fetch group if applied.
1283:                if (getDescriptor().hasFetchGroupManager()) {
1284:                    getDescriptor().getFetchGroupManager()
1285:                            .prepareQueryWithFetchGroup(this );
1286:                }
1287:
1288:                // Validate and prepare join expressions.			
1289:                if (getJoinedAttributeManager().hasJoinedExpressions()) {
1290:                    getJoinedAttributeManager().prepareJoinExpressions(
1291:                            getSession());
1292:                } else {
1293:                    // If the query is being re-prepared must clear possible old cached data.
1294:                    getJoinedAttributeManager().reset();
1295:                }
1296:            }
1297:
1298:            /**
1299:             * PUBLIC:
1300:             * Refresh the attributes of the object(s) resulting from the query.
1301:             * If cascading is used the private parts of the objects will also be refreshed.
1302:             */
1303:            public void refreshIdentityMapResult() {
1304:                setShouldRefreshIdentityMapResult(true);
1305:            }
1306:
1307:            /**
1308:             * INTERNAL:
1309:             * All objects queried via a UnitOfWork get registered here.  If the query
1310:             * went to the database.
1311:             * <p>
1312:             * Involves registering the query result individually and in totality, and
1313:             * hence refreshing / conforming is done here.
1314:             *
1315:             * @param result may be collection (read all) or an object (read one),
1316:             * or even a cursor.  If in transaction the shared cache will
1317:             * be bypassed, meaning the result may not be originals from the parent
1318:             * but raw database rows.
1319:             * @param unitOfWork the unitOfWork the result is being registered in.
1320:             * @param arguments the original arguments/parameters passed to the query
1321:             * execution.  Used by conforming
1322:             * @param buildDirectlyFromRows If in transaction must construct
1323:             * a registered result from raw database rows.
1324:             *
1325:             * @return the final (conformed, refreshed, wrapped) UnitOfWork query result
1326:             */
1327:            public abstract Object registerResultInUnitOfWork(Object result,
1328:                    UnitOfWorkImpl unitOfWork, AbstractRecord arguments,
1329:                    boolean buildDirectlyFromRows);
1330:
1331:            /**
1332:             * ADVANCED:
1333:             * If a distinct has been set the DISTINCT clause will be printed.
1334:             * This is used internally by TopLink for batch reading but may also be
1335:             * used directly for advanced queries or report queries.
1336:             */
1337:            public void resetDistinct() {
1338:                setDistinctState(UNCOMPUTED_DISTINCT);
1339:                //Bug2804042 Must un-prepare if prepared as the SQL may change.
1340:                setIsPrepared(false);
1341:            }
1342:
1343:            /**
1344:             * INTERNAL:
1345:             * Additional fields can be added to a query.  This is used in m-m bacth reading to bring back the key from the join table.
1346:             */
1347:            public void setAdditionalFields(Vector additionalFields) {
1348:                this .additionalFields = additionalFields;
1349:            }
1350:
1351:            /**
1352:             * PUBLIC:
1353:             * Set the cache usage.
1354:             * By default only primary key read object queries will first check the cache before accessing the database.
1355:             * Any query can be configure to query against the cache completely, by key or ignore the cache check.
1356:             * <p>Valid values are:
1357:             * <ul>
1358:             * <li> DoNotCheckCache - The query does not check the cache but accesses the database, the cache will still be maintain.
1359:             * <li> CheckCacheByExactPrimaryKey - If the query is exactly and only on the object's primary key the cache will be checked.
1360:             * <li> CheckCacheByPrimaryKey - If the query contains the primary key and possible other values the cache will be checked.
1361:             * <li> CheckCacheThenDatabase - The whole cache will be checked to see if there is any object matching the query, if not the database will be accessed.
1362:             * <li> CheckCacheOnly - The whole cache will be checked to see if there is any object matching the query, if not null or an empty collection is returned.
1363:             * <li> ConformResultsAgainstUnitOfWork - The results will be checked againtst the changes within the unit of work and object no longer matching or deleted will be remove, matching new objects will also be added.
1364:             * <li> shouldCheckDescriptorForCacheUsage - This setting functions like CheckCacheByPrimaryKey, except checks the appropriate descriptor's
1365:             * shouldDisableCacheHits setting when querying on the cache.
1366:             * </lu>
1367:             */
1368:            public void setCacheUsage(int cacheUsage) {
1369:                this .cacheUsage = cacheUsage;
1370:            }
1371:
1372:            /**
1373:             * INTERNAL:
1374:             * Set the descriptor for the query.
1375:             */
1376:            public void setDescriptor(ClassDescriptor descriptor) {
1377:                super .setDescriptor(descriptor);
1378:                if (this .joinedAttributeManager != null) {
1379:                    this .joinedAttributeManager.setDescriptor(descriptor);
1380:                }
1381:            }
1382:
1383:            /**
1384:             * ADVANCED:
1385:             * If a distinct has been set the DISTINCT clause will be printed.
1386:             * This is used internally by TopLink for batch reading but may also be
1387:             * used directly for advanced queries or report queries.
1388:             */
1389:            public void setDistinctState(short distinctState) {
1390:                this .distinctState = distinctState;
1391:            }
1392:
1393:            /**
1394:             * INTERNAL:
1395:             * Set the the time this query went to the database.
1396:             */
1397:            public void setExecutionTime(long executionTime) {
1398:                this .executionTime = executionTime;
1399:            }
1400:
1401:            /**
1402:             * PUBLIC:
1403:             * Set the InMemoryQueryIndirectionPolicy for this query
1404:             */
1405:            //Feature 2297
1406:            public void setInMemoryQueryIndirectionPolicy(
1407:                    InMemoryQueryIndirectionPolicy inMemoryQueryIndirectionPolicy) {
1408:                //Bug2862302 Backwards compatibility.  This makes sure 9.0.3 and any older version project xml don't break
1409:                if (inMemoryQueryIndirectionPolicy != null) {
1410:                    this .inMemoryQueryIndirectionPolicy = inMemoryQueryIndirectionPolicy;
1411:                }
1412:            }
1413:
1414:            /**
1415:             * PUBLIC:
1416:             * Sets whether this is a pessimistically locking query.
1417:             * <ul>
1418:             * <li>ObjectBuildingQuery.LOCK: SELECT .... FOR UPDATE issued.
1419:             * <li>ObjectBuildingQuery.LOCK_NOWAIT: SELECT .... FOR UPDATE NO WAIT issued.
1420:             * <li>ObjectBuildingQuery.NO_LOCK: no pessimistic locking.
1421:             * <li>ObjectBuildingQuery.DEFAULT_LOCK_MODE (default) and you have a CMP descriptor:
1422:             * fine grained locking will occur.
1423:             * </ul>
1424:             * <p>Fine Grained Locking: On execution the reference class
1425:             * and those of all joined attributes will be checked.  If any of these have a
1426:             * PessimisticLockingPolicy set on their descriptor, they will be locked in a
1427:             * SELECT ... FOR UPDATE OF ... {NO WAIT}.  Issues fewer locks
1428:             * and avoids setting the lock mode on each query.
1429:             * <p>Example:<code>readAllQuery.setSelectionCriteria(employee.get("address").equal("Ottawa"));</code>
1430:             * <ul><li>LOCK: all employees in Ottawa and all referenced Ottawa addresses will be locked.
1431:             * <li>DEFAULT_LOCK_MODE: if address is a joined attribute, and only address has a pessimistic
1432:             * locking policy, only referenced Ottawa addresses will be locked.
1433:             * </ul>
1434:             * @see oracle.toplink.essentials.descriptors.PessimisticLockingPolicy
1435:             */
1436:            public void setLockMode(short lockMode) {
1437:                if ((lockMode == LOCK) || (lockMode == LOCK_NOWAIT)) {
1438:                    lockingClause = ForUpdateClause.newInstance(lockMode);
1439:                    setShouldRefreshIdentityMapResult(true);
1440:                } else if (lockMode == NO_LOCK) {
1441:                    lockingClause = ForUpdateClause.newInstance(lockMode);
1442:                } else {
1443:                    lockingClause = null;
1444:                    setIsPrePrepared(false);
1445:                }
1446:                setIsPrepared(false);
1447:            }
1448:
1449:            /**
1450:             * INTERNAL:
1451:             * Return the attributes that must be joined, but not fetched, that is,
1452:             * do not trigger the value holder.
1453:             */
1454:            protected void setNonFetchJoinAttributeExpressions(
1455:                    Vector nonFetchJoinExpressions) {
1456:                this .nonFetchJoinAttributeExpressions = nonFetchJoinExpressions;
1457:            }
1458:
1459:            /**
1460:             * INTERNAL:
1461:             * The locking clause contains a list of expressions representing which
1462:             * objects are to be locked by the query.
1463:             * <p>
1464:             * Use for even finer grained control over what is and is not locked by
1465:             * a particular query.
1466:             */
1467:            public void setLockingClause(ForUpdateClause clause) {
1468:                if (clause.isForUpdateOfClause()) {
1469:                    this .lockingClause = clause;
1470:                    setIsPrePrepared(false);
1471:                } else {
1472:                    setLockMode(clause.getLockMode());
1473:                }
1474:            }
1475:
1476:            public void setEJBQLString(String ejbqlString) {
1477:                super .setEJBQLString(ejbqlString);
1478:                setIsPrePrepared(false);
1479:            }
1480:
1481:            /**
1482:             * REQUIRED:
1483:             * Set the reference class for the query.
1484:             */
1485:            // TODO This method is left in so the javadoc remains, but is actually implemented on 
1486:            // super.  We should reconsider this.
1487:            public void setReferenceClass(Class aClass) {
1488:                super .setReferenceClass(aClass);
1489:            }
1490:
1491:            /**
1492:             * INTERNAL:
1493:             * Set the reference class for the query.
1494:             */
1495:            // TODO This method is left in so the javadoc remains, but is actually implemented on 
1496:            // super.  We should reconsider this.
1497:            public void setReferenceClassName(String aClass) {
1498:                super .setReferenceClassName(aClass);
1499:            }
1500:
1501:            public void setSelectionCriteria(Expression expression) {
1502:                super .setSelectionCriteria(expression);
1503:                if ((expression != null) && (defaultBuilder != null)) {
1504:                    // For flashback: Must make sure expression and defaultBuilder always in sync.
1505:                    ExpressionBuilder newBuilder = expression.getBuilder();
1506:                    if ((newBuilder != defaultBuilder)
1507:                            && (newBuilder.getQueryClass() == null || newBuilder
1508:                                    .getQueryClass().equals(
1509:                                            defaultBuilder.getQueryClass()))) {
1510:                        defaultBuilder = newBuilder;
1511:                    }
1512:                }
1513:            }
1514:
1515:            /**
1516:             * INTERNAL:
1517:             * Set if the rows for the result of the query should also be returned using a complex query result.
1518:             * @see ComplexQueryResult
1519:             */
1520:            public void setShouldIncludeData(boolean shouldIncludeData) {
1521:                this .shouldIncludeData = shouldIncludeData;
1522:            }
1523:
1524:            /**
1525:             * PUBLIC:
1526:             * Set if the attributes of the object(s) resulting from the query should be refreshed.
1527:             * If cascading is used the private parts of the objects will also be refreshed.
1528:             */
1529:            public void setShouldRefreshIdentityMapResult(
1530:                    boolean shouldRefreshIdentityMapResult) {
1531:                this .shouldRefreshIdentityMapResult = shouldRefreshIdentityMapResult;
1532:            }
1533:
1534:            /**
1535:             * INTERNAL:
1536:             * Set to false to have queries conform to a UnitOfWork without registering
1537:             * any additional objects not already in that UnitOfWork.
1538:             * @see #shouldRegisterResultsInUnitOfWork
1539:             * @bug 2612601
1540:             */
1541:            public void setShouldRegisterResultsInUnitOfWork(
1542:                    boolean shouldRegisterResultsInUnitOfWork) {
1543:                this .shouldRegisterResultsInUnitOfWork = shouldRegisterResultsInUnitOfWork;
1544:            }
1545:
1546:            /**
1547:             * PUBLIC:
1548:             * Return if cache should be checked.
1549:             */
1550:            public boolean shouldCheckCacheOnly() {
1551:                return getCacheUsage() == CheckCacheOnly;
1552:            }
1553:
1554:            /**
1555:             * PUBLIC:
1556:             * Return whether the descriptor's disableCacheHits setting should be checked prior
1557:             * to querying the cache.
1558:             */
1559:            public boolean shouldCheckDescriptorForCacheUsage() {
1560:                return getCacheUsage() == UseDescriptorSetting;
1561:            }
1562:
1563:            /**
1564:             * PUBLIC:
1565:             * Should the results will be checked against the changes within the unit of work and object no longer matching or deleted will be remove, matching new objects will also be added..
1566:             */
1567:            public boolean shouldConformResultsInUnitOfWork() {
1568:                return getCacheUsage() == ConformResultsInUnitOfWork;
1569:            }
1570:
1571:            /**
1572:             * INTERNAL:
1573:             * return true if this query should use a distinct
1574:             */
1575:            public boolean shouldDistinctBeUsed() {
1576:                return getDistinctState() == USE_DISTINCT;
1577:            }
1578:
1579:            /**
1580:             * INTERNAL:
1581:             * Return if the rows for the result of the query should also be returned using a complex query result.
1582:             * @see ComplexQueryResult
1583:             */
1584:            public boolean shouldIncludeData() {
1585:                return shouldIncludeData;
1586:            }
1587:
1588:            /**
1589:             * INTERNAL:
1590:             * Allows one to do conforming in a UnitOfWork without registering.
1591:             * Queries executed on a UnitOfWork will only return working copies for objects
1592:             * that have already been registered.
1593:             * <p>Extreme care should be taken in using this feature, for a user will
1594:             * get back a mix of registered and original (unregistered) objects.
1595:             * <p>Best used with a WrapperPolicy where invoking on an object will trigger
1596:             * its registration (CMP).  Without a WrapperPolicy {@link oracle.toplink.essentials.sessions.UnitOfWork#registerExistingObject registerExistingObject}
1597:             * should be called on any object that you intend to change.
1598:             * @return true by default.
1599:             * @see #setShouldRegisterResultsInUnitOfWork(boolean)
1600:             * @see oracle.toplink.essentials.publicinterface.Descriptor#shouldRegisterResultsInUnitOfWork()
1601:             * @bug 2612601
1602:             */
1603:            public boolean shouldRegisterResultsInUnitOfWork() {
1604:                return shouldRegisterResultsInUnitOfWork;
1605:            }
1606:
1607:            /**
1608:             * INTERNAL:
1609:             * Return if this is a full object query, not partial nor fetch group.
1610:             */
1611:            public boolean shouldReadAllMappings() {
1612:                return (!hasPartialAttributeExpressions())
1613:                        && (!hasFetchGroupAttributeExpressions());
1614:            }
1615:
1616:            /**
1617:             * INTERNAL:
1618:             * Check if the mapping is part of the partial attributes.
1619:             */
1620:            public boolean shouldReadMapping(DatabaseMapping mapping) {
1621:                if ((!hasPartialAttributeExpressions())
1622:                        && (!hasFetchGroupAttributeExpressions())) {
1623:                    return true;
1624:                }
1625:
1626:                // bug 3659145
1627:                if (hasFetchGroupAttributeExpressions()) {
1628:                    return isFetchGroupAttribute(mapping.getAttributeName());
1629:                }
1630:
1631:                return true;
1632:            }
1633:
1634:            /**
1635:             * PUBLIC:
1636:             * Set to a boolean. When set means refresh the instance
1637:             * variables of referenceObject from the database.
1638:             */
1639:            public boolean shouldRefreshIdentityMapResult() {
1640:                return shouldRefreshIdentityMapResult;
1641:            }
1642:
1643:            public String toString() {
1644:                if (getReferenceClass() == null) {
1645:                    return super .toString();
1646:                }
1647:                return Helper.getShortClassName(getClass()) + "("
1648:                        + getReferenceClass().getName() + ")";
1649:            }
1650:
1651:            /**
1652:             * ADVANCED:
1653:             * Used for CMP only.  This allows users to indicate whether cmp finders executed
1654:             * at the beginning of a transaction should always be run against a UnitOfWork.
1655:             * Defaults to true.
1656:             * <p>
1657:             * If set to false, then UnitOfWork allocation will be deferred until a business
1658:             * method (including creates/removes) or finder with shouldProcessResultsInUnitOfWork == true
1659:             * is invoked.  Any finder executed before such a time, will do so against the
1660:             * underlying ServerSession.  Forcing finder execution to always go through a
1661:             * UnitOfWork means the results will be cloned and cached in the UnitOfWork up
1662:             * front.  This is desired when the results will be accessed in the same transaction.
1663:             * <p>
1664:             * Note that finders executed with an unspecified transaction context will never
1665:             * be executed against a UnitOfWork, even if this setting is true.  This case may happen
1666:             * with the NotSupported, Never, and Supports attributes.
1667:             */
1668:            public void setShouldProcessResultsInUnitOfWork(
1669:                    boolean processResultsInUnitOfWork) {
1670:                this .shouldProcessResultsInUnitOfWork = processResultsInUnitOfWork;
1671:            }
1672:
1673:            /**
1674:             * ADVANCED:
1675:             * Used for CMP only.  Indicates whether cmp finders executed at the beginning
1676:             * of a transaction should always be run against a UnitOfWork.
1677:             * Defaults to true.
1678:             * <p>
1679:             * If set to false, then UnitOfWork allocation will be deferred until a business
1680:             * method (including creates/removes) or finder with shouldProcessResultsInUnitOfWork == true
1681:             * is invoked.  Any finder executed before such a time, will do so against the
1682:             * underlying ServerSession.  Forcing finder execution to always go through a
1683:             * UnitOfWork means the results will be cloned and cached in the UnitOfWork up
1684:             * front.  This is desired when the results will be accessed in the same transaction.
1685:             * <p>
1686:             * Note that finders executed with an unspecified transaction context will never
1687:             * be executed against a UnitOfWork, even if this setting is true.  This case may happen
1688:             * with the NotSupported, Never, and Supports attributes.
1689:             */
1690:            public boolean shouldProcessResultsInUnitOfWork() {
1691:                return this .shouldProcessResultsInUnitOfWork;
1692:            }
1693:
1694:            /**
1695:             * ADVANCED:
1696:             * If a distinct has been set the DISTINCT clause will be printed.
1697:             * This is used internally by TopLink for batch reading but may also be
1698:             * used directly for advanced queries or report queries.
1699:             */
1700:            public void useDistinct() {
1701:                setDistinctState(USE_DISTINCT);
1702:                //Bug2804042 Must un-prepare if prepared as the SQL may change.
1703:                setIsPrepared(false);
1704:            }
1705:
1706:            /**
1707:             * INTERNAL:
1708:             * Helper method that checks if clone has been locked with uow.
1709:             */
1710:            public boolean isClonePessimisticLocked(Object clone,
1711:                    UnitOfWorkImpl uow) {
1712:                return false;
1713:            }
1714:
1715:            /**
1716:             * INTERNAL:
1717:             * Helper method that records clone with uow if query is pessimistic locking.
1718:             */
1719:            public void recordCloneForPessimisticLocking(Object clone,
1720:                    UnitOfWorkImpl uow) {
1721:                if ((isLockQuery()) && lockingClause.isReferenceClassLocked()) {
1722:                    uow.addPessimisticLockedClone(clone);
1723:                }
1724:            }
1725:
1726:            /**
1727:             * INTERNAL: Helper method to determine the default mode. If true and quey has a pessimistic locking policy,
1728:             * locking will be configured according to the pessimistic locking policy.
1729:             */
1730:            public boolean isDefaultLock() {
1731:                return (lockingClause == null);
1732:            }
1733:
1734:            /**
1735:             * Return the fetch group set in the query.
1736:             * If a fetch group is not explicitly set in the query, default fetch group optionally defined in the decsiptor
1737:             * would be used, unless the user explicitly calls query.setShouldUseDefaultFetchGroup(false).
1738:             */
1739:            public FetchGroup getFetchGroup() {
1740:                return fetchGroup;
1741:            }
1742:
1743:            /**
1744:             * INTERNAL:
1745:             * Initialize fetch group
1746:             */
1747:            public void initializeFetchGroup() {
1748:                if (fetchGroup != null) {
1749:                    //fetch group already set.
1750:                    return;
1751:                }
1752:
1753:                //not explicitly set dynamically fetch group
1754:                if (fetchGroupName != null) {//set pre-defined named group
1755:                    fetchGroup = getDescriptor().getFetchGroupManager()
1756:                            .getFetchGroup(fetchGroupName);
1757:                    if (fetchGroup == null) {
1758:                        //named fetch group is not defined in the descriptor
1759:                        throw QueryException
1760:                                .fetchGroupNotDefinedInDescriptor(fetchGroupName);
1761:                    }
1762:                } else {//not set fecth group at all
1763:                    //use the default fetch group if not explicitly turned off
1764:                    if (shouldUseDefaultFetchGroup()) {
1765:                        fetchGroup = getDescriptor().getDefaultFetchGroup();
1766:                    }
1767:                }
1768:            }
1769:
1770:            /**
1771:             * Set a dynamic (use case) fetch group to the query.
1772:             */
1773:            public void setFetchGroup(FetchGroup newFetchGroup) {
1774:                fetchGroup = newFetchGroup;
1775:            }
1776:
1777:            /**
1778:             * Set a descriptor-level pre-defined named fetch group  to the query.
1779:             */
1780:            public void setFetchGroupName(String groupName) {
1781:                //nullify the fecth group refernce as one query can only has one fetch group.
1782:                fetchGroup = null;
1783:                fetchGroupName = groupName;
1784:            }
1785:
1786:            /**
1787:             * Return the fetch group name set in the query.
1788:             */
1789:            public String getFetchGroupName() {
1790:                return fetchGroupName;
1791:            }
1792:
1793:            /**
1794:             * Return false if the query does not use the default fetch group defined in the descriptor level.
1795:             */
1796:            public boolean shouldUseDefaultFetchGroup() {
1797:                return shouldUseDefaultFetchGroup;
1798:            }
1799:
1800:            /**
1801:             * Set false if the user does not want to use the default fetch group defined in the descriptor level.
1802:             */
1803:            public void setShouldUseDefaultFetchGroup(
1804:                    boolean shouldUseDefaultFetchGroup) {
1805:                this .shouldUseDefaultFetchGroup = shouldUseDefaultFetchGroup;
1806:            }
1807:
1808:            /**
1809:             * INTERNAL:
1810:             * Return if fetch group attributes.
1811:             */
1812:            public boolean hasFetchGroupAttributeExpressions() {
1813:                return (getFetchGroup() != null)
1814:                        && (getFetchGroup().hasFetchGroupAttributeExpressions());
1815:            }
1816:
1817:            /**
1818:             * INTERNAL:
1819:             * Return if fetch group attribute.
1820:             */
1821:            public boolean isFetchGroupAttribute(String attributeName) {
1822:                if (getFetchGroup() == null) {
1823:                    //every attribute is fetched already
1824:                    return true;
1825:                }
1826:                return getFetchGroup().getAttributes().contains(attributeName);
1827:            }
1828:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.