You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by co...@apache.org on 2008/09/04 21:50:02 UTC

[CONF] Apache Jackrabbit: 5' with Jackrabbit OCM (page edited)

5' with Jackrabbit OCM (JCR) edited by Christophe Lombart
      Page: http://cwiki.apache.org/confluence/display/JCR/5%27+with+Jackrabbit+OCM
   Changes: http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=75934&originalVersion=2&revisedVersion=3

Comment:
---------------------------------------------------------------------

Simplify this tutorial. The ocm:discriminator is not necessary for this basic tutorial

Change summary:
---------------------------------------------------------------------

Simplify this tutorial. The ocm:discriminator is not necessary for this basic tutorial

Change summary:
---------------------------------------------------------------------

Simplify this tutorial. The ocm:discriminator is not necessary for this basic tutorial

Change summary:
---------------------------------------------------------------------

Simplify this tutorial. The ocm:discriminator is not necessary for this basic tutorial

Change summary:
---------------------------------------------------------------------

Simplify this tutorial. The ocm:discriminator is not necessary for this basic tutorial

Content:
---------------------------------------------------------------------

This very small tutorial describes how to create an application with Jackrabbit OCM.
In short, you have to :
* Create one or more persistent classes. 
* Instantiate an [Object Content Manager] component.
* Use the [Object Content Manager] to persist your data.


h2. Create a persistent class

This tutorial is using the annotation support to define a persistent class. Your data objects are simple pojos with some OCM annotations. 
Here is a example of a PressRelease class. 

{code}
package org.apache.jackrabbit.ocm.model;

import java.util.Date;

import org.apache.jackrabbit.ocm.mapper.impl.annotation.Field;
import org.apache.jackrabbit.ocm.mapper.impl.annotation.Node;

@Node
public class PressRelease 
{
	@Field(path=true) String path;
	@Field String title; 
	@Field Date pubDate; 
	@Field String content;
	
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public Date getPubDate() {
		return pubDate;
	}
	public void setPubDate(Date pubDate) {
		this.pubDate = pubDate;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	
	
}
{code}

The annotation {code}@Node{code} has to be added on the top of the class.
Each persistent class must have a *path field* which will be mapped into the JCR Node path. This can be specify with the annotation {code}@Field(path=true){code}.
Other persistent fields can be defined with the annotation {code}@Field{code}

That's all for the class definition. In other tutorials, we will see how to map advanced fields like collections or custom objects. 




h2. Instantiate an Object Content Manager component
In order to save a PressRelease object, you have to instantiate an [Object Content Manager] component : 

{code}
List<Class> classes = new ArrayList<Class>();	
classes.add(PressRelease.class); // Call this method for each persistent class
		
Mapper mapper = new AnnotationMapperImpl(classes);
ObjectContentManager ocm =  new ObjectContentManagerImpl(session, mapper);	
{code}

h2. Use the Object Content Manager to persist your data

Now, you are ready to create a new PressRelease and use the [Object Content Manager] to persist it into the JCR repository. 

{code}
// Insert an object
System.out.println("Insert a press release in the repository");
PressRelease pressRelease = new PressRelease();
pressRelease.setPath("/newtutorial");
pressRelease.setTitle("This is the first tutorial on OCM");
pressRelease.setPubDate(new Date());
pressRelease.setContent("Many Jackrabbit users ask to the dev team to make a tutorial on OCM");
			
ocm.insert(pressRelease);
ocm.save();
			
// Retrieve 
System.out.println("Retrieve a press release from the repository");
pressRelease = (PressRelease) ocm.getObject("/newtutorial");
System.out.println("PressRelease title : " + pressRelease.getTitle());
			
// Delete
System.out.println("Remove a press release from the repository");
ocm.remove(pressRelease);
ocm.save();
{code}

---------------------------------------------------------------------
CONFLUENCE INFORMATION
This message is automatically generated by Confluence

Unsubscribe or edit your notifications preferences
   http://cwiki.apache.org/confluence/users/viewnotifications.action

If you think it was sent incorrectly contact one of the administrators
   http://cwiki.apache.org/confluence/administrators.action

If you want more information on Confluence, or have a bug to report see
   http://www.atlassian.com/software/confluence