We are currently working on a Java project with neo4j.
We use Spring Data Neo4j and get most of the information out of the Good Relationships book.
This document states, that the standard entity type representation in the graph is IndexingNodeTypeRepresentationStrategy.
For our project we would prefer the one with subreference nodes.
How can we configure neo4j to use this strategy when working with repositories. We started with the HelloWorld example, so we currently have a repository interface as follows:
public interface IWebsiteGraphRepository extends GraphRepository<Website> {}
Additionally we have our node entity (I omit most of the irrelevant code):
@NodeEntity
public class Website {
...
}
Can anyone provide a small example of how to set the TypeRepresentationStrategy ?
Can this be done in the Spring configuration?
The config would be:
<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" />
Edit:
After another session I finally was able to get it to work! I started based on Michael Hungers answer, but was not able to create the Bean. I found where his example may be from: gitHub. This, however, still did not work for me. I began looking deeper into the source code of the TypeRepresentationStrategyFactory class. I found out, that Strategy is a private enumeration. This is the second constructor arg I tried to provide. In my project it did never detect it as of type Strategy.
First I was a little sceptic, because Strategy is private. I mean, I couldn't even instanciate a TypeRepresentationStrategyFactory in Code, because it would not find the type Strategy. I soon found out, that Spring supposedly can do things like that: Beans with private constructor.
In the end I had to adjust Michaels solution, as it was not identifying the constructor argument whatsoever. No matter what I did. Maybe it's my setup, I really don't know. But finally I came up with the solution to create a bean from the private enumeration and provide this as a reference to the constructor:
<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>
This should do: