Question

There are several Spring Data projects like Neo4j that use the Spring Data Commons to build up a PersistentEntity/PeristentProperty (basically type info plus property geters and setters) and EntityConverter to roll from a native store to Java. This is what the SDN (Spring Data Neo4j) does plus it bundling BeanWrapper converters to make sure that certain property types are allowed for the Neo4j data structure.

Basically Java beans are stamped with a @NodeEntity annotation and the beans is decomposed on writes into nodes (think a bean with only simple properties) interlinked by relationship objects.

Wondering if I can do the same with Orika? Means identifying classes via an annotation and processing each property when complex recursively. For example:

 @NodeEntity
 class Software {
    String name;
    ....
    Organisation organisation;
    ....
 } 

 @NodeEntity
 class Organisation {
    String name;
 }

Should be rolled into 2 nodes each containing the property name and a relationship object (denotes Organisation as a member of Software).

Was it helpful?

Solution

Here is an example of an Orika ClassMapBuilder supporting custom annotations, I think you can adapt it to meet your needs.

Gist : AnnotationClassMapBuilder

For Node (or DBObject of MongoDB) you can use a custom property resolver, take a look at: http://orika-mapper.github.com/orika-docs/advanced-mappings.html (ElementPropertyResolver)

Edit

Orika build mappers by class-map which are actually, just a collection of property-pair, property can be any thing which has name, type and setter or/and getter.

You can automatically create for each attribute in your beans an equivalent in Neo4J side, and let Orika build the mapper.

For example you can create a Person(name)->PrintStream mapper, in which you create for each person's property (name) an equivalent that print data (System.out)

Example

final Builder name = new Property.Builder()
                          .name("name")
                          .type(String.class.getName())
                          .setter("append(\"My name : \").append(%s).append('\\n')");       
factory.classMap(Person.class, PrintStream.class).fieldMap("name", name, false).add().register();

factory.getMapperFacade().map(person, System.out); // This print to default output stream, My name : xxxx
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top