Apache Cayenne > Index > Cayenne Examples > Raw XML Examples

Cayenne has an excellent GUI tool but some developers prefer to edit Cayenne's XML mapping and configuration files directly. This page is a modest effort to demonstrate Cayenne's XML syntax.

Feel free to add or email your own examples to Chris Farnham.

Simple Example

Below is a simple example which defines a users table and maps a User object to it. Notice that
the obj-entity doesn't define an id attribute. ID fields are maintained by Cayenne and can be
ignored by the user.

    <data-map project-version="2.0">
        <db-entity name="users">
           <db-attribute name="id" type="INTEGER" isPrimaryKey="true" isMandatory="true" />
           <db-attribute name="name" type="VARCHAR" isMandatory="true" length="128"/>
           <db-attribute name="created" type="TIMESTAMP" isMandatory="true" />
        </db-entity>
        <obj-entity name="User" className="org.apache.cayenne.examples.User" dbEntityName="users">
          <obj-attribute name="name" type="java.lang.String" db-attribute-path="name"/>
          <obj-attribute name="created" type="java.util.Date" db-attribute-path="created" />
        </obj-entity>
       ...
    </data-map>

One-to-one Relationship

  ...
   <db-entity name="users">
     <db-attribute name="id" type="INTEGER" isPrimaryKey="true" isMandatory="true" />
     <db-attribute name="phone_number_id" type="INTEGER" isMandatory="true" />
     <db-attribute name="name" type="VARCHAR" isMandatory="true" length="128"/>
     <db-attribute name="created" type="TIMESTAMP" isMandatory="true" />
   </db-entity>

   <obj-entity name="User" className="org.apache.cayenne.examples.User" dbEntityName="users">
     <obj-attribute name="name" type="java.lang.String" db-attribute-path="name"/>
     <obj-attribute name="created" type="java.util.Date" db-attribute-path="created" />
   </obj-entity>

  <db-entity name="phone_numbers">
    <db-attribute name="id" type="INTEGER" isPrimaryKey="true" isMandatory="true" />
    <db-attribute name="phone_number" type="VARCHAR" isMandatory="true" length="128"/>
  </db-entity>

  <obj-entity name="PhoneNumber" className="org.apache.cayenne.examples.PhoneNumber" dbEntityName="phone_numbers">
    <obj-attribute name="phoneNumber" type="java.lang.String" db-attribute-path="name"/>
  </obj-entity>

  <db-relationship name="user_to_phone_number" source="users" target="phone_numbers" toMany="false">
    <db-attribute-pair source="phone_number_id" target="id"/>
  </db-relationship>

  <db-relationship name="phone_number_to_user" source="roles" target="users" toMany="false">
    <db-attribute-pair source="id" target="phone_number_id"/>
  </db-relationship>

  <obj-relationship name="phoneNumber" source="User"
                target="PhoneNumber" db-relationship-path="user_to_phone_number" />
 
  <obj-relationship name="user" source="PhoneNumber"
                target="User" db-relationship-path="phone_number_to_user" />
  
  ...

One-to-many Relationship

Many-to-one Relationship

Many-to-many Relationship

Flattened many-to-many relationship

Named Queries

Default Values

Default schema for newly created DbEntities and Procedures.

Default package for newly created ObjEntities.

Specify default superclass for the DataObject classes

Default optimistic locking policy for ObjEntities.

Specify remote persistence client classes.

ObjEntity Qualifier

Creating ObjEntity Hierarchy

Delete Rules

Configuration

Object Validation

Transactional Behavior

Caching Behavior