How to map associations between objects
This tutorial explains how to map associations between objects (1:1 and 1:n). You can find the tutorial code sample from here. 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".
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 :
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
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
For this kind of association, you can also use a Map instead of a Collection
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.
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 :
You can download the OCM project from here