Donnerstag, 20. März 2008

Transition from ORM to JCR modeling

I am currently thinking about using JCR as a Backend for Grails (or more generally Groovy). Graeme Rocher has made some great steps in creating a Grails plugin.

GORM is imho one of the best possibilities to define an ORM model, since it hides all technical aspects of the implementation and lets the developer describe the domain model in a clear an consistent way.

Therefore this clearness should still be around when using JCR as backend.

Take an Grails/GORM example:

class Author {
static hasMany = [ books : Book ]
String name
}
class Book {
static belongsTo = [author:Author]
String title
}

The dynamical nature of Grails adds the necessary properties, you could also write it as

class Author {
static hasMany = [ books : Book ]
String name
Set books
}
class Book {
static belongsTo = [author:Author]
Author author
String title
}
There are 2 aspects to this domain model
  1. object relations: an author has a set of books
  2. constraints: you have to make sure that a book really has an author (except when you define this property as nullable, which is also a contraint in this sense)
This simple domain model has got an interessting implication when mapping it to a repository:
  1. the one-to-many relation author vs. books would typically be mapped by a hierarchical manner, e.g.

    /authors
    /stephen king
    /books
    /Misery

    etc.
  2. the constraints, on the other hand would mean to add a structure to the repository by, e.g., defining node types. I think, that's what David means. Might be nice to have (I personally don't mind structure), but is not mandatory
Conclusion? I think what can be said is that thinking "data first" might lead to an ever more relaxed approach of defining domain models than Grails/GORM already has today ....

Keine Kommentare: