Re: [CONF] Apache Cayenne: XML Mapping File (page edited)

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Thu Dec 20 2007 - 03:54:47 EST

  • Next message: Aristedes Maniatis: "Re: svn commit: r605850 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne: ./ access/ access/jdbc/ access/types/ dba/ dba/openbase/ dba/oracle/ dba/sqlite/ dba/sqlserver/ exp/parser/ query/ reflect/ util/ wocompat/"

    Sorry, here is the correct link to the diff:

    http://cwiki.apache.org/confluence/pages/diffpages.action?pageId=69339&originalId=72978

    On Dec 20, 2007, at 10:53 AM, Andrus Adamchik wrote:

    > Wonder who this guy is and how is this addition relevant to the "XML
    > Mapping File" page?
    >
    > http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=9806&originalVersion=2&revisedVersion=3
    >
    > Andrus
    >
    >
    > On Dec 20, 2007, at 4:28 AM, confluenc..pache.org wrote:
    >
    >> Page Edited : CAY : XML Mapping File
    >> XML Mapping File has been edited by Terrence Pietrondi (Dec 19,
    >> 2007).
    >>
    >> (View changes)
    >>
    >> Content:
    >> Introduction
    >> The XML mapping file is a schema of sorts for mapping your
    >> Persistent objects to XML. It is fully compatible with the format
    >> used by WebObjects, making migration to or from Cayenne simple.
    >>
    >> This document aims to describe the format of the XML mapping file
    >> and how you may use it.
    >>
    >> FormatOverview
    >> All mapping documents are composed of three elements:
    >>
    >> • <model>
    >> • <entity>
    >> • <property>
    >> The elements are nested in that order as well. A property always
    >> belongs to an entity. An entity always belongs to a model. This
    >> implies that <model> is always the root tag of the mapping file.
    >>
    >> <entity> and <property> also have attribute values that affect the
    >> mapping. Please read their dedicated sections for more details.
    >>
    >> <model>
    >> <model> is always the root of the mapping document. It accepts no
    >> attributes and there may only be one per document.
    >>
    >> <entity>
    >> <entity> corresponds to a Persistent object in your Cayenne datamap.
    >>
    >> Attributes:
    >>
    >> Name
    >> Required
    >> Explanation
    >> name
    >>
    >> Fully qualified class name of the Persistent object to represent.
    >> xmlTag
    >>
    >> The XML tag that will be used to represent the Persistent object in
    >> its encoded form.
    >> <property>
    >> <property> corresponds to a JavaBeans property of a Persistent
    >> object in your Cayenne datamap.
    >>
    >> Name
    >> Required
    >> Explanation
    >> name
    >>
    >> The name of the property of the enclosing Persistent object to
    >> represent. This uses standard JavaBeans notation, including dotted
    >> values for arbitrarily nested properties.
    >> xmlTag
    >>
    >> The XML tag that will be used to represent the property in its
    >> encoded form.
    >> ExamplesMapping Simple Attributes
    >> This example shows how to get the value of a specific column in a
    >> CayenneDataObject as the result of a select on that object's table.
    >> For example, given a table named YOUR_TABLE that has column
    >> YOUR_FIELD, you want to select a specific row and get the value for
    >> YOUR_FIELD using the ObjEntity of the CayenneDataObject.
    >>
    >> // Create a data context
    >> DataContext context = ....;
    >> // Create a match expression
    >> Expression qual =
    >> ExpressionFactory.matchExp(YourTable.YOUR_FIELD_PROPERTY,match);
    >> // Create your select query
    >> SelectQuery select = new SelectQuery(YourTable.class,qual);
    >> // Limit the results to one
    >> select.setFetchLimit(1);
    >> // Form your data object from the query getting the first result
    >> CayenneDataObject next = (YourTable)
    >> context.performQuery(select).get(0);
    >> Now to get the value of YOUR_FIELD from the CayenneDataObject
    >> object, you need to create an ObjEntity (for later) from the
    >> CayenneDataObject, get the DataMap from the context, and create a
    >> DbEntity:
    >>
    >> // For later
    >> ObjEntity entity = next.getObjEntity();
    >> DataMap mapping =
    >> context.getEntityResolver().getDataMap("YourDomainMap");
    >> DbEntity table = mapping.getDbEntity("YOUR_TABLE");
    >> At compile time, we hard code the table names and columns, because
    >> we know them from the database, but we do not know for instance, or
    >> at least you might not want to guess what the ObjEntity property
    >> names are. And so we can look in the map for our column name that
    >> we know and find its property name to pull its value from the
    >> CayenneDataObject as a result of our select:
    >>
    >> // Get the DbEntity attributes as a collection
    >> Collection collect = entity.getAttributes();
    >> // And its iterator
    >> Iterator colItr = collect.iterator();
    >> // Initialize a return value
    >> String ruleValue = null;
    >> // Loop the attributes
    >> while(colItr.hasNext()){
    >> // Get the next ObjAttribute on the DbEntity
    >> ObjAttribute attr = (ObjAttribute) colItr.next();
    >> // Get the attribute database path, the column name
    >> String dbattr = attr.getDbAttributePath();
    >> // Check if the current database field name matches
    >> // what you are interested in
    >> if(dbattr.equals("YOUR_FIELD")){
    >> // If so, read the property in your CayenneDataObject
    >> // using the name of the current ObjAttribute on the DbEntity
    >> ruleValue = (String) next.readProperty(attr.getName());
    >> break;
    >> }
    >> }
    >> // Commit your work
    >> context.commitChanges();
    >> //Return the value of YOUR_FIELD on the CayenneDataObject
    >> return returnValue;
    >> Mapping RelationshipsMapping Collections
    >>
    >>
    >> Powered by Atlassian Confluence (Version: 2.2.9 Build:#527 Sep 07,
    >> 2006) - Bug/feature request
    >>
    >> Unsubscribe or edit your notifications preferences
    >
    >



    This archive was generated by hypermail 2.0.0 : Thu Dec 20 2007 - 03:55:22 EST