title: Spring-data cross store entity management
body: <p>I want to use Postgres, MongoDB and Neo4j together in my application. I was able to configure them all, however now each of my POJOs is backed my graphNode as well as document by aspectJ.</p>
  
  <p>Is there any way to filter out which POJOs are backed by graphNodes only and which by document only?</p>
  
  <p>I have a problem with saving POJOs when I do it more than twice [sic!] in a single request and I can see in the log that mongo and neo4j are both trying to create lots of instances which cause some king of deadlock.</p>
  
  <p>So long story short:</p>
  
  <ol>
  <li>Is there a way to filter data mappings to configure pojo "A" to be mapped by RDBMS and graph (no document) and pojo B by document and graph (no RDBMS)</li>
  <li>Is there any sample for cross-store spring-data based application which more or less covers my problem?</li>
  <li>Why can I save two instances of pojo class in my controller one by one but when third instance is created I can notice a deadlock?</li>
  </ol>
  
  <p>[EDIT]</p>
  
  <p>What I've noticed:</p>
  
  <ol>
  <li>Mongo aspectj builder backs @Entity annotated POJOs an I don't know how to use @Entity to map POJO for Hibernate and not in MongoDB</li>
  <li>Neo4j related freeze occurs only when connected via REST and happens sometimes on 3rd sometimes on 4th and sometimes doesnt happen at all. See the controller initiation how it is done.I've tried  all commented lines with no success.</li>
  <li>Transaction manager configuration must be placed in correct place, otherwise Neo4J configuration validator service fails.</li>
  </ol>
  
  <p>[/EDIT]</p>
  
  <p>I use:</p>
  
  <ol>
  <li>Spring 3.0.5</li>
  <li>SpringRoo 1.4</li>
  <li>Spring-data 1.0</li>
  <li>Postgres 9.0 + Hibernate 3.5.5</li>
  <li>Neo4j 1.3 remotely </li>
  <li>MongoDB 1.8.2 remotely</li>
  <li>All DBs are on single remote machine and works fine</li>
  </ol>
  
  <p>[EDIT]</p>
  
  <p>POM slices:</p>
  
  <pre><code>&lt;properties&gt;
      &lt;roo.version&gt;1.1.4.RELEASE&lt;/roo.version&gt;
      &lt;spring.version&gt;3.0.5.RELEASE&lt;/spring.version&gt;
      &lt;aspectj.version&gt;1.6.11&lt;/aspectj.version&gt;
      &lt;slf4j.version&gt;1.6.1&lt;/slf4j.version&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;spring-security.version&gt;3.0.5.RELEASE&lt;/spring-security.version&gt;
      &lt;jackson.version&gt;1.8.0&lt;/jackson.version&gt;
      &lt;spring.data.mongodb.version&gt;1.0.0.M2&lt;/spring.data.mongodb.version&gt;
      &lt;spring.data.graph.version&gt;1.0.0.RELEASE&lt;/spring.data.graph.version&gt;
      &lt;spring.data.commons.version&gt;1.0.0.RELEASE&lt;/spring.data.commons.version&gt;
  &lt;/properties&gt;
      ...
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
          &lt;artifactId&gt;spring-data-neo4j&lt;/artifactId&gt;
          &lt;version&gt;${spring.data.graph.version}&lt;/version&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
          &lt;artifactId&gt;spring-data-mongodb-cross-store&lt;/artifactId&gt;
          &lt;version&gt;${spring.data.mongodb.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
          &lt;artifactId&gt;spring-data-mongodb&lt;/artifactId&gt;
          &lt;version&gt;${spring.data.mongodb.version}&lt;/version&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
          &lt;artifactId&gt;spring-data-neo4j-rest&lt;/artifactId&gt;
          &lt;version&gt;${spring.data.graph.version}&lt;/version&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
          &lt;artifactId&gt;spring-data-commons-core&lt;/artifactId&gt;
          &lt;version&gt;${spring.data.commons.version}&lt;/version&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
      &lt;/dependency&gt;
  
    ....
    &lt;plugin&gt;
              &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
              &lt;artifactId&gt;aspectj-maven-plugin&lt;/artifactId&gt;
              &lt;version&gt;1.0&lt;/version&gt;
              &lt;dependencies&gt;
                  &lt;!-- NB: You must use Maven 2.0.9 or above or these are ignored (see 
                      MNG-2972) --&gt;
                  &lt;dependency&gt;
                      &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
                      &lt;artifactId&gt;aspectjrt&lt;/artifactId&gt;
                      &lt;version&gt;${aspectj.version}&lt;/version&gt;
                  &lt;/dependency&gt;
                  &lt;dependency&gt;
                      &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
                      &lt;artifactId&gt;aspectjtools&lt;/artifactId&gt;
                      &lt;version&gt;${aspectj.version}&lt;/version&gt;
                  &lt;/dependency&gt;
              &lt;/dependencies&gt;
              &lt;executions&gt;
                  &lt;execution&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;compile&lt;/goal&gt;
                          &lt;goal&gt;test-compile&lt;/goal&gt;
                      &lt;/goals&gt;
                  &lt;/execution&gt;
              &lt;/executions&gt;
              &lt;configuration&gt;
                  &lt;outxml&gt;true&lt;/outxml&gt;
                  &lt;aspectLibraries&gt;
                      &lt;aspectLibrary&gt;
                          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                          &lt;artifactId&gt;spring-aspects&lt;/artifactId&gt;
                      &lt;/aspectLibrary&gt;
                                              &lt;aspectLibrary&gt;
                          &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
                          &lt;artifactId&gt;spring-data-neo4j&lt;/artifactId&gt;
                      &lt;/aspectLibrary&gt;
                      &lt;aspectLibrary&gt;
                          &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
                          &lt;artifactId&gt;spring-data-mongodb-cross-store&lt;/artifactId&gt;
                      &lt;/aspectLibrary&gt;
  
                  &lt;/aspectLibraries&gt;
                  &lt;source&gt;1.6&lt;/source&gt;
                  &lt;target&gt;1.6&lt;/target&gt;
              &lt;/configuration&gt;
          &lt;/plugin&gt;
  </code></pre>
  
  <p>POJO:</p>
  
  <pre><code>@NodeEntity
  @RooToString
  @RooJavaBean
  public class DElement {
  
  @Indexed
  private Long id;
  
  @RelatedTo(direction=Direction.BOTH, elementClass=DRegion.class, type="SUBELEMENT_OF")
  private Set&lt;DElement&gt; childElements = new HashSet&lt;DElement&gt;();
  
  @Indexed(indexName = "delement-name", fulltext=true)
  private String name;    
  
  
  
  @Transactional
  public void addChild(DElementchild child)
  {
      this.childElements.add(child);
  }
  
  }
  </code></pre>
  
  <p>Controller (with loading):</p>
  
  <pre><code>    @Controller
      @RequestMapping(value="/DElements")
      public class DElementsController {
  
          DElementRepository DElementRepository;
  
          GraphDatabaseContext gdbc;
  
          @Autowired
          public DElementsController(DElementRepository DElementRepository, GraphDatabaseContext gdbc)
          {
              this.DElementRepository = DElementRepository;
              this.gdbc = gdbc;
              this.initElements();
          }
  
          @Transactional
          private void initElements()
          {
              try
              {
                  DElementRepository.deleteAll();
  
              } 
              catch (Exception e) {} finally{}
  
              //Transaction txn = gdbc.beginTx();
  
              referenceNode.createRelationshipTo(allElements.getPersistentState(), myRelation);
  
              DElement naElements = new DElement().persist();
              naElements.setName("1");
              allElements.addChild(naElements);
  
              DElement saElements = new DElement().persist();
              saElements.setName("2");
              allElements.addChild(saElements);               
  
              DElement euElements = new DElement().persist();
              euElements.setName("3");
              allElements.addChild(euElements);
  
              DElement afElements = new DElement().persist();
              afElements.setName("4");    
              allElements.addChild(afElements);
  
              DElement asElements = new DElement().persist();
              asElements.setName("5");    
              allElements.addChild(asElements);
  
              DElement auElements = new DElement().persist();
              auElements.setName("6");
              allElements.addChild(auElements);
  
  
              //txn.success();
              //txn.finish();
  
          }
      }
  </code></pre>
  
  <p>applicationContext.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:jee="http://www.springframework.org/schema/jee"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:mongo="http://www.springframework.org/schema/data/mongo"
      xmlns:graph="http://www.springframework.org/schema/data/graph"
      xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
          http://www.springframework.org/schema/data/graph http://www.springframework.org/schema/data/graph/datagraph-1.0.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
      &lt;context:property-placeholder location="classpath*:META-INF/spring/*.properties" /&gt;
      &lt;context:spring-configured /&gt;
      &lt;bean id="validator"
          class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" /&gt;
  
      &lt;context:component-scan base-package="com.foobar"&gt;
          &lt;context:exclude-filter expression=".*_Roo_.*"
              type="regex" /&gt;
          &lt;context:exclude-filter expression="org.springframework.stereotype.Controller"
              type="annotation" /&gt;
      &lt;/context:component-scan&gt;
      &lt;jee:jndi-lookup id="dataSource" jndi-name="jdbc/foobar" /&gt;
  
  
  
  
      &lt;mongo:mongo host="${foobar.mongodb.addr}" port="27017" /&gt;
      &lt;mongo:mapping-converter base-package="com.foobar.lib.model.mongo"/&gt;
  
      &lt;bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate"&gt;
          &lt;constructor-arg name="mongo" ref="mongo" /&gt;
          &lt;constructor-arg name="databaseName" value="foobar" /&gt;
          &lt;constructor-arg name="defaultCollectionName" value="basecoll" /&gt;
      &lt;/bean&gt;
  
      &lt;bean class="org.springframework.data.document.mongodb.MongoExceptionTranslator" /&gt;
  
      &lt;!-- Mongo cross-store aspect config --&gt;
      &lt;bean
          class="org.springframework.data.persistence.document.mongo.MongoDocumentBacking"
          factory-method="aspectOf"&gt;
          &lt;property name="changeSetPersister" ref="mongoChangeSetPersister" /&gt;
      &lt;/bean&gt;
      &lt;bean id="mongoChangeSetPersister"
          class="org.springframework.data.persistence.document.mongo.MongoChangeSetPersister"&gt;
          &lt;property name="mongoTemplate" ref="mongoTemplate" /&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;/bean&gt;
  
  
      &lt;bean 
          id="graphDatabaseService" class="org.neo4j.kernel.EmbeddedGraphDatabase"
          destroy-method="shutdown" &gt;
          &lt;constructor-arg index="0" value="c:/neo4j/data/foobar" /&gt;
      &lt;/bean&gt;
  &lt;!--  REST DOESNT WORK FOR THE MOMENT
      &lt;bean id="graphDatabaseService" class="org.springframework.data.graph.neo4j.rest.support.RestGraphDatabase"&gt;
          &lt;constructor-arg value="${foobar.neo4j.reststore}"/&gt;        
      &lt;/bean&gt; --&gt;
  
  
  &lt;!--    &lt;bean id="graphDatabaseContext" class="org.springframework.data.graph.neo4j.support.GraphDatabaseContext"&gt;
          &lt;property name="graphDatabaseService" ref="graphDatabaseService"/&gt;
      &lt;/bean&gt; --&gt;
  
      &lt;graph:repositories base-package="com.foobar.data.repositories.neo4j" graph-database-context-ref="graphDatabaseContext"/&gt;
  
  
      &lt;graph:config graphDatabaseService="graphDatabaseService" entityManagerFactory="entityManagerFactory" /&gt;
      &lt;tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" /&gt;    
  
      &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;     
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt; 
  &lt;/beans&gt;
  </code></pre>
  
  <p>[/EDIT]</p>