Announcement Announcement Module
Collapse
No announcement yet.
spring-data-neo4j and spring-data-mongodb mixed up Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring-data-neo4j and spring-data-mongodb mixed up

    Hi All,

    First of all, spring-data-neo4j 2.0.0.RELEASE has some broken dependencies, believe it is in spring-data-commons-core. Would really love to see a fix very soon.

    A related question I have is that if I need to use mongodb and neo4j together in my application. What versions shall I use so that all the dependencies on common libs would not cause issues.

    Below is the main problem I am having right now. Thanks all for looking into this:

    I am using Neo4J and MongoDb together. The versions are:

    spring-data-neo4j 2.0.0.RC1
    spring-data-mongodb 1.0.0.RC1

    The spring-data-commons-core version is 1.2.0.M02 . That's the only such jar in my war.

    I spent much of the time trying to figure out the right combination of these versions. This at least to get me going with mongodb.

    I configure both db in my spring configuration file as:

    Code:
       <neo4j:config storeDirectory="db/neo4j" />
    
      <mongo:db-factory dbname="mongodb"/>
    
      <bean id="mongoTemplate"
            class="org.springframework.data.mongodb.core.MongoTemplate">
         <constructor-arg ref="mongoDbFactory"/>
      </bean>
    This is my PersonRepository:

    Code:
    public interface PersonRepository extends GraphRepository<Person>
    The entity is rather simple. It is sitting in the domain module.

    Code:
    @NodeEntity
    public class Person
    {
      @GraphId
      private Long id;
    
      @Indexed
      private String firstName;
      private String lastName;
    
      private String email;
    
      private boolean active;
    }
    There are other entities but all annotated only for Neo4J.

    I used to the following to configure rest of Neo4J

    Code:
      <context:spring-configured/>
      <context:annotation-config/>
      <context:component-scan base-package="com.xxxxx.domain" />
    
      <neo4j:repositories base-package="com.xxxxx.domain.repository" />
    Calling the save method on PersonRepository generates an error. Same
    configuration, I do not have an issue invoking MongoDb operations
    using spring-data-mongodb objects and methods.

    Here is an exception when calling the save method on my
    PersonRespository (extends GraphRepository<Person>):

    Code:
    java.lang.ClassCastException:
    org.springframework.data.neo4j.support.mapping.Neo4jPersistentEntityImpl
    cannot be cast to
    org.springframework.data.mongodb.core.mapping.MongoPersistentEntity
    	at
    org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:
    79)
    	at
    org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:
    47)
    	at
    org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:
    97)
    	at
    org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:
    303)
    	at
    org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:
    256)
    	at
    org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:
    165)
    	at
    org.springframework.data.neo4j.mapping.RelationshipInfo.<init>(RelationshipInfo.java:
    61)
    	at
    org.springframework.data.neo4j.mapping.RelationshipInfo.fromField(RelationshipInfo.java:
    82)
    	at
    org.springframework.data.neo4j.support.mapping.Neo4jPersistentPropertyImpl.extractRelationshipInfo(Neo4JPersistentPropertyImpl.java:
    92)
    	at
    org.springframework.data.neo4j.support.mapping.Neo4jPersistentPropertyImpl.<init>(Neo4JPersistentPropertyImpl.java:
    57)
    	at
    org.springframework.data.neo4j.support.mapping.Neo4jMappingContext.createPersistentProperty(Neo4jMappingContext.java:
    54)
    	at
    org.springframework.data.neo4j.support.mapping.Neo4jMappingContext.createPersistentProperty(Neo4jMappingContext.java:
    38)
    	at org.springframework.data.mapping.context.AbstractMappingContext
    $PersistentPropertyCreator.doWith(AbstractMappingContext.java:377)
    	at
    org.springframework.util.ReflectionUtils.doWithFields(ReflectionUtils.java:
    513)
    	at
    org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:
    246)
    	at
    org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:
    165)
    	at
    org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:
    140)
    	at
    org.springframework.data.neo4j.support.Neo4jTemplate.getPersistentEntity(Neo4jTemplate.java:
    558)
    	at
    org.springframework.data.neo4j.support.Neo4jTemplate.getMappingPolicy(Neo4jTemplate.java:
    457)
    	at
    org.springframework.data.neo4j.support.Neo4jTemplate.getMappingPolicy(Neo4jTemplate.java:
    616)
    	at
    org.springframework.data.neo4j.support.Neo4jTemplate.save(Neo4jTemplate.java:
    292)
    	at
    org.springframework.data.neo4j.repository.NodeGraphRepositoryImpl.save(NodeGraphRepositoryImpl.java:
    42)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
    39)
    	at
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
    25)
    	at
    org.springframework.data.repository.core.support.RepositoryFactorySupport
    $QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:
    322)
    	at
    org.springframework.data.repository.core.support.RepositoryFactorySupport
    $QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:
    307)
    	at
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:
    172)
    	at
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:
    110)
    	at
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:
    172)
    	at
    org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:
    155)
    	at
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:
    172)
    	at
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:
    202)
    	at $Proxy32.save(Unknown Source)
    Last edited by MichaelHunger; Feb 21st, 2012, 04:04 AM. Reason: [CODE] formatting

  • #2
    SDN 2.0.1.RELEASE will be there next week with fixed dependencies.

    Could you share your project?

    Thanks

    Michael

    Comment


    • #3
      I have the same problem (mixing spring data for mongodb and neo4j generates a ClassCastException):

      Code:
      java.lang.ClassCastException: org.springframework.data.neo4j.support.mapping.Neo4jPersistentEntityImpl cannot be cast to org.springframework.data.mongodb.core.mapping.MongoPersistentEntity
      	at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:79)
      	at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:47)
      	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:97)
      	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:324)
      	at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:263)
      	at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:165)
      	at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:140)
      	at org.springframework.data.neo4j.support.Neo4jTemplate.getPersistentEntity(Neo4jTemplate.java:573)
      	at org.springframework.data.neo4j.support.Neo4jTemplate.getMappingPolicy(Neo4jTemplate.java:470)
      	at org.springframework.data.neo4j.support.Neo4jTemplate.getMappingPolicy(Neo4jTemplate.java:631)
      	at org.springframework.data.neo4j.support.Neo4jTemplate.save(Neo4jTemplate.java:295)
      	at org.springframework.data.neo4j.repository.AbstractGraphRepository.save(AbstractGraphRepository.java:106)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      	at java.lang.reflect.Method.invoke(Method.java:597)
      	at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:323)
      	at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:308)
      	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
      	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
      	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
      	at $Proxy27.save(Unknown Source)
      I guess the problem is quite easy to reproduce (I have only very simple classes that I use). However, this is the configuration snippet:
      Code:
        <!-- MongoDB support -->
        <!-- Need to tune all these parameters. -->
        <mongo:mongo replica-set="${mongo.replicaSet}">
          <mongo:options connections-per-host="${mongo.connectionsPerHost}"
            threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
            connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}"
            auto-connect-retry="${mongo.autoConnectRetry}" socket-keep-alive="${mongo.socketKeepAlive}"
            socket-timeout="${mongo.socketTimeout}" slave-ok="${mongo.slaveOk}"
            write-number="1" write-timeout="0" write-fsync="true" />
        </mongo:mongo>
      
        <mongo:db-factory dbname="${mongo.dbname}" mongo-ref="mongo"
          username="${mongo.username}" password="${mongo.passowrd}" />
      
        <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
          <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
        </bean>
      
        <mongo:repositories base-package="com.acme.scrt.network.mongo.repos" />
      
        <!-- Neo4j support. -->
      
        <bean id="graphDatabaseService"
          class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
          <constructor-arg index="0" value="${neo4j.uri}" />
        </bean>
      
        <neo4j:config graphDatabaseService="graphDatabaseService" />
      
      <!--   <bean id="neo4jTemplate" class="org.springframework.data.neo4j.support.Neo4jTemplate"> -->
      <!--     <constructor-arg index="0" ref="graphDatabaseService" /> -->
      <!--   </bean> -->
      
        <neo4j:repositories base-package="com.acme.scrt.network.neo4j.repos" />
      However, if I try to use a new neo4j template:
      Code:
        <bean id="neo4jTemplate" class="org.springframework.data.neo4j.support.Neo4jTemplate">
          <constructor-arg index="0" ref="graphDatabaseService" />
        </bean>
      ... I bump into a different problem:
      Code:
      java.lang.NullPointerException
      	at org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.getTemplate(SourceStateTransmitter.java:82)
      	at org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.copyPropertiesTo(SourceStateTransmitter.java:116)
      	at org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.write(Neo4jEntityConverterImpl.java:149)
      	at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister$CachedConverter.write(Neo4jEntityPersister.java:176)
      	at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:238)
      	at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:227)
      	at org.springframework.data.neo4j.support.Neo4jTemplate.save(Neo4jTemplate.java:295)
      	at org.springframework.data.neo4j.repository.AbstractGraphRepository.save(AbstractGraphRepository.java:106)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      	at java.lang.reflect.Method.invoke(Method.java:597)
      	at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:323)
      	at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:308)
      	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
      	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
      	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
      	at $Proxy27.save(Unknown Source)
      Did someone else meet this problem? Where can I see how the default neo4jTemplate is initialized?

      Thanks!
      McV

      Comment


      • #4
        Could you please update to the most recent versions?

        * Spring Data Neo4j 2.0.1.RELEASE
        * Spring Data Mongodb 1.0.1.RELEASE

        The default Neo4jTemplate is configured in Neo4jConfiguration.

        Could you otherwise raise a JIRA issue for Spring-Data MongoDB if the issue persists, that it has to additionally filter for the correct Persistent-Entity-Type in this method.

        Code:
        public class MongoPersistentEntityIndexCreator implements
        		ApplicationListener<MappingContextEvent<MongoPersistentEntity<MongoPersistentProperty>, MongoPersistentProperty>> {
        ....
        	public void onApplicationEvent(
        			MappingContextEvent<MongoPersistentEntity<MongoPersistentProperty>, MongoPersistentProperty> event) {
        		checkForIndexes(event.getPersistentEntity());
        	}
        Thanks

        Michael

        Comment


        • #5
          can i do it as it?

          Originally posted by MichaelHunger View Post
          Could you please update to the most recent versions?

          * Spring Data Neo4j 2.0.1.RELEASE
          * Spring Data Mongodb 1.0.1.RELEASE

          The default Neo4jTemplate is configured in Neo4jConfiguration.

          Could you otherwise raise a JIRA issue for Spring-Data MongoDB if the issue persists, that it has to additionally filter for the correct Persistent-Entity-Type in this method.

          Code:
          public class MongoPersistentEntityIndexCreator implements
          		ApplicationListener<MappingContextEvent<MongoPersistentEntity<MongoPersistentProperty>, MongoPersistentProperty>> {
          ....
          	public void onApplicationEvent(
          			MappingContextEvent<MongoPersistentEntity<MongoPersistentProperty>, MongoPersistentProperty> event) {
          		checkForIndexes(event.getPersistentEntity());
          	}
          Thanks

          Michael
          Code:
          	public void onApplicationEvent(
          			MappingContextEvent<MongoPersistentEntity<MongoPersistentProperty>, MongoPersistentProperty> event) {
          		if(event.getPersistentEntity() instanceof MongoPersistentEntity) {
          			checkForIndexes(event.getPersistentEntity());
          		}
          	}

          Comment


          • #6
            There is now a public demo project on github that makes sure that the correct versions of the Spring Data projects are working together. Please check it out: https://github.com/SpringSource/spri...ultistore-test

            Comment

            Working...
            X