【问题标题】:Configure TypeRepresentationStrategy for Neo4j为 Neo4j 配置 TypeRepresentationStrategy
【发布时间】: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


    【解决方案1】:

    应该这样做:

    <bean id="typeRepresentationStrategyFactory" class="org.springframework.data.neo4j.support.typerepresentation.TypeRepresentationStrategyFactory">
        <constructor-arg index="0" ref="graphDatabaseService"/>
        <constructor-arg index="1" value="SubRef"/>
    </bean>
    

    【讨论】:

    • 谢谢。不幸的是,它对我不起作用。 bean 无法创建并出现错误“无法解析匹配的构造函数”。我试过没有构造函数 arg Subref,它可以工作。我什至尝试将 type 或 name 属性添加到 constructor-arg 标记。它没有改变任何东西。我找到了构造函数:public TypeRepresentationStrategyFactory(GraphDatabase graphDatabaseService,Strategy strategy),我真的不明白为什么这不起作用。是因为 Strategy 类型对于 TypeRepresentationStrategyFactory 是私有的吗?
    • @Rob 不幸的是,我没有多少时间进行测试。接下来几天我会有一些空闲时间再试一次。我真的很想将其标记为“答案”;)但我还不能这样做。
    • 好吧,如果你能把你拥有的代码扔到 GitHub 上,我很乐意提供帮助。
    • 有趣,b/c 我将代码示例从工作测试的配置中取出。可以分享一下你的配置吗? (可能在 Neo4j Google 小组或 Spring 论坛上)
    • 我在春季论坛上创建了这个帖子:forum.springsource.org/…
    【解决方案2】:

    你可以这样做:

    <bean id="typeRepresentationStrategyFactory" class="org.springframework.data.neo4j.support.typerepresentation.TypeRepresentationStrategyFactory">
        <constructor-arg index="0" ref="graphDatabaseService"/>
        <constructor-arg index="1">
            <value type="org.springframework.data.neo4j.support.typerepresentation.TypeRepresentationStrategyFactory.Strategy">SubRef</value>
        </constructor-arg>
    </bean>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-15
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-09
      • 2016-01-25
      相关资源
      最近更新 更多