gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 15:26:28 26 4
gpt4 key购买 nike

我在使用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 类仅公开了一个'tx.tx {闭包}'方法,该方法启动了graphDb事务,即使闭包失败也将其关闭。

我的域类是:
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 :

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

但是,当我尝试通过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。 :)

最佳答案

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

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

关于grails - Spring数据neo4j没有在Grails中创建唯一的索引/约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26826997/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com