【发布时间】:2012-06-04 16:49:54
【问题描述】:
我们目前正在使用 neo4j 开发一个 Java 项目。
我们使用Spring Data Neo4j 并从Good Relationships 书中获取大部分信息。
本文档指出,图中的标准实体类型表示为IndexingNodeTypeRepresentationStrategy。
对于我们的项目,我们更喜欢带有子引用节点的那个。
我们如何配置 neo4j 以在使用存储库时使用此策略。我们从 HelloWorld 示例开始,因此我们目前有一个存储库接口,如下所示:
public interface IWebsiteGraphRepository extends GraphRepository<Website> {}
此外,我们还有我们的节点实体(我省略了大部分不相关的代码):
@NodeEntity
public class Website {
...
}
谁能提供一个小例子来说明如何设置 TypeRepresentationStrategy ?
这可以在 Spring 配置中完成吗?
配置将是:
<context:annotation-config />
<tx:annotation-driven mode="aspectj" transaction-manager="neo4jTransactionManager" />
<context:component-scan base-package="my.base">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<neo4j:config storeDirectory="target/neo4j-db" />
<neo4j:repositories base-package="my.base.packages.repositories" />
编辑:
在又一次会议之后,我终于能够让它工作了!我开始基于 Michael Hungers 的回答,但无法创建 Bean。我发现他的例子可能来自:gitHub。但是,这仍然对我不起作用。我开始深入研究 TypeRepresentationStrategyFactory 类的源代码。我发现, Strategy 是一个私有枚举。这是我试图提供的第二个构造函数 arg。在我的项目中,它从未将其检测为 Strategy 类型。
首先我有点怀疑,因为战略是私人的。我的意思是,我什至无法在代码中实例化 TypeRepresentationStrategyFactory,因为它找不到类型 Strategy。我很快发现,据说 Spring 可以做这样的事情:Beans with private constructor。
最后我不得不调整 Michaels 的解决方案,因为它没有识别构造函数参数。不管我做了什么。也许这是我的设置,我真的不知道。但最后我想出了创建bean from the private enumeration 并将其作为构造函数的引用的解决方案:
<bean id="subref" factory-method="valueOf"
class="org.springframework.data.neo4j.support.typerepresentation.TypeRepresentationStrategyFactory.Strategy">
<constructor-arg>
<value>SubRef</value>
</constructor-arg>
</bean>
<bean id="typeRepresentationStrategyFactory"
class="org.springframework.data.neo4j.support.typerepresentation.TypeRepresentationStrategyFactory">
<constructor-arg index="0" ref="delegatingGraphDatabase" />
<constructor-arg index="1" ref="subref" />
</bean>
【问题讨论】:
标签: neo4j graph-layout spring-data-neo4j