O-R Mapping in OC4J

Date: 22-Jul-2002

Introduction

When using Container Managed Persistence (CMP) with Entity beans, the EJB container automatically maps the fields of the bean to the corresponding database tables. Oracle9iAS Containers for J2EE (OC4J) supports several types of mappings to database tables. This document contains the following topics:

Overview of O-R Mapping

Oracle9iAS Containers for J2EE (OC4J) supports the following types of mappings to the database tables.

  • Simple objects
  • Serializable user defined objects
  • Collection objects
  • Entity reference objects

These different types of mappings enable CMP bean developers to map complicated object models to relational databases very easily.

O-R Mapping Techniques

All these mapping techniques are accomplished through orion-ejb-jar.xml. This file is created and packaged with the deployment jar file. All the object to relational mappings are specified in this file. DTD for this file can be found at orion-ejb-jar.dtd.

For O-R mapping, the tag <entity-deployment> should be present for every entity bean.  This tag will contain the tag <cmp-field-mapping> for every field that is mapped to a database column. The tag <cmp-field-mapping> must atleast contain a name attribute, which reperesents the name of the field that is being persisted. The sample orion-ejb-jar.xml can be found here.

<entity-deployment> tag contains an attribute called table, which allows us to mention the table to which the mapping is being done. Otherwise, the container creates a new table with name that is value of the name attribute.

Simple Objects and Primitive Data Type Mapping

Simple objects like String, Integer and primitive data types like int, float can be mapped using this technique. These fields can be mapped directly to a column of the database table. This can be achieved by setting the <persistence-name> attribute with a value in the <cmp-field-mapping> tag. Optionally, a persistence-type attribute can be specified to tell OC4J the type of database field to use when auto-creating tables. If no persistence-type is specified, the default type from the database-schema specified in the data-source will be used instead. For a sample code snippet, please click here.

User-defined Serializable Object Mapping

An Object contains its own fields or properties. To map these objects, the <cmp-field-mapping> tag should contain a body specifying how the fields will be mapped. The <cmp-field-mapping> tag consists of some sub tags which allows us to perform the mapping. For mapping an object & its fields, we will be  using the <fields> or <properties> tag within the <cmp-field-mapping> tag. For sample code snippet please click here.

Entity Reference Mapping

 Entity beans can contain references to other enity beans. Mapping of this entity reference can be done by including the <entity-ref> tag in the <cmp-field-mapping> tag. The home attribute specifying the JNDI location of the referenced bean will be provided as value to the <entity-ref> tag. For sample code snippet please click here.

Collection Object Mapping

 OC4J supports 4 types of collection object mapping. They are

  • list-mapping
  • set-mapping
  • collection-mapping
  • map-mapping

Thse mappings can be implemented by adding the following tags within the <cmp-field-mapping> tag respectively. They are <list-mapping>,  <set-mapping>, <collection-mapping> and <map-mapping>.

These tags contains two important elements called <primkey-mapping> and <value-mapping>. If the collection is a map or a hashtable, then it will also contain an element <map-key-mapping> for mapping the primary key.

The <value-mapping> and <map-key-mapping> must contain an attribute type which is a fully qualified java class name of the object that is being mapped in the collection.

The <primkey-mapping>,<value-mapping> and <map-key-mapping> will contain a <cmp-field-mapping> which specifies how the actual values are mapped. For sample code snippet please click here.

O-R mapping sample

Please download CMP demo application from here.

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy