Simple Object Mapping Strategies

Introduction

In order to explain the basic mapping strategies, we will use the following simple object model :

  • A page contains a path (of course), a title and a collection of paragraph.
  • Each paragraph contains a path and a text field.

This object model could be too simple for real applications and it is uses here just to simplify the description of the different mapping strategies.

Based on that object model, we can define the following java classes :

public class Page
{
	String path;
	String title;
	Collection paragraphs;
		
	/*  Add here the getter and setter methods */
			
	public void addParagraph(Paragraph paragraph)
	{
		if (paragraphs == null)
		{
			paragraphs = new ArrayList();
		}
						
		paragraphs.add(paragraph);
	}
}	

public class Paragraph
{
	private String path;
	private String text;
	
	/* Add here the getter and setter methods */

}
				

... and the class-descriptors can be :

 				
<class-descriptor className="org.apache.portals.graffito.jcr.testmodel.Page" jcrNodeType="graffito:page">
	<field-descriptor fieldName="path" path="true" />
	<field-descriptor fieldName="title" jcrName="graffito:title" />
	<collection-descriptor fieldName="paragraphs" proxy="false" 
	  elementClassName="org.apache.portals.graffito.jcr.testmodel.Paragraph" 
	  collectionConverter="org.apache.portals.graffito.jcr.persistence.collectionconverter.impl.NTCollectionConverterImpl" />
</class-descriptor>
				
<class-descriptor className="org.apache.portals.graffito.jcr.testmodel.Paragraph" jcrNodeType="graffito:paragraph">
	<field-descriptor fieldName="path" path="true" />
	<field-descriptor fieldName="text" jcrName="graffito:text"/>
</class-descriptor>
				

If the page is stored on the path :"/mysite/mypage1" and contains 2 paragraph, here is the resulting jcr structure :

 
/mysite/page1
	graffito:title = "This is the title of my page 1"
/mysite/page1/paragraph1
	graffito:text = "This is the content of para1"
/mysite/page1/paragraph2
	graffito:text = ""This is the content of para2"
					

Now, lets look in more details this class descriptor.

field-descriptor

The field-descriptor map a bean attribute into one JCR property. it can map any kind of atomic types (java primitive data type and their wrapper classes)

Based on our current example, the following field-descriptor is used to map the bean field "title" into the JCR property "graffito:title".

 				
<field-descriptor fieldName="title" jcrName="graffito:title" />	
				

bean-descriptor

collection-descriptor