Frequently Asked Questions

FAQ

 

Oracle Application Server Logo

Oracle Application Server 10g

Oracle TopLink

Frequently Asked Questions

  January 2006

 

Introduction

This FAQ addresses frequently asked questions relating to TopLink, the industry's most advanced object-to-relational persistence architecture. It provides a highly flexible and productive mechanism for storing Java objects and Enterprise Java Beans (EJBs) in relational database tables. TopLink offers developers excellent performance and choice, working with any database, any application server, any development toolset and process, and any J2EE architecture.

This FAQ is broken into a number of topic areas: About TopLink, Mapping, Querying, Developing with TopLink, and Object-XML Mapping.

 

About TopLink

  1. What is TopLink?
  2. What does TopLink cost? How is it licensed?
  3. Is TopLink an application or EJB server?
  4. Is there an overview of TopLink?
  5. What does TopLink include?
  6. What Java types does TopLink support?
  7. What databases does TopLink support?
  8. What kind of Java objects does TopLink support (e.g., Java Objects, EJBs, JavaBeans)
  9. What architectures does TopLink support (e.g., J2EE, EJB, JSP)?
  10. Can I use TopLink Foundation Library in my application server?
  11. With EJB 2.x and 3.0, do I still need TopLink?
  12. Does TopLink work in a multithreaded environment?
  13. Where can I find demos?
  14. Is there a TopLink user forum?
  15. Does TopLink have a GUI for mapping? Do I have to use it?
  16. Does TopLink perform code generation to persist objects?
  17. Does TopLink require me to subclass a special class or implement a special interface?
  18. Does TopLink use get/set methods to access persistent instance variables or does it go straight to instance variables?
  19. Why does TopLink use runtime reflection?
  20. Does TopLink support Oracle's grid computing initiative?
  21. Does TopLink comply with the JDO (JSR-012) specification?
  22. How do I configure my environment to work with TopLink?
  23. What additional functionality is available in the TopLink container-managed persistence (CMP) Foundation Libraries?
  24. What application servers does TopLink support?
 

Mapping

  1. What types of mappings does TopLink support?
  2. Can TopLink map the private and protected fields?
  3. Can you map columns, which allow NULL, to primitive Java types?
  4. Does TopLink support relationship mappings to interfaces as well as classes?
  5. How do I define a 1:1 variable class mapping using TopLink?
  6. What does it mean when a relationship mapping is privately owned?
  7. Is it possible to have multiple mappings to a single column?
  8. How do I generate and assign primary keys to objects I create?
  9. How can I define a selection criteria that is not only on foreign keys in a foreign reference mapping?
  10. When must I define a 1:1 mapping back from the target to the source object in a 1:M mapping? Are there alternatives?
  11. How can I customize TopLink's TypeConversionMapping to make use of a third-party class?
  12. What types can I use to hold the values for a collection mapping (1:M, M:M, and DirectCollection)?
  13. Can TopLink map BLOB data?
  14. What causes the "invalid column name" message in SQL Exception?
  15. I have set up sequencing, but I am getting an error when inserting a new object. What is wrong?
  16. How do TopLink inheritance mappings compare with Java class inheritance?
  17. I have a domain model with a lot of inheritance. Do I have to model my inheritance in TopLink?
  18. I have specified Transparent Indirection for my mappings. Do I have to do anything special to get it to work?
  19. Does TopLink provide a way to plug in user defined object/data transformations?
  20. How do I configure a mapping to use lazy loading (indirection)?
  21. How do I configure TopLink to delete dependent objects when an owner object is deleted (i.e., cascade delete)?
  22. How do I create relationships with TopLink?
  23. Is it possible to map an object to more than one table?
  24. Does TopLink support Oracle Database's XML data type?
 

Querying

  1. What query capabilities does TopLink support
  2. Does TopLink support EJBQL?
  3. What query capabilities do TopLink's expressions support?
  4. Where are TopLink Expressions documented?
  5. Can I have both EJBQL queries and SQL queries in the same application?
  6. What if I don't like the SQL TopLink generates for queries?
  7. Do TopLink Expressions support sub-queries?
  8. Does TopLink support table qualifiers and creators?
  9. How can I setup a Class to query only the instances of the parent class but not the subclass instances?
  10. What is the difference between a QueryByExample object and a selection object?
  11. Can you use more than one ExpressionBuilder subexpressions for a single expression?
  12. I keep getting multiple joins when I use multiple anyOf( ) functions in my expression, which is returning too many results. How do I get it down to one join instead?
  13. Why do dates not work through my JDBC driver?
  14. Why do I receive a conversion exception when reading date values from my database?
  15. How do I query historical data?
  16. Does TopLink support Oracle DB flashback queries?
  17. How do I ensure that query results incorporate data inserted or updated in an as yet uncommitted transaction?
 

