首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring数据neo4j没有在Grails中创建唯一的索引/约束

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

Stack Overflow用户
提问于 2014-11-09 09:53:22
回答 1查看 404关注 0票数 0

在grails中使用SpringData-ne4j时,我很难获得独特的约束。

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

我使用的是grails2.4.3,其中包含以下依赖项:

代码语言:javascript
复制
  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用于我的测试环境:

代码语言:javascript
复制
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{闭包}”方法,即使闭包失败,也会关闭它。

我的域类是:

代码语言:javascript
复制
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
}

储存库是:

代码语言:javascript
复制
package repositories

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

interface JunctionRepository extends GraphRepository<Junction> {}

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

代码语言:javascript
复制
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

我怀疑我不应该手工创建模板或映射上下文。规范的弹簧配置似乎是.。

但是,当我尝试通过spring执行此操作时,会出现一个奇怪的错误:

代码语言:javascript
复制
 xmlns neo4j:"http://www.springframework.org/schema/data/neo4j"
 neo4j.'repositories'( 'base-package': "repositories" )
 neo4j.'config'( 'graphDatabaseService': "graphDb" )

错误是:

代码语言:javascript
复制
| 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被定义为?这是一个bug吗?新4j.‘config’(‘graphDatabaseService:"graphDb“)告诉它使用graphDb bean,不是吗?

追踪2

如果我将bean名更改为graphDatabaseService,它就能正常工作。看起来,neo4j.config并没有将命名的graphDb bean传递给它构建的所有bean。:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-09 19:55:49

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

还要确保您的tx类也调用tx.success(),而不仅仅是tx.close()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26826997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档