Oracle® Application Server TopLink Application Developer's Guide
10g Release 2 (10.1.2) Part No. B15901-01 |
|
Previous |
Next |
At its core, OracleAS TopLink is a runtime engine that provides Java or J2EE applications with access to persistent entities stored in a relational database. In addition to runtime capabilities, the Oracle Application Server TopLink Foundation Library includes the OracleAS TopLink Application Programming Interface (API). This API enables applications to access OracleAS TopLink runtime features, as well as development tools that simplify application development. The tools capture mapping and runtime configuration information in metadata files that OracleAS TopLink passes to the runtime.
Figure 1-1 OracleAS TopLink Components in the Development Cycle
OracleAS TopLink application development comprises three elements: the development environment, the OracleAS TopLink runtime, and the metadata that ties them together.
Development
To create an OracleAS TopLink application, map the object and relational models using OracleAS TopLink Mapping Workbench, and capture the resulting mappings and additional runtime configurations in the OracleAS TopLink project file (the project.xml
file). Then build a session configuration file (the sessions.xml
file) in the OracleAS TopLink Sessions Editor. These files together represent your entire OracleAS TopLink project.
During development, you leverage the OracleAS TopLink API to define query and transaction logic. When you use EJB entity beans, there is usually little or no direct use of the OracleAS TopLink API.
Runtime
The OracleAS TopLink Foundation Library provides the OracleAS TopLink runtime component. Access the runtime component either directly through the OracleAS TopLink API, or indirectly through a J2EE container when using EJB entity beans. The runtime engine is not a separate or external process; instead, it is embedded within the application. Application calls invoke OracleAS TopLink to provide persistence behavior. This function allows for transactional and thread-safe access to shared database connections and cached objects.
Metadata
OracleAS TopLink metadata is the bridge between the development of an application and its deployed runtime. Capture the metadata using OracleAS TopLink Mapping Workbench and the OracleAS TopLink Sessions Editor, and pass the metadata to the runtime using deployment project.xml
and sessions.xml
files. You can also hand-code these files using Java and the OracleAS TopLink API, but this approach is more labor-intensive.
The metadata, encapsulated in the project.xml
file and the sessions.xml
file, allows you to pass configuration information into the runtime environment. The runtime uses the information in conjunction with the persistent entities (Java objects or EJB entity beans), and the code written with the OracleAS TopLink API, to complete the application.
OracleAS TopLink Mapping Workbench is a graphical development tool that enables you to map between the object and relational models, and configure many of the OracleAS TopLink Foundation Library features. OracleAS TopLink Mapping Workbench creates an OracleAS TopLink project, the primary object in the OracleAS TopLink metamodel. Export the project as a single deployment XML file (the project.xml
file), which OracleAS TopLink uses in conjunction with the OracleAS TopLink runtime to provide the application-specific persistence capabilities.
Figure 1-2 OracleAS TopLink Mapping Workbench in an OracleAS TopLink Environment
OracleAS TopLink Mapping Workbench can import compiled entity classes (Java objects or EJB entity beans), as well as relational schema through a JDBC driver that the developer configures. Because OracleAS TopLink imports the object and relational models for mapping, you can develop the two models relatively independently from the O-R mapping phase of a project development.
Most OracleAS TopLink applications include a session configuration file, the sessions.xml
file, to simplify the application deployment process. The OracleAS TopLink Sessions Editor provides a graphical environment to configure the sessions.xml
file.
Use the sessions.xml
file to configure one or more sessions for the OracleAS TopLink project, and associate the sessions with the project. This approach allows you to specify individual configurations for each session and to add or modify:
Database (JDBC) login information different from the login information used during development (for example, external datasources for the connection pools of the host application server)
JTA/JTS transaction usage
Cache synchronization
Session broker (enables client applications to view multiple databases and projects as a single OracleAS TopLink session)
The Oracle Application Server TopLink Foundation Library includes a Java library that forms the runtime component of the product. It provides support and the API for the components that make up an OracleAS TopLink application. The API enables you to interact with OracleAS TopLink to retrieve and modify your application persistent entities.
Note: Although this chapter describes how these components fit into J2EE architectures, note that OracleAS TopLink also supports non-J2EE solutions. Chapter 2, "OracleAS TopLink Architectures", describes theses solutions in more detail. |
A session is the primary interface between the client application and OracleAS TopLink, and represents the connection to the underlying relational database. OracleAS TopLink offers several different session types, each optimized for different design requirements and architectures. The session manager configures and manages the session as a singleton within the application.
The most commonly used session is the server session, a singleton session that clients access on the server through a client session. The server session provides a shared cache and shared JDBC connection resources. OracleAS TopLink supports sessions for two-tier architectures, distributed applications, and multiple databases.
The OracleAS TopLink data access component provides access to JDBC connections through connection pooling, provided either by OracleAS TopLink or a host application server. This component manages the SQL generation required by the different query operations and reconciles any differences between JDBC drivers and SQL dialects. OracleAS TopLink offers many performance tuning options that optimize its data access capabilities.
OracleAS TopLink supplies an object level cache that guarantees object identity and enhances performance. You can configure the OracleAS TopLink cache and maximize application efficiency by reducing the number of times the application accesses the database. In a clustered environment, you can configure OracleAS TopLink to synchronize changes with other instances of the deployed application.
The OracleAS TopLink query framework provides you with the flexibility necessary to manage the complex persistence requirements of enterprise applications. The key features of this query framework include:
A rich set of query types to allow object retrieval, summary results, and raw data retrieval
The ability to specify search criteria using EJB QL, SQL, query by example, stored procedures, or OracleAS TopLink Expressions (for object model based queries)
Configuration options that enable you to specify how the query is executed, and to customize many of its performance optimizing features
You can define OracleAS TopLink queries using OracleAS TopLink Mapping Workbench, in Java code using the OracleAS TopLink API, or, in the case of EJB entity beans, through EJB Finders.
OracleAS TopLink provides the ability to write transactional code isolated from the underlying database and schema. OracleAS TopLink achieves this functionality through the Unit of Work.
The Unit of Work isolates changes in a transaction from other threads until it successfully commits the changes to the database. Unlike other transaction mechanisms, the Unit of Work automatically manages changes to the objects in the transaction, the order of the changes, and changes that may invalidate other OracleAS TopLink caches. The Unit of Work manages these issues by calculating a minimal change set, ordering the database calls to comply with referential integrity rules and deadlock avoidance, and merging changed objects into the shared cache. In a clustered environment, the Unit of Work also synchronizes changes with the other servers in the cluster.
If an application uses EJB entity beans, you do not access the Unit of Work API directly, but still benefit from its features. The integration between the OracleAS TopLink runtime and the J2EE container leverages the Unit of Work automatically.
By default, OracleAS TopLink allows the application to create transaction boundaries for all object-level changes. OracleAS TopLink explicitly manages the database transaction, and if it encounters problems, safely rolls back both the database changes and the object-level changes.
In the case of a J2EE application, you can configure OracleAS TopLink to synchronize with the JTA/JTS subsystem of the host application server. This feature allows an application to use container-managed transactions, rather than the default user-managed transactions.
Note that this functionality is not limited to EJB architectures. You can configure any OracleAS TopLink architecture to use container-managed transactions.
The OracleAS TopLink approach to persistence is based on metadata that defines the class structure (objects) and relational schema, along with other configuration information used by OracleAS TopLink at runtime. You can employOracleAS TopLink Mapping Workbench to define this metadata, and the OracleAS TopLink runtime component uses the metadata to provide the necessary persistence capabilities, using the reflective and introspective capabilities of Java.
The OracleAS TopLink application metadata model is based around the OracleAS TopLink project. The project includes descriptors, mappings, and different policies that customize the runtime capabilities.
Use the sessions.xml
file to configure sessions for the project. You can build and edit these files with the OracleAS TopLink Sessions Editor. The session manager uses the sessions.xml
configuration file during application initialization.
The OracleAS TopLink deployment project is the primary container for the metadata. A project usually represents an application and contains the mapping information for all persistent classes and their relationships. Each session (excluding the session broker) in the deployed application references a single project. Although you can build a project by coding it using the OracleAS TopLink API, we recommend that you create and manage the project in OracleAS TopLink Mapping Workbench, and use it to generate either an XML or Java source version of the project for use at runtime.
A descriptor represents the association between a persistent Java class and a relational table. The descriptor contains configuration information for the class level within a project, as well as a set of mappings for each of its persistent attributes. Many of the more advanced configuration options are set at the descriptor level. OracleAS TopLink Mapping Workbench supports most of these options, but a few of them must be set using the OracleAS TopLink API.
Mappings describe how the attributes of a mapped class are associated with columns in the database. OracleAS TopLink provides a sophisticated set of flexible and customizable mappings that allow for complex mapping scenarios between the object and relational models.
There are two types of mappings: direct mappings, and relationship mappings.
Direct mappings relate an attribute or attributes to a column or columns in the relational schema. OracleAS TopLink provides several direct mappings that allow for conversions between the types from the database and the object model's attribute types. Here are the direct mappings and their function:
Direct-to-field mappings map a Java attribute directly to a value database column.
Type Conversion mappings explicitly map a database type to a Java type.
Object type mappings match a fixed number of database values to Java objects.
Serialized object mappings store large data objects, such as multimedia files and BLOBs, in the database.
Transformation mappings offer specialized translations between how a value is represented in Java and in the database, such as when you map multiple fields into a single attribute.
OracleAS TopLink offers sophisticated relationship mapping, which enables you to represent object relationships based on the database table columns and foreign keys. Here are the relationship mappings and their functions:
One-to-one mappings represent simple pointer references between two Java objects. The references use any of foreign keys, target foreign keys, or variable classes to define the pointer.
Aggregate object mappings represent the relationship between a given object and a target object. The objects have a strict one-to-one relationship between the objects, and all the attributes of the second object are retrievable from the same table as the owning object.
Aggregate collection mappings represent the relationship between a single-source object and a collection of target objects. Unlike one-to-many mappings, in which there must be a one-to-one back reference mapping from the target objects to the source object, no back reference is required for the aggregate collection mappings, because the foreign key relationship is resolved by the aggregation (object & collection).
One-to-many mappings represent the relationship between a single-source object and a collection of target objects.
Many-to-many mappings represent the relationships between a collection of source objects and a collection of target objects. They require an intermediate table for managing the associations between the source and target records.
Object-relational mappings are mappings that leverage databases that support object-relational entity storage within tables.