Developing with TopLink

  1. Why is it faster to read objects from the database the second time than it is the first time?
  2. How does TopLink's object caching work?
  3. How does TopLink maintain object identity?
  4. What caching options does TopLink provide?
  5. Can TopLink create classes from tables and automatically map them?
  6. Can TopLink create tables from classes and automatically map them?
  7. Can TopLink create the database tables for me?
  8. Can I use stored procedures with TopLink?
  9. What is the difference between a DatabaseSession and a ServerSession?
  10. What are a UnitOfWork and a Client Session? What's the difference?
  11. What functionality does a UnitOfWork provide?
  12. How does TopLink know when to INSERT an object versus UPDATE it?
  13. What does registration mean in a UnitOfWork?
  14. Do I have to register new objects created in the UnitOfWork? What about non-private parts of the new objects? Should they be registered too?
  15. How do I instantiate my objects?
  16. How deeply can I nest a UnitOfWork? Can I use more than one UnitOfWork in parallel?
  17. What happened to the SessionsEditor in 10.1.3?
  18. How do I incorporate project XML generation into my build process?
  19. When do I shutdown a Server session?
  20. How do I load a TopLink project and obtain a Session?
  21. How do I configure TopLink logging?
  22. Can I use TopLink with Spring?
  23. How do I configure proxy authentication for database login?
 

Object-XML Mapping

  1. Can I map existing classes to XML?
  2. Is TopLink OXM JAXB compliant?
  3. Does TopLink OXM support document preservation?
 

