Annotating your Java classesTo use JCROM, you need to be able to annotate the class you want to map. All the JCROM annotations are on the field level, except for one that is on the class level. I'll introduce the most important ones, and then give a table of all the annotations. Let's assume that we have the following Java class: import java.util.Date;
public class WeblogEntry {
private String title;
private String excerpt;
private String body;
private Date publishDate;
// ... getters and setters
}We have four simple fields here. Let's look at the class after annotating it with JCROM annotations: import java.util.Date;
import org.jcrom.annotations.JcrName;
import org.jcrom.annotations.JcrPath;
import org.jcrom.annotations.JcrProperty;
public class WeblogEntry {
@JcrName private String name;
@JcrPath private String path;
@JcrProperty private String title;
@JcrProperty private String excerpt;
@JcrProperty private String body;
@JcrProperty private Date publishDate;
public void setTitle( String title ) {
this.title = title;
this.name = title;
}
// ... other getters and setters
}Two things to notice here. First of all, we've added a new field, "name", with the annotation @JcrName. The @JcrName annotation is mandatory: each class that is to be mapped using JCROM must have a String field that is annotated with @JcrName. This will be mapped to the node name, and will therefore represent the last token in the path to the node. The reason we've created another field, rather than use the title field, is that JCROM will clean the @JcrName field when mapping to a node name ("Hello, world!" would become "Hello_world"). We therefore store the original title in the title field, and the clean version in the name field (note that in the title setter we set the name as well). We've also added a "path" field, annotated with @JcrPath. This is also mandatory. The path field is a JCR read-only field that will contain the full path to the JCR node storing the object. Note that the name cleanup can be turned off by using the Jcrom( boolean cleanName ) constructor. The second thing to notice, is that we have annotated the other fields using @JcrProperty. JCROM will map all fields annotated with @JcrProperty to a property on the node. For a list of valid property types, see the table later in this chapter. @JcrProperty can be also be used for multi-value properties, but such fields need to be mapped as java.util.List, parameterized with a valid field type (more on this in the annotation reference below). In general, to annotate your object for JCROM, you do the following: - Annotate a String field with @JcrName. This will store the node name. Mandatory.
- Annotate a String field with @JcrPath. This will store the full node path. Mandatory.
- Annotate the class with @JcrNode if you need to specify a custom node type to map to.
- Annotate fields with @JcrProperty. Multi-valued fields should be represented as a java.util.List with a valid property parameter.
- Annotate child objects with @JcrChildNode. The child objects must be annotated with JCROM annotations. Multiple children are represented as a java.util.List, parameterized with the child object type.
- Annotate parent object references with @JcrParentNode.
- Annotate file objects with @JcrFileNode.
- Annotate files for storing read-only JCR information with @JcrUUID, @JcrCreated.
I'll now describe these steps in more detail. Note: - JCROM uses field level annotation, not method level. The fields can be private. Therefore there is no requirement for getter and setter methods.
- JCROM handles inheritance, so annotations from superclasses are automatically detected.
|
I want to note, that it is important to have a default constructor. I always forget do define one and the mapping error message does not indicate that.
regards