Wednesday, May 23, 2007

Simple little things of Programming - How to Toplink?

Here is what I compiled a short high level overview on Toplink:

Object persistence impedence mismatch is one of the most struggled areas of Software development. Using Object Oriented philosophy has become a norm to build scalable and complex systems. Pretty much all the current day applications use databases to store some type of data. The data are usually stored in relational model but the Objects are more hierarichal in structure. Due to the difference of technologies and the perception of the models it becomes harder to map the application with the data schema. The team ownership of the database schema is also usually different from the teams who design and build the application. That makes even harder to control how the internal components of an application are constructed. Using the jdbc drivers directly to retrieve and persist data makes the application harder to scale and manage. It also makes it more error prone and slower. To solve the problem O-R mappers come to rescue.

What is a domain model?
The domain model is a conceptual model of a system that describes the various entities involved in a system and their relationships. The domain model also documents the key concepts and the vacabulary of the system. The model displays the relationships among all major entities within the system and usually identifies their important methods and attributes. This means that the domain model provides a structural view of the system. In most cases the simple Java classes are used to create a domain model. These Java objects contain the attributes that together represent a state of the object. This is the state of the object that is persisted in the database.

What is a data model?
Data model defines how data are structured and used in the database. There are three model elements that a data model defines:

  • Structural:
  • A collection of data structures that are used to create databases representing the entities or objects modeled by the database.
  • Integrity:
  • A collection of rules governing the constraints placed on these data structures to ensure structural integrity.
  • Manipulation:
  • A collection of operators, which are applied to the data structures, to update and query the data contained in the database.

What is a Toplink?
Toplink is at core an O-R mapper, which stands for Object-Relational mapper. Oracle Toplink builds high-performance applications that store persistent object-oriented data in a relational database. It successfully transforms object-oriented data into either relational data or Extensible Markup Language (XML) elements. Using Toplink we can integrate peristence and object-transformation into our application, which staying focussed on our primary domain problem by taking advantage of an efficient, flexible and field proven solution.
Toplink is suitable for use with a wide range of Java 2 Enterprise Edition (J2EE) and Java application architectures. Use Toplink to design, implement, deploy and optimize an advanced, object-persistence and object-transformation layer that supports a variety of data sources and formats including the following:

  • Relational –
  • For transactional persistence of Java objects to a relational database accessed using Java Database Connectivity (JDBC) drivers.
  • Object-Relational –
  • For transactional persistence of Java objects to special purpose structured data source representations optimized for storage in object-relational databases such as Oracle Database.
  • Enterprise Information System (EIS) –
  • For transactional persistence of Java objects to a nonrelational data source accesses using a J2EE Connector Architecture (J2C) adapter, and any supported EIS recore type including indexed, mapped or XML.
  • XML –
  • For non-transactional, non-persistent (in-memory) conversion between Java Objects and XML Schema Document (XSD)-based XML documents using Java Architecture for XML Binding (JAXB).

Toplink includes support for Container-managed persistence (CMP) containers from a variety of vendors including Oracle OC4J, IBM WebSphere and BEA WebLogic Server and support for base classes that simplify the Bean-managed persistence (BMP). The Toplink runtime lets the application exploit the mapping metadata with a simple session façade that provides in-depth support for data-access, queries, transactions (both with or without external transaction controller) and caching.
The following diagram can illustrate the Toplink Runtime :

Toplink Key Features
Toplink provides an extensive and thorough set of features. Java developers can use these features to rapidly build high-performance enterprise applications that are both scalable and maintainable.
Some of the primary features of Toplink are the following:

  • Nonintrusive, flexible, metadata-based architecture.

  • Comprehensive visual Toplink Workbench

  • Advanced mapping support and flexibility (relational, object-relational, EIS and XML)

  • Object caching support

  • Query flexibility

  • Just-in-time reading

  • Caching

  • Object-level transaction support and integration

  • Locking

  • Multiple performance tuning options

  • Architectural flexibility

Getting Started
So, how to start with Toplink? What exactly does it do? How does it create the mappings? Where does the mapping reside? These are the right kind of questions to ask if you are starting or thinking of using Toplink. The goal of any O-R mapping is to create an object graph in memory that represents the Object hierarchy of the domain model and has all the definitions of its relationship to the data model. Oracle Toplink imports the database tables, Java classes and their relationships as a set of XML files. Once these files have been created, the object graph is built either by importing the project as an XML document or a Java class. The XML or the Toplink project Java class contains all the mappings to create the Object graph required to persist or query against the database and is one core artifact required to run a Toplink enabled application. The Toplink runtime uses the XML mapping or the Project Java class to create the Object graph in memory which is accessed by the Toplink session to provide the data access. Toplink makes the application agnostic to the details of the database as it is now managed in one single layer. Toplink also provides a set of Java interfaces (APIs) to access or manipulate its Objectgraph.