About TopLink

  1. What is TopLink?

    TopLink is thethe industry's most advanced object-to-relational persistence framework. It provides a highly flexible and productive mechanism for storing Java objects and Enterprise Java Beans (EJBs) in relational database tables. TopLink offers developers excellent performance and choice, working with:

    • any database
    • any application server
    • any development toolset and process, and
    • any J2EE architecture.

    With release 10.1.3, TopLink includes support for mapping objects to XML documents and to legacy systems.

  2. What does TopLink cost? How is it licensed?

    Under the OTN license, TopLink is free to download for evaluation and use during development. A production license is included with all editions of OracleAS or can be licensed separately for $5K per CPU.

  3. Is TopLink an application or EJB server?

    TopLink is neither an application nor an EJB server—it is a pure Java class library. Because TopLink is certified 100% Pure Java, it is capable of running within any certified Java environment. With the enterprise classes, TopLink integrates with application and EJB servers. TopLink also provides container-managed persistence (CMP) for Entity Beans in Oracle Application Server Containers for J2EE (OC4J), IBM WebSphere, and BEA WebLogic.

    Refer to TopLink Application Architectures for more information.

  4. Is there an overview of TopLink?

    Yes, an overview is included in the Quick Tour.

  5. What does TopLink include?

    TopLink includes the following:

    • TopLink runtime library (called the Foundation Library)
    • Development tool: TopLink Workbench
    • Runtime tool: Web Client

  6. What Java types does TopLink support?

    For relational database, TopLink automatically converts values to and from the following Java types:

    • java.lang: Boolean, Float, Integer, String, Double, Long, Short, Byte, Byte[ ], Character, Character[ ]; all the primitives associated with these classes
    • java.math: BigInteger, BigDecimal
    • java.sql: Date, Time, Timestamp
    • java.util: Date, Calendar
  7. For other Java types, you must use an object-type, type conversion, or transformation mapping; or transform the value in the direct-to-field get/set methods. Refer to Simple Type Translator for information on supported Java types supported in XML mapping.

  8. What databases does TopLink support?

    TopLink can communicate with any relational database that has a JDBC-compliant driver available such as Oracle, IBM Universal Database, IBM Informix, IBM Cloudscape, Microsoft SQL Server, Microsoft Access, and Sybase Adaptive Server. TopLink supports both the JDBC 1.x and 2.0 standards.

    Refer to Oracle TopLink 10.1.3 Database Support for additional information.

  9. What kind of Java objects does TopLink support (e.g., Java Objects, EJBs, JavaBeans)?

    TopLink supports persistence for Plain Old Java Objects (POJOs) and JavaBeans, EJB bean managed persistence (BMP) in any app server, and container-managed persistence (CMP) for Entity Beans in Oracle Application Server Containers for J2EE (OC4J), IBM WebSphere, and BEA WebLogic.

  10. What architectures does TopLink support (e.g., J2EE, EJB, JSP)?

    TopLink is architecture neutral. It does not dictate how you architect your application and has been used in thousands of applications using J2SE, J2EE, EJB, JSP, Struts, etc.

  11. Can I use TopLink Foundation Library in my application server?

    TopLink Foundation Library can run in any application server running in a Java Virtual Machine (JVM). You can use regular Java objects or integrate with bean-managed persistence (BMP) Entity Beans. You need the TopLink container persistence (CMP) Foundation Libraries if you are going to use TopLink with CMP Entity Beans on Oracle Application Server Containers for J2EE (OC4J), BEA WebLogic, or IBM WebSphere application servers.

  12. With EJB 2.x and 3.0, do I still need TopLink?

    Yes. The EJB 2.x and 3.0 specifications define two types of persistence mechanisms for Entity Beans: container managed persistence (CMP) and bean managed persistence (BMP). But the specification is just that—a specification and not an implementation. TopLink provides an implementation of CMP for the Oracle Application Server Containers for J2EE (OC4J), BEA WebLogic, and IBM WebSphere application servers. TopLink can also be used for BMP in any EBJ server.

  13. Does TopLink work in a multithreaded environment?

    TopLink is 100% thread safe and can be used in JSP/Servlet or EJB applications that require thread safety.

  14. Where can I find demos?

    A number of demos can be found on the TopLink Demos page on OTN. Extensive examples are also included with the TopLink distribution download accessible from the TopLink page on OTN.

  15. Is there a TopLink user forum?

    Yes. The TopLink Forum on OTN is very active with participation from both TopLink users and members of the TopLink engineering team.

  16. Does TopLink have a GUI for mapping? Do I have to use it?

    Yes, TopLink does have mapping tools and no, you don't have to use them. Mapping tools are integrated into JDeveloper and also available in the stand-alone TopLink Workbench. They provide a great productivy boost over hand editing XML deployment descriptor files or coding directly on the TopLink Foundation Library API. The TopLink mapping tools identify problems in your mappings so that you don't have to run your code to discover you've made and error. Tracking down a mapping problem on a large project with 100's of XML mapping files is not a trivial task. The TopLink mapping tools make O/R mappings manageble.

  17. Does TopLink perform code generation to persist objects?

    Because TopLink uses a metadata architecture, it does not need to generate any code to enable your application's persistence.

    TopLink can generate:

    • The TopLink project and associated descriptors into a single Java class that, when compiled, can be used in place of the runtime deployment XML file.
    • A table creator Java class that when compiled can create all the tables on the database with required constraints.
    • Source code for the classes defined in the project.
    • Java object model, EJB-CMP EntityBean model, and EJB-BMP EntityBean model from an existing database schema.

    TopLink does generate classes for EJB2.0 CMP.

  18. Does TopLink require me to subclass a special class or implement a special interface?

    TopLink does not force a developer to subclass from one of its classes or use special types to maintain relationships. You could build a domain model to map persistently that has no dependencies on TopLink.

    Refer to the Building Superior Java Applications whitepaper for details.

  19. Does TopLink use get/set methods to access persistent instance variables or does it go straight to instance variables?

    TopLink can use either get/set methods or directly access instance variables. Protected and private instance variables can also be directly accessed.

    See also Can TopLink map the private and protected fields?.

  20. Why does TopLink use runtime reflection?

    In an interview given to The Server Side, TopLink architect Mike Keith answered this question by saying:

  21. TopLink has always made extensive use of reflection. We have also always claimed that execution in a reflection-based architecture is no slower than non-reflection-based products, as long as the reflection artifacts can be cached. This has proven to be true and other products have also backed this claim up. In fact anybody that does a quick benchmark of reflective method invocation or field access can see similar results.

    Refer to the entire article, TopLink: 10 Years of Persistence, for more information.

  22. How does TopLink support Oracle's grid computing initiative?

    TopLink allows applications to be developed and enhanced flexibly, to fit into a service-oriented architecture. By enabling loose coupling between the application and persistent data, TopLink helps developers optimize their applications for the grid. In addition, TopLink provides crucial persistent functionality used in applications and other Oracle development frameworks.

  23. Does TopLink comply with the JDO (JSR-012) specification?

    No, TopLink does not implement the complete JDO 1.0 specification. Although support for some of the JDO API was provided in past releases of TopLink, this API is deprecated in 10.1.3 and will be removed in a future release.

  24. How do I configure my environment to work with TopLink?

    After installing TopLink, you must configure your development system to use the TopLink classes, as well as allow TopLink to access the core Java classes, Java virtual machine (JVM), and your domain classes. Refer to Using an Integrated Development Environment and Using TopLink Workbench for details. The MetaLink Knowledge Base also contains detailed information. In general you must:

    • Ensure that a valid JVM exists in your path.
    • Ensure that the TopLink Foundation classes (toplink.jar) are in your classpath.
    • Ensure that all domain classes, that you will want to map persistently using TopLink, are included in your classpath.
    • Ensure that the path to your JDBC driver classes are included in your classpath.

  25. What additional functionality is available in the TopLink container-managed persistence (CMP) Foundation Libraries?

    TopLink CMP Foundation Libraries are supersets of the TopLink Foundation Library. You get all of the same functionality. However, these libraries also allow you to use TopLink with CMP Entity Beans on BEA WebLogic and IBM WebSphere application servers.

  26. What application servers does TopLink support?

    TopLink can be used within any compliant J2EE and Web container with various persistence architectures. However, TopLink does have explicit support and certification testing performed with each release on: Oracle Application Server, BEA WebLogic, and IBM WebSphere. Other containers are supported through standard J2EE integration and may require minor customizations.

    Refer to the TopLink Support and Certifications page on OTN for more information.

 

