What is the best way to save my POJOs into Jackrabbit JCR?
Asked Answered
C

5

10

In Jackrabbit I have experienced two ways to save my POJOs into repository nodes for storage in the Jackrabbit JCR:

  1. writing my own layer and
  2. using Apache Graffito

Writing my own code has proven time consuming and labor intensive (had to write and run a lot of ugly automated tests) though quite flexible.

Using Graffito has been a disappointment because it seems to be a "dead" project stuck in 2006

What are some better alternatives?

Clangor answered 16/12, 2008 at 6:0 Comment(0)
B
15

Another alternative is to completely skip an OCM framework and simply use javax.jcr.Node as a very flexible DAO itself. The fundamental reason why OCM frameworks exist is because with RDBMS you need a mapping from objects to the relational model. With JCR, which is already very object-oriented (node ~= object), this underlying reason is gone. What is left is that with DAOs you can restrict what your programmers can access in their code (incl. the help of autocompletion). But this approach does not really leverage the JCR concept, which means schema-free and flexible programming. Using the JCR API directly in your code is the best way to follow that concept.

Imagine you want to add a new property to an existing node/object later in the life of your application - with an OCM framework you have to modify it as well and make sure it still works properly. With direct access to nodes it is simply a single point of change. I know, this is a good way to get problems with typos in eg. property names; but this fear is not really backed by reality, since you will in most cases very quickly notice typos or non-matching names when you test your application. A good solution is to use string constants for the common node or property names, even as part of your APIs if you expose the JCR API across them. This still gives you the flexibility to quickly add new properties without having to adopt OCM layers.

For having some constraints on what is allowed or what is mandatory (ie. "semi-schema") you can use node types and mixins (since JCR 2.0 you can also change the node type for existing content): thus you can handle this completely on the repository level and don't have to care about typing and constraints inside your application code - apart from catching the exceptions ;-)

But, of course, this choice depends on your requirements and personal preferences.

Barahona answered 16/12, 2008 at 12:26 Comment(2)
Very interesting. I admit I have not really gotten away from the old "OCM style" of thinking. Good food for thought.Clangor
How come that OCM didn't make it to JR 1.6.0 ? It looks deprecated, hibernated....Bandoline
I
2

You might want to have a look at Jackrabbit OCM that is alive and kickin. Of course another way is to manually serialize/deserialize the POJOs. For that there are many different options. Question is whether you need fix schema to query the objects in JCR. If you just want to serialize into XML then XStream is a very painless way to do so. If you need a more fix schema there is also Betwixt from Apache Commons.

Inflight answered 16/12, 2008 at 8:57 Comment(2)
Thanks for the pointers to XStream, Betwixt and the Jackrabbit OCM.Clangor
Do you know what is the status of OCM ? Why it didn't make it to 1.6.0 version and JCR 2.0 specification ?Bandoline
M
1

There is also the JCROM project at http://code.google.com/p/jcrom/. That project went dormant for a couple of years, but there have been a few new releases as of summer 2013.

Mol answered 17/1, 2011 at 18:30 Comment(0)
G
1

It depends on your needs. When you directly use javax.jcr.node, it means your code is heavily coupled to the underlying mechanism. In medium and even some small sized projects, this is not a good idea. Obviously the question will be how to go from the Node to your own domain model. The problem is quite similar as with going from Jdbc ResultSet to your own domain model. Mind you, I mean from a technical point of view the problem is similar. From a functional point of view, there are huge differences between using JDBC and JCR.

Another deciding factor is whether you can impose a structure in your JCR content or not. Some application domains can (but still match better with JCR than JDBC), in other domains the content may be highly unstructured in nature. In such case OCM is clearly overkill. I'd still advice to write your own wrapper layer around javax.jcr.* classes.

Gidgetgie answered 2/4, 2012 at 11:7 Comment(0)
S
1

There's also https://github.com/ilikeorangutans/omf, a very flexible object to JCR mapper. Unfortunately it doesn't have write support yet. However we're successfully using this framework in a large CMS installation.

Sowder answered 3/4, 2013 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.