2

I have created a Maven project using spring-data-neo4j. I have also installed the standalone Neo4j Server Community Edition 2.3.3. I am trying to save some Vertex objects to the database and then simply retrieve them to check everything works fine. Then, I would like to be able to open the created db in the standalone server for better visualization. I am using as dependencies:

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-neo4j</artifactId>
            <version>4.0.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency> 

The configuration class is:

@Configuration
@ComponentScan("neo4j.example")
@EnableAutoConfiguration
@EnableNeo4jRepositories("neo4j.example.repository")

public class App extends Neo4jConfiguration {

    public App() {
        System.setProperty("username", "neo4j");
        System.setProperty("password", "root");
    }

    @Override
    public SessionFactory getSessionFactory() {
        return new SessionFactory("neo4j.example.model");
    }

    @Override
    @Bean
    @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public Session getSession() throws Exception {
        return super.getSession();
    }

    @Override
    public Neo4jServer neo4jServer() {
        return new RemoteServer("http://localhost:7474");
    }

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }


    }

My NodeEntity looks like:

@NodeEntity
public class Vertex  {

    private String name;
    @GraphId
    private Long id;

    @Relationship(type = "PAIRS_WITH", direction = "UNDIRECTED")
    public  Set<Vertex> teammates;

    public Vertex() {   }

// getters, setters, equals, toString
    }

The repository:

@Repository
public interface VertexRepository extends GraphRepository<Vertex> {

    Vertex findByName(String name);

    List<Vertex> findByTeammatesName(String name);
}

The service:

@Service
public class VertexServiceImpl implements VertexService {

    @Autowired
    private VertexRepository vertexRepository;

    @Override
    @Transactional
    public Vertex create(Vertex vertex) {
        return vertexRepository.save(vertex);
    }

    @Override
    @Transactional
    public Iterable<Vertex> findAll() {
        return vertexRepository.findAll();
    }
//....
}

Then I have a controller with two simple methods to save a vertex and then query the database.

@RestController
@RequestMapping("/api/")
public class GraphController {

    @Autowired
    VertexService vertexService;

    @RequestMapping(value = "addvertex", method = RequestMethod.GET)
    public void add() {
        Vertex v = new Vertex();
        v.setId(1l);
        v.setName("name");
        Vertex v2 = new Vertex();
        v2.setId(2l);

        v.worksWith(v2);
        vertexService.create(v);
    }

    @RequestMapping(value = "all", method = RequestMethod.GET)
    public Iterable<Vertex> getAll() {
        return vertexService.findAll();
    }

}

When I save the vertex to the db there is no error. When I call /all the db is empty. I checked messages.log and there is no exception...last lines being:

2016-03-26 14:25:15.716+0000 INFO  [o.n.k.i.DiagnosticsManager] Interface Microsoft Wi-Fi Direct Virtual Adapter-WFP 802.3 MAC Layer LightWeight Filter-0000:
2016-03-26 14:25:15.716+0000 INFO  [o.n.k.i.DiagnosticsManager] --- INITIALIZED diagnostics END ---
2016-03-26 14:25:15.747+0000 INFO  [o.n.k.i.DiagnosticsManager] --- STOPPING diagnostics START ---
2016-03-26 14:25:15.747+0000 INFO  [o.n.k.i.DiagnosticsManager] --- STOPPING diagnostics END ---
2016-03-26 14:25:15.747+0000 INFO  [o.n.k.i.f.GraphDatabaseFacade] Shutdown started

Any help is fully appreciated!

Roxana Roman
  • 1,012
  • 1
  • 8
  • 17

2 Answers2

3

I managed to solve my problem. The configuration is fine, the problem was I was trying to set the id property of a @NodeEntity object. Even if I remove the @GraphId property the vertex is not saved. This post addresses the same problem.
In Good Relations:The Spring Data Neo4j Guide Book it is mentioned that: "If the field is simply named 'id' then it is not necessary to annotate it with @GraphId as the OGM will use it automatically."

It would be nice if there was some kind of warning/ error message or more explicitly mentioned in the documentation that you cannot setId() and that the node will not be saved in the db if you do. Hope this will save somebody some time!

Community
  • 1
  • 1
Roxana Roman
  • 1,012
  • 1
  • 8
  • 17
2

You are mixing embedded and remote server?

You should look for your data in the remote server.

Also you must have disabled auth for this to work in the server, or you have to provide username (neo4j) and password to your config.

DO NOT START AN EMBEDDED DATABASE ON THE SAME DIRECTORY AS THE SERVER USES

Michael Hunger
  • 41,339
  • 3
  • 57
  • 80
  • The embedded database is not using the same location as the server. The password for the server is root and the user is neo4j as I specified in the config class App. I commented out the embedded db part. But I have the same behavior. No data is written to the db. Do I have to specify somehow in the code the name and location of the directory the server uses? Or is it enough to point it to localhost:7474 and have the server started when I try to write to it, as I have it now? – Roxana Roman Mar 26 '16 at 18:18