1
votes

Recently I've been playing with Spring Data for Neo4j (version 2.1.0.BUILD-SNAPSHOT) and I'm having an issue related to its GraphRepository interface.

You can write a method that will search for a node of T class using the properties contained in the method's name, like this:

public interface UserRepository extends GraphRepository<User> {
    public User findByUsername(String username);
    public User findById(Long id);
}

My User class:

@NodeEntity
public class User {

    @GraphId
    Long id;

    @Indexed
    private String username;

    private String password;

    ...
}

And my applicationContext.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation=" 
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/data/neo4j
        http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">


    <context:spring-configured />
    <context:annotation-config />

    <context:component-scan base-package="package.for.services" />  
    <neo4j:repositories base-package="package.for.repositories" />

    <neo4j:config graphDatabaseService="graphDatabaseService" />
    <bean id="graphDatabaseService" class="org.neo4j.kernel.EmbeddedGraphDatabase"
        destroy-method="shutdown" scope="singleton">
        <constructor-arg value="/path/to/neo4j.db" />
        <constructor-arg>
            <map>
                <entry key="allow_store_upgrade" value="true" />
            </map>
        </constructor-arg>
    </bean>
</beans>

My issue is that findById(Long id) method does return a node with that specific id, but findByUsername(String username) does return null instead of a node with that specific username.

Any help will be appreciated.

1

1 Answers

0
votes

Solved by recreating the database. Deleted its directory and launched the application again.

I guess the indexes have not been created correctly since the beggining, and Spring Data is not able to change them once they were already created.

Thank you.