Page 1 of 25
Special Interest Activity
ITK 478 Fall 2006
Object Relational Mapping using Cayenne Modeler
Kedar Gawande
Table of Contents
1. Table of Contents………………………………………………………………………………………………………… 22. Task Plan Summary……………………………………………………………………………………………………… 3
3. Evaluation Report……………………………………………………………………………………………………….. 5
a. Introduction……………………………………………………………………………………………………. 5
b. What Cayenne does behind the Scenes……………………………………………………………. 5
c. Understanding Reflective of this Tool……………………………………………………………….5
d. Is Cayenne Right for You ?...... 5
e. Relational Schema to Java Object Mapping using CayenneModeler………………….6
f. Cayenne Mapping Architecture………………………………………………………………………..7
g. Cayenne Project Structure………………………………………………………………………………..8
h. Limitations of Report………………………………………………………………………………………..9
i. Conclusion……………………………………………………………………………………………………….10
4. In Class Demonstration………………………………………………………………………………………………..11
a. Introduction…………………………………………………………………………………………………….11
b. System Requirements………………………………………………………………………………………11
c. Obtaining Cayenne…………………………………………………………………………………………..11
d. Demonstration…………………………………………………………………………………………………11
i. Chief Purpose…………….………………………………………………………………………..11
ii. Tutorial Contents………………………………………………………………………………… 11
1. Setup of Modeler, Derby DB & Eclipse IDE………………………………. 12
2. Start Cayenne Mapping Project ……………………………………………….12
3. Create Relational Schema & its Object/ Relational Mapping…….16
4. Creating Derby Database & Equivalent Java Classes………………… 17
5. Using DataContext to access Classes………………………………………..18
6. Creating Data Objects using CayenneDataObjects……………………18
7. Creating Mapping SQL Query from Cayenne…………………………….19
8. Executing Mapped Query from Main()……………………………………..20
9. Executing Standalone Query from Main()…………………………………21
10. Converting & Deploying A Web Application………………………………21
5. Appendix……………………………………………………………………………………………………………………… .25
6. References…………………………………………………………………………………………………………………….25
ACS 478 Special Interest Activity Task Plan Summary
Fall 2006
Student: ______Kedar M Gawande______
Special Interest Activity Title: Object Relational Mapping using Cayenne Modeler
Activity Abstract: (summarize your activity’s purpose in 1 or 2 sentences)
Studying & demonstrating how Object Relational mapping of a Relational Schema into an Abstract Object Oriented Schema in Java is done using Cayenne Modeler.
Activity Task Plan
Task/step Description /Hours spent
Background research of the salient features of this tool and how it needs special attention from developers around the world. / 10Download, Installation & Initial Setup of Tool Cayenne Modeler, Derby Database & IBM Rational Application Developer / 5
Design & Setup of Database Schema in Derby to be used in the Demo / 2
Learning the features & how to implement them / 15
Learning concepts behind the functionalities provided & Understanding the actual implications of using certain features in the tool / 15
Formulating a demonstration sequence to highlight the most important features of using Cayenne, Choosing sample values & functions / 5
Executing the demonstration & ensuring the expected & the actual outcomes match. Further experimenting in the demo / 20
Writing the evaluation report / 15
Page 25 of 25
Object Relational Mapping using Cayenne Modeler : Evaluation Report
Apache Cayenne: Professional Object Relational Mapping
Introduction:
Apache Cayenne is a powerful, full-featured Java Object Relational Mapping framework. It is open source and completely free.
Cayenne provides management of persistent Java objects mapped to relational databases, single method call queries and updates (including atomic updates of all modified objects), seamless integration of multiple databases into a single virtual data source. Cayenne is distributed with CayenneModeler - a complete GUI mapping tool. Cayenne has been successfully deployed in production environment on high volume sites and has a constantly growing international community.
Note that Cayenne is not based on an Object-Relational framework but a Object framework with Relational schema mapping.
What Cayenne does behind the scenes in the mapping process?
For tons of applications that currently exist with an Object Oriented application environment working with Relational Database Schema, Cayenne provides automated features to map the Relational Schema into Objects without having the developers to create classes and objects from scratch. The intelligent logic behind the Cayenne framework maps the relational schema to a Java object environment and creates classes, objects, basic functions like getters, setters etc automatically during the mapping process.
Understanding Reflective of this Tool:
Cayenne is written in Java and is intended for Java developers working with relational databases.
Cayenne allows to reduce the development time in any database project by creating an object-oriented abstraction of the database schema. Development teams using Cayenne will be able to concentrate on the core business requirements and the data model instead of the SQL details.
Following section deal with specifics of the results of this report.
Is Cayenne Right for You ?
How does Object Relational Modeling approach fit into the needs of the projects in your organization? This approach should be chosen based on the project requirements as Cayenne might not be suitable to all kinds of projects.
A few scenarios are shown below that describe the most common cases that a development team may encounter on a typical O/R project. Let’s assume an imaginary company called "Some Company" that sells services to consumers as its main business, but also sells consumer products, and works with business customers. There are a number of project teams working on a set of Object/Relational projects.
Project 1: Java Interface to an Existing Database
"Some Company" has a billing system that runs on Sybase database, that uses a set of UNIX C++ applications to perform nightly batch jobs and print the bills. The project goal is to create a Java client-server Object/Relational system that will be used by customer service representatives to provide customer support and correct billing errors. Such system must use the existing database schema without changing it, but both Swing frontend and Java business layer that performs the database access have to be created from scratch.
Project 2: New System
"Some Company" is planning a brand new e-commerce site for its web users. Both the database and Java business objects must be designed from scratch as a part of the same project.
Project 3: Converting an Existing Java Business Framework
Before getting into O/R technology, "Some Company" created a set of Java applications for its sales and marketing teams working with business customers. These applications use a custom persistence framework based on Data Access Objects (DAO - a term for objects that encapsulate the handcoded SQL for their persistence operations). The goal of the project is to convert DAO to Cayenne-based Object/Relational business framework, preserving all business rules, and using the existing database.
Relational Schema to Java Object Mapping using CayenneModeler
Cayenne stores mapping and deployment information using a set of XML files with a predefined format. These files can be always edited by hand, but Cayenne goes beyond that. A standard distribution contains CayenneModeler - a Java Swing GUI application that serves as an Object/Relational modeling tool.
It is much more than just a specialized XML editor. It provides a comprehensive set of O/R functions, including:
· Powerful Open Source technology with Database independence
o DB Adapters are available for most of the databases commercially available. A list can be found at : http://cwiki.apache.org/CAYDOC/guide-to-12-features.html
o Auto Adapter Detection: Users no longer need to specify DbAdapter in the Modeler. All standard adapters are "guessed" by Cayenne in runtime using database metadata. In simple terms, Cayenne automatically recognizes the underlying database being used, if it’s one of the standard ones.
· Remote Object Persistence
o Easily integrates remote object persistence which deals with storing object data and status into a persistent form. Functions done with persistent data objects are also configurable in Cayenne with very little effort.
· Reverse-engineering RDBMS schema into Cayenne DataMaps.
o DataMaps hold the relational schema. For a predefined and available relational schema, one can reverse engineer the schema into a Java utilizable object schema.
· Generation of the database schema from the mapping information stored in DataMaps.
o Cayenne mapping takes a relational schema involving entities as an input. Once the relational schema with all the relationships is fed into the Cayenne Modeler, Cayenne then can generate a Java Object schema based on the relational schema. This is done by storing the mapping information in the Cayenne DataMaps.
· Management of the project deployment structure and database connection information.
o Cayenne maintains all the deployment structure of the application and the database connection information in a set of XML files which are editable in text as well as other popular XML editors. This XML is also updated every time there is modification done through the Cayenne Modeler GUI.
· Separating the Modeler GUI generated schema from the user editable schema to avoid unnecessary errors induced due to a programmer’s negligence towards uniformity. This is illustrated in the demonstration that follows this report.
· SQL Querying
o Generating SQL statements in the raw format to be consumed by the Java functions to be called using a single call mechanisms. This is illustrated in the demonstration accompanying this report.
Cayenne Mapping Architecture
The Cayenne framework is based on mapping between the following 2 layers.
The RDBMS Mapping layers uses a traditional relational schema with entities and relationships
The Object Mapping layer with ObjectEntities and the relationships converted into ObjectRelationships which are fully duplex ( Aà B as well as the inverse B à A )
The transformation of DbEntity into ObjEntity has been traditionally done by developers from scratch by writing classes and creating attributes. Cayenne automates this process by creating equivalent classes in Java and provides automatically generated functions and methods that work on the relationships for navigating from one object to another based on the relationships
Figure 1 : Mapping of Relational Schema ( DbEntities ) into Java Classes & Objects ( ObjEntities) []
Cayenne Project Structure:
A typical Cayenne project is a set of XML configuration files needed for a Java application to work with Cayenne. It is editable by CayenneModeler.
An application itself is editable in environments like IBM Eclipse/ RAD that work on standalone Java applications and web applications.
A project has roughly the following structure:
Figure 2: Cayenne Project Structure
This structure is saved in the following set of files:
§ cayenne.xml file: This is a main project file in each application. It should always be called cayenne.xml. It configures the layout of the data sources used by the application, breaking it by domains and nodes. It also links to other parts of Cayenne project described below. Nodes represent physical datasources. For each database JDBC URL there should be a single node configured. Domains represent virtual datasources that are a combination of one or more physical datasources. In a simplest (and most common) case, there will be only one domain (virtual datasource) that has a single node (physical datasource). But Cayenne is flexible to allow grouping multiple nodes under domains thus hiding lots of underlying details and allowing applications to work with multiple databases as if it was a single one. XML structure of cayenne.xml is described in this DTD file.
§ DataMap file: Used to instantiate DataMap objects that describe a database schema and map it to the Java classes in the Java application. XML structure of the DataMap file is described in this DTD file.
§ DataSource file: In a standalone Java application, Java code has no access to DataSource objects that are normally provided by deployment containers. Cayenne framework still needs a DataSource to connect to the database. Therefore Cayenne has a set of wrapper classes located in org.apache.cayenne.conn package, to create DataSource objects based on regular JDBC drivers. DataSource file provides needed information about database URL, JDBC driver, login, password and connection pooling parameters of a single DataSource. Note: such configuration file can be present in development so that CayenneModeler tool could obtain a database connection, even if an application wouldn't use it in deployment and instead would use a DataSource provided by the container.
Limitations of Report:
The scope of the activity did not allow a lot of in depth analysis and study of all the features and/or limitations of the tool.
A bunch of additional features core to Cayenne’s distribution and it’s portfolio are :
· "Lazy" relationships and incremental fetching of data.
· Object queries, including in-memory sorting and filtering of objects with Cayenne expression language.
· Isolation of object graph changes between user sessions.
· Committing all created, modified or deleted objects with a single method call.
· Distributed cache.
· Automatic ordering of DML operations to satisfy database integrity constraints.
· Combining multiple physical data sources into a single virtual data source.
· Database independence with adapters for all major databases.
· Multiple strategies for automated primary key generation.
A few more features that deserve mentioning here, without any particular order:
1. Distributed Caching. 2. Batching and Operation Sorting. 3. Inheritance and Advanced Mapping. 4. Optimistic Locking. 5. Lazy Collections and "hollow" objects.
A deeper look at these features has been physically impossible due to the time constraints of this activity. Needless, all the above features beyond the scope of this report are what continues to distinguish Cayenne from any other Object Relational Mapping tool. An extensive study of these features would have certainly strengthened my position in this report.
On the flip side, the bug/ open issues count of this open source project has been 701. But then, which software does not have bugs ? But the magic of Cayenne is that it is an open source project and there are hundreds, may be even thousands of programmers constantly working on Cayenne to make it a better mapping tool. Cayenne is a true open source project drawing its strength from the developer and user community and will continue to grow in the future with its increasing set of features coming in with every subsequent release.
Conclusion:
There are tons of applications in hundreds of organization created with the traditional relational schema. For organizations that cannot afford to completely shift on to object relational databases like Oracle 10g, a better solution is to port their application over to an object oriented environment with the existing relational schemas. For such applications, application development can be accelerated with Cayenne.
Cayenne is a fast to learn, easy to adapt, highly scalable mapping environment. The Cayenne "trademark" is its ease of use, quick learning, and developer productivity. Cayenne automates a number of tasks that take up a huge amount of developer’s time unnecessarily. This provides a fast paced learning and productive environment for the developers to work with.