Understanding Toplink Metadata
The Toplink metadata is the bridge between the development of an application and its deployed run-time environment. Capture the metadata using Toplink Workbench (or JDeveloper) and pass the metadata to the run-time environment using deployment XML files (such as sessions.xml). We could also code these files using Java and the Toplink API but this approach is more labor-intensive. Toplink Workbench also provides an option to export the mapping as a Java class.
The metadata encapsulated in deployment XML files, lets you pass configuration information into the run-time environment. The run-time environment uses the information in conjunction with the persistent entities and the code written with the Toplink API to complete the application. Here is an example of a simple interface. The illustration shows how Toplink uses metadata (In XML format) to manage the data model, classes and the database table information.

Advantages of Toplink Metadata Architecture
The Toplink metadata architecture provides many important benefits, including the following:

  • Stores mapping information in XML descriptors and not in the domain model objects.

  • By using the metadata, Toplink does not intrude in the object model or the database schema.

  • Allows us to design the object model as needed without forcing any specific design.

  • Allows DBAs to design the database as needed without forcing any specific design.

  • Does not rely on code-generation (which can cause serious design, implementation and maintenance issue).

  • Is unintrusive: Adapts to the object model and database schema rather than requiring you to design their object model or database schema to suite Toplink.

Creating Project Metadata
A Toplink project contains the mapping metadata that the Toplink runtime uses to map objects to a data source. The project is the primary object used by the Toplink runtime. This section describes the principal contents of project metadata including Descriptors and mappings and Data source login information.
1. Descriptors and Mappings: The Toplink maps persistent entities to the database in the application using the descriptors and mappings we build with Toplink Workbench. Toplink workbench supports several approaches to project development including the following:

  • Importing classes and tables for mapping

  • Importing classes and generating tables and mappings

  • Importing tables and generating classes and mappings

  • Creating both classes and table definitions

Toplink Workbench supports all these options. The most common solution is to develop the persistent entities using a development tool such as an IDE like Oracle JDeveloper or a modeling tool and to develop a relational model through appropriate relational design tools. You then use Toplink Workbench to construct mappings that relate these two models. Although Toplink workbench does offer the ability to generate persistent entities or the relational model components for an application, these utilities are intended only to assist in rapid initial development strategies not to complete a round trip application development.
2. Amending Descriptors: An amendment method lets you implement a Toplink feature that is not currently supported by Toplink workbench. We would need to write a Java method to amend the descriptor after it is loaded and specify the method in the Toplink workbench for inclusion in the project metadata.

Data Sourch Login Information
For non-CMP projects we need to configure a session login in the session metadata that specifies the information required to access the data source. For CMP projects, the project contains a deployment login that specifies the information required to access the data source.
There are three ways to create Toplink mapping artificate: Toplink Workbench, JDeveloper or Using Toplink Java APIs to create the mapping directly. The two more frequently used IDEs are Toplink Workbench and the Integrated Toplink component for JDeveloper:
Toplink Workbench
Toplink Workbench is a standalone development environment that allows users to import Java classes or domain objects created externally in form of a jar or compiled classes in a directory. The workbench also allows creating and testing a Database connection using JDBC drivers. The workbench provides an option to use either of the development, testing or production database credentials for logging in to the database. The users can then import a set of database tables for which the mapping is required. Any classes that are in the workbench classpath can be imported into the workbench project and mapped to the database tables imported earlier. Once imported, the Workbench can recreate the database tables in a defined schema on any other database or schema. This provides a flexibility of code and mapping mobility. Once the mapping is completed, the project can be exported to an XML document or a Jav aclass whose package and path can be defined in the project properties.
Oracle JDeveloper
Oracle JDeveloper is an Integrated Development Environment that provides an ability to create multiple types of projects. The IDE also has a Toplink component that can be used to map the Java classes in the jDev project. The flexibility that the JDeveloper provides is that the toplink mapping can be done on the Java source itself anddoes not require the classes to be compiled. It provides one development interface for a complete application development.

Supporting Information




No comments: