Announcement Announcement Module
Collapse
No announcement yet.
Spring Data Neo4j findAll does not return results in some conditions Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Data Neo4j findAll does not return results in some conditions

    Hello, I am developing an example of Spring Data Neo4j and findAll method of Repository class sometimes returns all inserted objects but other times returns no result without any modification. It seems like there was two embedded instances defined although I can only see one. I am using Spring Data Neo4j 2.1.0.Released. Let me show the code:

    Model class is as easy as:

    Code:
    package com.lordofthejars.nosqlunit.springdata.neo4j;
    
    import java.util.Set;
    
    import org.neo4j.graphdb.Direction;
    import org.springframework.data.neo4j.annotation.Fetch;
    import org.springframework.data.neo4j.annotation.GraphId;
    import org.springframework.data.neo4j.annotation.NodeEntity;
    import org.springframework.data.neo4j.annotation.RelatedTo;
    import org.springframework.data.neo4j.annotation.RelatedToVia;
    
    @NodeEntity
    public class Member {
    
    	private static final String COMMANDS = "COMMANDS";
    	
    	@GraphId Long nodeId;
    	
    	private String name;
    	
    	private Starship assignedStarship;
    	
    	public Member() {
    		super();
    	}
    	
    	public Member(String name) {
    		this.name = name;
    	}
    	
    	@Fetch @RelatedTo(type=COMMANDS, direction=Direction.BOTH)
    	private Set<Member> commands;
    	
    	public void command(Member member) {
    		this.commands.add(member);
    	}
    	
    	public Set<Member> commands() {
    		return this.commands;
    	}
    	
    	public Starship getAssignedStarship() {
    		return assignedStarship;
    	}
    	
    	public String getName() {
    		return name;
    	}
    	
    	public void assignedIn(Starship starship) {
    		this.assignedStarship = starship;
    	}
    }
    Member repository is:

    Code:
    package com.lordofthejars.nosqlunit.springdata.repository;
    
    import org.springframework.data.neo4j.repository.GraphRepository;
    import org.springframework.data.neo4j.repository.RelationshipOperationsRepository;
    
    import com.lordofthejars.nosqlunit.springdata.neo4j.Member;
    
    public interface MemberRepository extends GraphRepository<Member>,
    		RelationshipOperationsRepository<Member> {
    
    }
    Application Context is composed by two files:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
               http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.1.xsd
               http://www.springframework.org/schema/data/neo4j
               http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd">
                   
         <context:component-scan base-package="com.lordofthejars.nosqlunit.springdata.neo4j"/>
         <context:annotation-config/>
         
         <neo4j:repositories base-package="com.lordofthejars.nosqlunit.springdata.repository"/>
         
    </beans>
    And:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
               http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.1.xsd
               http://www.springframework.org/schema/data/neo4j
               http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd">
    
    
    	<import resource="classpath:com/lordofthejars/nosqlunit/springdata/neo4j/application-context.xml"/>
    	<neo4j:config storeDirectory="target/config-test"/>
    
    </beans>
    And finally the test method:

    Code:
    public void jean_luc() throws XMLStreamException {
    		
    		String[] names = applicationContext.getBeanNamesForType(GraphDatabaseService.class);
    		
    		System.out.println(Arrays.toString(names));
    		
    		printXml();
    		
    		EndResult<Member> allMembers = memberRepository.findAll();
    		
    		Iterator<Member> iterator = allMembers.iterator();
    		System.out.println("+");
    		while(iterator.hasNext()) {
    			Member member = iterator.next();
    			
    			System.out.println(member.getName());
    			
    			Starship assignedStarship = starshipRepository.findOne(member.getAssignedStarship().nodeId);
    			System.out.println(assignedStarship.getStarship());
    		}
    		
    	}
    Note that printXml method use @Autowired GraphDatabseService to find all nodes and print them through console in GraphML format, and this method always prints correctly the inserted data:

    Code:
    <?xml version="1.0" ?>
    <graphml xmlns="http://graphml.graphdrawing.org/xmlns"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
    	<key id="name" for="node" attr.name="name" attr.type="string"></key>
    	<key id="__type__" for="node" attr.name="__type__" attr.type="string"></key>
    	<key id="starship" for="node" attr.name="starship" attr.type="string"></key>
    	<graph id="G" edgedefault="directed">
    		<node id="0"></node>
    		<node id="1">
    			<data key="__type__">com.lordofthejars.nosqlunit.springdata.neo4j.Member
    			</data>
    			<data key="name">Jean-Luc Picard</data>
    		</node>
    		<node id="2">
    			<data key="__type__">com.lordofthejars.nosqlunit.springdata.neo4j.Starship
    			</data>
    			<data key="starship">NCC-1701-E</data>
    		</node>
    		<edge id="0" source="1" target="2" label="assignedStarship"></edge>
    	</graph>
    </graphml>
    But then executing findAll method, sometimes returns a Member object, and sometimes returns nothing. I have noticed that when no results are returned edge id is 0 and default node id is also 0 (like previous example) but when edge id is different from any other nodes id then it works perfectly. Nodes and Edges identifiers are given by Neo4j so it seems that both of them are valid (or not). If it is valid that one node and one edge can contain the same id (it should be ok) then this could be a problem in Spring Data Neo4j, on the other hand if it is not valid we should research why Neo4j sometimes returns a repeated id.

    That's all, thank you very much for your help in advance.

  • #2
    I'm running into the similar issue findAll will work if i'm running findAll from JUnit test. but it will return empty result when I run the same method from a controller.

    Comment

    Working...
    X