Mapping

  1. What types of mappings does TopLink support?

    TopLink provides a complete set of mappings for both direct and reference relationships. This allows developers to map from the object model to the relational model without one model dictating to the other.

    • Direct Mappings
      • Direct To Field
      • Object Type
      • Type Conversion
      • Serialized Object
      • Direct To XML
      • Direct Collection
      • Direct Map
      • Transformation
    • Relationship Mappings
      • Aggregate Object
      • One To One
      • Variable One To One
      • One To Many
      • Many To Many
      • Direct Collection
      • Aggregate Collection
    • Object-relational Mappings
      • Structure
      • Reference
      • Nested Table
      • Array
      • Object Array

  2. Can TopLink map the private and protected fields?

    TopLink can access non-public fields, methods, or both. Grant the ReflectPermission permission to TopLink to suppress default Java language access control checks when using reflected members and constructors. The security policy file grants permissions using the policy tool application.

  3. Can you map columns, which allow NULL, to primitive Java types?

    Normally no, because primitive types in Java cannot be null. If the field value is null and TopLink tries to assign null to a primitive, Java raises a NullPointerException. The easiest way to avoid this problem is to ensure that all the database fields that store primitive values cannot be null. Using the TopLink Workbench, you can also configure the mapping to use a default value for null by using the NullValue property on all direct mappings. To do this in code, you could use:

    mapping.setNullValue(new Integer(0));

    In addition, using Oracle TopLink Workbench, you can configure NullValue for all mappings and descriptors on the DatabaseLogin. To do this in code, you could use:

    login.setDefaultNullValue(int.class, new Integer(0));

  4. Does TopLink support relationship mappings to interfaces as well as classes?

    Yes, you can define mappings that reference interfaces instead of classes. TopLink calls this a variable class mapping. Interfaces can be defined for a single class or for a set of implementors. Querying on the interface will query each of the implementors.

  5. How do I define a 1:1 variable class mapping using TopLink?

    TopLink supports variable 1:1 mappings in which the source object has a reference to one of two or more non-inherited classes. To define a variable 1:1 mapping, the target classes of the mapping must be related through an interface.

    You must define an interface descriptor for the interface, and the target classes must implement the interface. The selection criteria of the mapping will be defined using query keys from the target interface, and a type field in the source object's table will store the type of object that is referenced.

  6. What does it mean when a relationship mapping is privately owned?

    TopLink provides the ability to define any reference mapping (1:1, 1:M, and M:M) as privately owned . This means that when the source object is deleted so are all its privately owned parts.

  7. Is it possible to have multiple mappings to a single column?

    Yes, provided that only one of the mappings is made writeable. TopLink requires one writeable mapping for which it will retrieve the required value from the object when constructing the row for the database. You can configure a mapping to be read-only or, in the case of a 1:1 mapping, may configure a target foreign key. Both of these will allow an additional mapping of a field.

  8. How do I generate and assign primary keys to objects I create?

    TopLink supports sequencing by assigning the object a unique identifier when it is inserted into the database. TopLink assigns the identifier here because this is the first time TopLink gets access to the object. When using native sequencing on IBM Informix, Microsoft SQL Server, and Sybase Adaptive Server, the identifier cannot be obtained until after the insert.

    Within a UnitOfWork, all registered new objects can also be assigned sequence numbers through the assignSequenceNumber(Object) and assignSequenceNumbers() methods.

  9. How can I define a selection criteria that is not only on foreign keys in a foreign reference mapping?

    Foreign reference mappings internally use a selection query to read objects from the target. This selection query is like any other read query, and can be accessed to provide advanced customization on the mapping's query. The selection query can itself be changed to provide a custom query if complex querying is required. If you have a relationship that is not solely based on foreign keys, an expression can be given to the mappings selection criteria.

    ExpressionBuilder builder = new ExpressionBuilder(); mapping.setSelectionCriteria( builder.getParameter("EMPLOYEE.ADDR_ID").equal(builder.getField("ADDRESS.ID").and(builder.getField( "ADDRESS.TYPE").equal("home"));

  10. When must I define a 1:1 mapping back from the target to the source object in a 1:M mapping? Are there alternatives?

    When a source object defines a 1:M mapping to a target object, you must also create a 1:1 mapping from the target object back to the source object to populate the foreign key field in the target table. Without this back-mapping, TopLink does not know how to populate this foreign key field in the target table.

    Use an AggregateCollectionMapping to represent the aggregate (privately-owned) relationship between a single source object and a collection of target objects. Unlike the normal 1:M mapping, there is no need for setting the 1:1 back reference, because the foreign key relationships have already been resolved by the aggregation.

    You can avoid setting the 1:1 back reference by defining a direct-to-field mapping on the foreign key field. In this case, the application must make sure that the object model picks up the correct value from the source object. However, this alternative may result in an overly complex implementation or object model. We recommend you use a 1:1 back reference.

    Another alternative is to define a M:M mapping from source to target and create a relation table. This is the best solution because it ensures that the object model is consistent with the data model. Neither model stores information about the relationship in the target object, and multiple source objects referencing the target can be supported.

  11. How can I customize the TopLink TypeConversionMapping to make use of a third-party class?

    TopLink provides a rich set of type conversions between database types and the Java types supplied with the JDK. It is possible to make use of additional or third-party types in a domain class through customization of TopLink.

    TopLink makes use of a singleton ConversionManager (found in the oracle.toplink.helper package). Developers can subclass this class and create a custom conversion manager with the additional conversions required. At system startup, an instance of this class must be provided as the singleton conversion manager.

  12. What types can I use to hold the values for a collection mapping (1:M, M:M, and DirectCollection)?

    By default, TopLink uses the Vector class for collections. TopLink supports other collection types through the mapping's container policy. You can define a new container policy that uses other types of containers that do not implement the Collection or Map interfaces.

  13. Can TopLink map BLOB data?

    TopLink maps BLOB data from the database into a byte array in the object model. BLOBs can also be mapped through transformation or serialized mappings to be complex objects in the object model. Because many JDBC drivers have problems dealing with large binary data, TopLink supports multiple options. By default, TopLink binds binary data into the SQL statement. TopLink also supports printing the data using the JDBC binary escape clause or native SQL.

    Binding is enabled or disabled on the DatabaseLogin through the dont/useByteArrayBinding() property. When binding is enabled, TopLink parameter binds the binary value. Because some JDBC drivers may have problems binding large values (but can support large values through streams), TopLink also supports stream binding. This is enabled in DatabaseLogin through the useStreamBinding() property. Some drivers have limits to BLOB sizes that may require your application to split the binary data into multiple fields or use a direct collection or 1:M mapping to map the data.

    Large string data may also require binding. You can configure TopLink to bind string data through the useStringBinding(int) property on DatabaseLogin.

    • Serialized Mapping - TopLink also provides the ability to map object structures, that would not map easily to relational tables, with a SerializedObjectMapping. Mappings of this type serialize the object structure from the root attribute and store it into a BLOB field on the database. When the object is read from the database, it is de-serialized into its original structure. This mapping does not require the developer to perform the serialization, but instead to make sure that objects in the structure are serializable and self-contained.
    • TypeConversionMapping - The Oracle JDBC thin driver uses enhanced methods to read and write LOB (BLOB and CLOB) information. TopLink now supports reading and writing LOB information in this way. To tell TopLink that the database type you are mapping to is a LOB (BLOB or CLOB) type, map the attribute as a TypeConversionMapping. TopLink will then call the appropriate methods.

  14. What causes the "invalid column name" message in SQL Exception?

    This message is given because SQL is trying to write to a non-existent column in the table. Possible reasons for this could be: The field names specified in the foreign reference mapping are not properly specified. The column name is misspelled or in the wrong case. Check the methods where field names are set in the mapping.

  15. I have set up sequencing, but I am getting an error when inserting a new object. What is wrong?

    It is likely that, although you set up the descriptor's and project's sequencing information, the sequence table is not yet initialized. The sequence table must contain one row for each sequence name you are using, and have an initial value in the count column. For example, if your Employee and Address descriptors are using sequencing, an initialized sequence table might look like this:

    SEQ_NAME SEQ_COUNT
    EMPLOYEE 0
    ADDRESS 0

  16. How do TopLink inheritance mappings compare with Java class inheritance?

    When TopLink maps a Java inheritance hierarchy, each class in the hierarchy defines a TopLink descriptor. Each subclass' descriptor defines its parent to be its superclass.

    This inheritance hierarchy can be reflected in the database in many ways. TopLink requires a common table for the root class that must be shared by all the subclasses. The subclasses are free to define additional tables to store their additional data or can share their parent's tables. TopLink uses an indicator field in the root table to determine the class to instantiate from the row; however, this mechanism can be customized if required.

    A common misconception is that a system with a root domain class must be mapped using this kind of inheritance mapping. Typically, this should be done only where the database schema dictates or the object model allows similar types to be stored together. If it does not make sense to define a table for the root class, this class cannot be mapped at all, or can be mapped as an interface. TopLink supports querying on interface descriptors and does not require that they be mapped to a database table.

    In a mapped inheritance hierarchy, all subclasses must share the root's primary key. In case of multiple tables, the primary key in the additional table can be named differently from the one located in the root table. In this case, a name mapping must be provided in the descriptor, or in complex cases a multiple table join expression. The root table's primary key will always be used internally in TopLink and for caching.

  17. I have a domain model with a lot of inheritance. Do I have to model my inheritance in TopLink?

    No. Inheritance in TopLink must be modeled relationally only if you plan to query or write the super classes.

    Consider an example where class A has a subclass B. A has two attributes: id and description. On the database, there is both an ID and DESCRIPTION field on B's table, but A has no table associated with it. With TopLink, you simply map B; you can ignore its superclass A . The fields contained in A can be mapped through mapping B.

    This allows you to map an object model but ignore certain classes that should not logically be persisted (such as abstract classes, "PersistentObject" superclasses, etc.).

  18. I have specified Transparent Indirection for my mappings. Do I have to do anything special to get it to work?

    In general, no. You do, however, have to make sure that your attribute is of the correct type. In the case of collection indirection, you must make sure that your attribute type is java.util.Collection. For one-to-one indirection, you must make sure that the attribute type is an interface, since TopLink replaces the target object with a special wrapper object that implements the same interfaces as the target class. If the variable is not of the correct type, TopLink will not be able to put the value into it.

    Serialization can also cause trouble with Transparent Indirection. Unless Remote Sessions are being used, Transparent Indirection cannot be instantiated on the client. The same holds true with regular indirection; however, the point at which the indirection is instantiated is now different with Transparent Indirection. Using traditional ValueHolders , the indirection is triggered as soon as the getAttribute() method is called. Before an object that is referenced using Transparent Indirection can be accessed on the client, a message must be sent to it on the server (for example, getSize() for transparent collections, hashCode() for transparent one-to-ones, and so on.).

  19. Does TopLink provide a way to plug in user defined object/data transformations?

    Yes. A Transformation Mapping is used to define custom transformations.

    See Transformation Mappings in the Oracle TopLink Developer's Guide for more information.

  20. How do I configure a mapping to use lazy loading (indirection)?

    Lazy loading for 1:1 or 1:Many relationships can be configured either in the MappingWorkbench, JDeveloper Mapping Editor, or directly in Java.

    Refer to Configuring Indirection in the Oracle TopLink Developer's Guide for instructions.

  21. How do I configure TopLink to delete objects when an owner object is deleted (i.e., cascade delete)?

    Objects whose lifecycle is coupled to their owner are called "privately owned" objects in TopLink. Privately owned objects are deleted when their parent is deleted. The configuration of a relationship as privately owned is specified on the descriptor either in the MappingWorkbench, JDeveloper Mapping Editor, or directly in Java.

    Refer to Specifying Private or Independent Relationships in the Oracle TopLink Developer's Guide for detailed instructions.

  22. How do I create relationships with TopLink?

    TopLink supports many different types of relationship mappings (1:1, 1:Many, etc.) that you can used to define relationships. Refer to Relational Mapping Types, XML Mapping Types, and EIS Mapping Types in the Oracle TopLink Developer's Guide for a complete list of available mappings.

  23. Is it possible to map a class to more than one table?

    Yes. Attributes from a single class can be mapped to multiple tables.

    Refer to Configuring Multitable Information in the Oracle TopLink Developer's Guide for more information.

  24. Does TopLink support Oracle Database’s XML data type?

    Yes, using a direct-to-XMLType mapping you can map XML data in the form of a String or an org.w3c.dom.Document object to an XMLType column in an Oracle Database (introduced in version 9.2.0.1). TopLink also provides expression operators to support querying XMLType. You'll need to adjust the Workbench and runtime classpath to include the Oracle Database xdb.jar.

    Refer to Direct to XMLType Mapping in the Oracle TopLink Developer's Guide for more information.

 

Querying

  1. What query capabilities does TopLink support?

    In addition to EJB QL and SQL, TopLink's rich expression framework allows almost any query to be specified in terms of the object model. These expressions are then dynamically turned into the appropriate SQL at runtime.

    When you define a descriptor with the TopLink Workbench, you can use the Queries tab to specify EJB QL and SQL queries and finders to use for database access. The Queries tab contains two additional tabs: Named Queries and Custom SQL.

    For 2.0 CMP projects, the ejb-jar.xml file stores query lists. You can define the queries in the file and then read them into the TopLink Workbench, or define them on the Queries tab and write them to the file.

  2. Do TopLink queries support EJBQL?

    A ReadAllQuery and ReadObjectQuery have the method setEJBQLString(String EJBQLString), which you can use to set the selection criteria of the query to a String containing EJB QL.

    The TopLink Workbench also supports EJB QL queries.

  3. What capabilities do TopLink's expressions support?

    TopLink's rich expression framework allows almost any query to be specified in terms of the object model. These expressions are then dynamically turned into the appropriate SQL at runtime. Features of the expression framework include:

    • Operators, such as =, !=, <, <=, =, >, >=, in, like, between
    • Logical operators, such as AND, OR, NOT
    • Traversing 1:1, 1:M, M:M relationships, joins (including outer joins on most databases)
    • Object comparison
    • Common database functions and user defined functions
    • Keyword searching
    • Query by Example

  4. Where are TopLink Expressions documented?

    Refer to TopLink Expressions in the Oracle TopLink Developer's Guide.

  5. Can I have both EJBQL queries and SQL queries in the same application?

    Yes. Queries may be specified using any of the query types supported by TopLink in any application. TopLink Expression, SQL, and EJBQL queries can all be used in a single application if desired.

  6. What if I don't like the SQL TopLink generates for queries?

    Queries can be specified directly in SQL if the SQL generated by TopLink for Expression or EJBQL based queries is unacceptable. However, this is not typically necessary.

    The SQL generated by TopLink for reading, writing, and deleting objects based on a class's descriptor can also be overridden.

    Refer to SQL Queries and Configuring Custom SQL Queries for Basic Persistence Operations in the Oracle TopLink Developer's Guide for more information.

  7. Do TopLink Expressions support sub-queries?

    Yes. TopLink Expressions provide subqueries to support subselects.

    Refer to Subselects and Subqueries in the Oracle TopLink Developer's Guide for more information.

  8. Does TopLink support table qualifiers and creators?

    In some databases or database configurations, table names must be qualified with either a table owner/creator name or a table space name. TopLink allows you to specify these either on a per session basis or individually in each descriptor.

    By sending the following method to the DatabaseLogin object before you login to the session, the table names of all of the registered descriptors will be qualified with the creator or qualifier name.

    login.setTableQualifier("PERSONNEL");

    Individual qualifiers can be entered using the TopLink Workbench on a per-table basis.

  9. How can I setup a Class to query only the instances of the parent class but not the subclass instances?

    By default reading a parent class will return all the instances of the parent and its subclasses. If only the instances of the parent class are desired, then the inheritance property "should read subclasses" can be turned off on the parent descriptor. If the subclass instances are not desired for a specific query, then the query expression can filter the children. The following gives an example of a subclass filtering expression:

    Expression builder = new ExpressionBuilder(); Expression expression = builder.getField("typeIndicatorField").equal("parent");

  10. What is the difference between a QueryByExample object and a selection object?

    A QueryByExample object does not replace a selection object in a query. A QueryByExample object allows you to create an object with various attributes set, to give you better control over your query. A selection object is an easy mechanism to query by the primary key only. Any other attributes in this object are ignored.

  11. Can you use more than one ExpressionBuilder subexpressions for a single expression?

    No, doing this might seem to be working in some cases but would fail in expressions using joins or complex expressions. We recommend that you always use the same ExpressionBuilder in a single expression.

    If your expression contains subselects, then the subquery may have a different ExpressionBuilder, but this should be the only case.

  12. I keep getting multiple joins when I use multiple anyOf( ) functions in my expression, which is returning too many results. How do I get it down to one join instead?

    Each time you use anyOf() in your expression, you get another join in your database. For example, the TopLink expression:

    emp.anyOf("managedEmployees").get("lastName").equal("Smith").and(emp.anyOf("managedEmployees").get("firstName").equal("John"));

    will return any Employee who manages a John or a Smith, not just John Smith. This is because a separate join is performed for each anyOf() . To force a single join, the same anyOf node must be used for each query:

    Expression managedEmployees = emp.anyOf("managedEmployees"); ... managedEmployees.get("lastName").equal("Smith").and(managedEmployees.get("firstName").equal("John"));

  13. Why do dates not work through my JDBC driver?

    Some JDBC drivers do not support the JDBC date syntax. In this case TopLink also supports printing date values in their native database syntax. To enable native SQL printing set the useNativeSQL() property in DatabaseLogin.

  14. Why do I receive a conversion exception when reading date values from my database?

    TopLink optimizes how data is converted from the JDBC driver. This includes date type where TopLink handles the conversion from strings internally. Some JDBC drivers, when returning the string representation of the date, return the wrong string syntax. In this case use the dontOptimizeDataConversion() property in DatabaseLogin to disable this optimization.

  15. How do I query historical data?

    TopLink provides support for querying historical versions of an object through an ObjectLevelReadQuery with an AsOfClause, in expressions using an asOf operator, and through Historical Client Sessions. When using a non-Oracle database platform you can query against a historical schema of your own design that you configure with a TopLink HistoryPolicy. Oracle9i Database Server (or later) automatically maintains historical versions of objects and extends SQL with an AS_OF clause used to query this historical data. Oracle refers to these as flashback queries. To use TopLink's historical query support with Oracle9i database you just have to configure your session with an OraclePlatform.

    Refer to Historical Queries, Historical ClientSessions, and Configuring a HistoryPolicy in the Oracle TopLink Developer's Guide for more information.

  16. Does TopLink support flashback queries?

    Yes, flashback queries can be performed using TopLink's historical query support. Historical queries are described in the answer to the question: How do I query historical data?

    There is also a how-to document on OTN on Oracle TopLink Flashback Queries.

  17. How do I ensure that query results incorporate data inserted or updated in an as yet uncommitted transaction?

    Uuse conforming on a query. Calling the conformResultsInUnitOfWork() method on a query before it is executed will ensure that uncommited changes will be accounted for in the query result. This has the downside of requiring extra processing of a query result and should only be used when necessary.

    Refer to Using Conforming Queries and Descriptors in the Oracle TopLink Developer's Guide for more information.

 

Developing with TopLink

  1. Why is it faster to read objects from the database the second time than it is the first time?

    If caching is enabled for a particular class, then querying for an instance of that class a second time will be faster than the first time. This is due to TopLink finding the object in the cache and thus saving the cost of constructing that object from database data. Caching is specified at the class level and various cache types can be selected.

    See also What caching options does TopLink provide?

  2. How does TopLink's object caching work?

    TopLink uses an object's primary key field values, as defined in the descriptor, to identify an object uniquely. The object cache, maintained within either the DatabaseSession or ServerSession, caches objects based on class and primary key values.

  3. How does TopLink maintain object identity?

    Each class type has an identity map in a DatabaseSession or ServerSession. Objects are stored in these identity maps based on the object's defined primary key field values.

    TopLink guarantees object identity by checking the cache during reads to ensure that for any given set of relational data (that maps to an object), only a single instance of the class is created, so multiple reads of the same object return the same reference. This feature is extremely valuable in ensuring that object changes made within the application stay in sync with other users of the objects. Object identity also allows the building of complex structures involving circular references without any extra work or potential infinite loop.

    Violating object identity refers to creating objects that already exist either in the cache (same class and primary key) or in the database, instead of using the object returned from TopLink.

  4. What caching options does TopLink provide?

    TopLink uses six types of caches that can be configured on a per class basis.

    • None (NoIdentityMap )
    • Full (FullIdentityMap )
    • Cache (CacheIdentityMap, fixed size, drops least recently used objects)
    • Weak (WeakIdentityMap )
    • Soft Cached Weak (SoftCacheWeakIdentityMap, default for Java 2)
    • Hard Cached Weak (HardCacheWeakIdentityMap )
  5. Can TopLink create classes from tables and automatically map them?

    Yes. TopLink can generate EJB Entity Beans or Java classes from tables and automatically map them to the source tables.

    Refer to Generating Descriptors and Classes from Database Tables in the Oracle TopLink Developer's Guide for more information on using the TopLink Workbench and the JDeveloper documentation for detailed instruction on how to do this in JDeveloper.

  6. Can TopLink create tables from classes and automatically map them?

    Yes. In the Mapping Workbench you can either generate tables for all classes in you project or for select classes. Select the class or classes you wish to generate tables for, right click, and choose "Generate Tables from Descriptors". From there you can either choose to generate tables for only the selected classes or all classes in the project.

  7. Can TopLink create the database tables for me?

    Yes. In the Mapping Workbench you can right click on any table and choose "Create on Database" and then choose "All Tables" or "Selected Tables". You must be logged into the database for this menu option to be enabled. When using TopLink in JDeveloper, you can create tables using the offline database feature.

    Refer to Generating Tables on the Database in the Oracle TopLink Developer's Guide for more information.

  8. How can I use stored procedures with TopLink

    Stored procedures are accessed through TopLink's custom SQL and stored procedure call support. To call stored procedures, you can use the session API executeSQL(...) and executeNonSelectingSQL() or in use a descriptor's or mapping's custom SQL properties. Stored procedures with output parameters are supported through the StoredProcedureCall object. You can also call Stored procedures with output parameters inside the descriptor's and mapping's properties and execute them directly on the session through the DataReadQuery and DataModifyQuery objects.

    Refer to Using a StoredProcedureCall in the Oracle TopLink Developer's Guide for more information.

  9. What is the difference between a DatabaseSession and a ServerSession?

    TopLink provides two types of sessions for developers to connect with the database:

    • DatabaseSession -- For two-tier applications in which a single JDBC connection will service all database requests.
    • ServerSession -- For three-tier applications. This allows multiple clients to access the database through a configurable JDBC connection pool. TopLink's three-tier solution also includes a ClientSession and a RemoteSession. Each client to the ServerSession must communicate through a ClientSession. The RemoteSession can be used across RMI, CORBA or EJB from a client applet or application.

    DatabaseSession provides the UnitOfWork functionality and lower level access to the database through insertObject(...), updateObject(...), deleteObject(...), beginTransaction(), commitTransaction(), and rollbackTransaction(). These are not provided through the ServerSession, ClientSession, and RemoteSession because merging of objects into a shared cache requires the more complicated behavior delivered with TopLink's UnitOfWork .

    Refer to Session Types in the Oracle TopLink Developer's Guide for more information.

  10. What are a UnitOfWork and a Client Session? What's the difference?

    A client session gives the client access to the database (and the session cache) through the server session. A client session does not support database modification; it cannot create, change, or delete objects directly. To perform database modification, a client must obtain a Unit of Work from the client session. The Unit of Work acts as an exclusive transactional object space, and also ensures that any changes that are committed to the database also occur in the session cache.

    Refer to Session Concepts and Understanding TopLink Transactions in the Oracle TopLink Developer's Guide for more information.

  11. What functionality does a UnitOfWork provide?

    TopLink's UnitOfWork provides developers with an object transaction mechanism. It provides a working copy of an object and its related objects so that changes can be made in isolation. Upon commit, the UnitOfWork identifies what has changed wit