Spring数据neo4j没有在Grails中创建唯一的索引/约束

时间:2014-11-09 09:53:22

标签: grails neo4j spring-data-neo4j

我在使用grails中的spring-data-neo4j获取唯一约束时遇到了一些麻烦。

我怀疑这是因为我没有正确连接它,但是存储库正在被扫描和连接,并且CRUD正在工作,所以我不确定我做错了什么。

我正在使用grails 2.4.3,这些依赖项:

  neo4jVerison="2.1.5"
  dependencies {
    compile("org.neo4j:neo4j-community:$neo4jVerison")
    compile("org.neo4j.app:neo4j-server:$neo4jVerison")
    compile("org.neo4j:neo4j-rest-graphdb:2.0.1")
    compile("org.neo4j:neo4j-spatial:0.13-neo4j-2.1.4")
    compile 'org.springframework.data:spring-data-neo4j:3.2.1.RELEASE'
    test group:"org.neo4j", name:"neo4j-kernel", version: "$neo4jVerison", classifier:"tests"
    test "org.grails:grails-datastore-test-support:1.0-grails-2.4"
    test "xmlunit:xmlunit:1.5"
} 

和我测试环境的这些bean:

testGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory)
graphDb(GraphDatabaseService) { bean ->
    bean.factoryBean = "testGraphDatabaseFactory"
    bean.factoryMethod = "newImpermanentDatabase"
    bean.destroyMethod = "shutdown"  }

xmlns neo4j:"http://www.springframework.org/schema/data/neo4j"
neo4j.'repositories'( 'base-package': "repositories" )
neo4jTemplate(Neo4jTemplate, graphDb)
neo4jMappingContext(Neo4jMappingContext)
tx(NeoTransactions, graphDb)

tx 类只暴露了一个启动graphDb事务的'tx.tx {closure}'方法,即使关闭失败也会关闭它。

我的域名是:

package repositories

import org.springframework.data.neo4j.annotation.GraphId
import org.springframework.data.neo4j.annotation.Indexed
import org.springframework.data.neo4j.annotation.NodeEntity

@NodeEntity
class Junction {
    @GraphId Long id;
    @Indexed(unique = true) Long legacyId
}

并且存储库是:

package repositories

import org.springframework.data.neo4j.repository.GraphRepository

interface JunctionRepository extends GraphRepository<Junction> {}

鉴于所有这些,我希望这会失败:

package repositories

import org.springframework.dao.DataIntegrityViolationException
import org.springframework.data.neo4j.conversion.Result

class JunctionRepositoryIntegrationSpec extends RepositorySpecification {
    JunctionRepository junctionRepository
    def tx

    def "should not be able to create two junctions with the same legacyId"() {
        given: "some junctions with the same id"
        Junction j = new Junction(legacyId: 10)
        Junction j2 = new Junction(legacyId: 10)

        when: "both are saved"
        tx.tx {
            [j, j2].each { junctionRepository.save(it) }
        }

        then:
        1 == junctionRepository.count()

        when: "the second one is changed to have the same legacy id"
        def j3 = new Junction(legacyId: 11)
        tx.tx {
            junctionRepository.save(j3)
            j3.legacyId = 10
            junctionRepository.save(j3)
        }

        then: "an exception should be thrown"
        thrown DataIntegrityViolationException
    }
}

奇怪的是,虽然只保存了一个联结(即 1 == junctionRepository.count())但是当我尝试保存修改后的 j3 时,没有抛出异常。

此外,当我在Neo4J Web控制台中打开数据库并运行:schema 时,似乎没有创建任何索引/约束。

我猜测我的布线配置不正确,因为我认为在布线时解析域对象时应该设置索引。

有人知道遗漏了什么吗?

后续行动1

我怀疑我不应该手动创建模板或映射上下文。规范弹簧配置似乎是&lt; neo:config /&gt;

然而,当我尝试通过spring dsl执行此操作时出现了一个奇怪的错误:

 xmlns neo4j:"http://www.springframework.org/schema/data/neo4j"
 neo4j.'repositories'( 'base-package': "repositories" )
 neo4j.'config'( 'graphDatabaseService': "graphDb" )

错误是:

| Error Fatal error running tests: Error creating bean with name 'junctionRepository':
  Cannot resolve reference to bean 'neo4jTemplate' while setting bean property 'neo4jTemplate';
  nested exception is org.springframework.beans.factory.BeanCreationException: Error creating
   bean with name 'neo4jTemplate' defined in class org.springframework.data.neo4j.config.Neo4jConfiguration:
  Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.data.neo4j.support.Neo4jTemplate org.springframework.data.neo4j.config.Neo4jConfiguration.neo4jTemplate() throws java.lang.Exception] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'neo4jMappingContext' defined in class org.springframework.data.neo4j.config.Neo4jConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.data.neo4j.support.mapping.Neo4jMappingContext org.springframework.data.neo4j.config.Neo4jConfiguration.neo4jMappingContext() throws java.lang.Exception] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityIndexCreator' defined in class org.springframework.data.neo4j.config.Neo4jConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.data.neo4j.support.mapping.EntityIndexCreator org.springframework.data.neo4j.config.Neo4jConfiguration.entityIndexCreator() throws java.lang.Exception] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'indexProvider' defined in class org.springframework.data.neo4j.config.Neo4jConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.data.neo4j.support.index.IndexProvider org.springframework.data.neo4j.config.Neo4jConfiguration.indexProvider() throws java.lang.Exception] threw exception; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'graphDatabaseService' is defined (Use --stacktrace to see the full trace)

嗯:没有定义名为'graphDatabaseService'的bean ?那是一个错误吗? neo4j.'config'('graphDatabaseService':“graphDb”)告诉它使用 graphDb bean,不是吗?

后续行动2

如果我将bean名称更改为 graphDatabaseService ,它可以正常工作。看起来 neo4j.config 没有将命名的graphDb bean传递给它构造的所有那些bean。 :)

1 个答案:

答案 0 :(得分:1)

是的,它确实依赖于名称graphDatabaseService(不幸的是)。

另外,请确保您的tx课程还调用tx.success()而不只是tx.close()