jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Jackrabbit: How to map associations between objects (page edited)
Date Thu, 04 Sep 2008 21:16:00 GMT
How to map associations between objects (JCR) edited by Christophe Lombart
      Page: http://cwiki.apache.org/confluence/display/JCR/How+to+map+associations+between+objects
   Changes: http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=96517&originalVersion=2&revisedVersion=3


h2. Overview

This tutorial explains how to map associations between objects (1:1 and 1:n). You can find
the tutorial code sample from [here|^Beans_and_collections.zip]. It is based on Maven and
ready to be used inside Eclipse. If you have some configuration issues, please review the
tutorial "[A simple OCM project with Maven & Eclipse]". 

h2. The Conctent Model 

We will extend the content model created in the previous tutorial [5' with Jackrabbit OCM].
Each PressRelease is made by an Author and it is possible to add some references (URL). 

So, we have to add 2 new associations in our model  : 
- An 1:1 association between a PressRelease and an Author.
- An 1:n association between PresseRelease and Url. 

Here is the main java class, the PressRelease : 

public class PressRelease
    @Field(path=true) private String path;
    @Field  private String title;
    @Field  private Date pubDate;
    @Field  private String content;
    @Bean private Author author;
    @Collection  List<Url> urls;
    //if you want a map instead of a list, use the following declaration
    @Collection Map<String,Url> map;
    [... Add here getters & setters ...]


Since the tutorial [5' with Jackrabbit OCM], we can understand the goal of the annotations
@Node and @Field. 
An association 1:1 can be specified with the annotation @Bean like 
@Bean private Author author;

It is possible to set extra settings with this annotation but it is out of the scope of this
tutorial.You can review the code of the Author class which is very simple. As you will see,
it is not mandatory to add annotation @Field(path=true) in the Author class because it is
an aggregation of a PressRelease.

An 1:n association can be specified with the annotation @Collection like 
@Collection  List<Url> urls;

For this kind of association, you can also use a Map instead of a Collection
@Collection Map<String,Url> map;

Right now, the support of Map is limited to the usage of String for the key because the map
key will be used as the Node name.

h2.How are those objects stored in the repository ?

For this tutorial each java class is mapped into the "nt:unstructured" node type. Making this
kind of mapping is quite flexible because it does not imply specific repository configuration.
There is no constraints in the JCR repository. All constrains are defined in the java code.

Note : It is possible to associate a specific node type to each java class but this imply
more repository configurations. 
       It is also possible to change the corresponding JCR node structure by using specific
Bean or Collection converters. 
       Later, we will add more tutorials on OCM converters. 

Following our example, the Author and Urls nodes will be created as subnodes of a press release.

Here is an example of the correspoding JCR structure : 

- PressRelease_1 
	* path : "/mypath/myrelease"
	* title : "..."
	* pubDate : 10/06/08
	* content :  "...."
	- Author
		* firstName : "..."
		* lastName : "..."
	- urls
		* url1 
			* url : "http://...."
			* caption : "..."
			* description : "..."
		* url2 
			* url : "http://...."
			* caption : "..."
			* description : "..."
	- map
	   * Apache
	       * url : "http://www.apache.org"
               * caption : "..."
               * description : "..."
           * Jackrabbit
              * url : "http://jackrabbit.apache.org"
              * caption : "..."
              * description : "..."

h2.Download the tutorial code

You can download the OCM project from [here|^Beans_and_collections.zip] 

This message is automatically generated by Confluence

Unsubscribe or edit your notifications preferences

If you think it was sent incorrectly contact one of the administrators

If you want more information on Confluence, or have a bug to report see

View raw message