Hot questions for Using Neo4j in spring mvc

Top Java Programmings / Neo4j / spring mvc

Question:

Background I am trying to figure out the correct way to add a UUID to neo4j if I am using Spring Data.

I have seen: https://dzone.com/articles/assigning-uuids-neo4j-nodes and here a TransactionEventHandler is used to insert a UUID when necessary. But the person who made this tutorial was not using spring data.

I have also seen this person's code: https://github.com/spring-projects/spring-data-neo4j/blob/master/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/web/domain/User.java They seem to be using java's java.util.UUID and then just converting it to a string and using it as a string entity and indexing it and going from there. This seems to be the simplest way.

But, in the docs: https://docs.spring.io/spring-data/neo4j/docs/current/reference/html/ They seem to using UUID's as their example for a usecase of an AddUuidPreSaveEventListener

Questions

Which method should I use for adding UUID's?

Could I just add

...
import java.util.UUID;

import org.neo4j.ogm.annotation.Index;
import org.neo4j.ogm.annotation.typeconversion.Convert;
import org.neo4j.ogm.typeconversion.UuidStringConverter;

...

@Convert(UuidStringConverter.class)
@Index(unique = true, primary = true)
private UUID uuid = UUID.randomUUID();

...

to my GraphType.java file and call it good?

Note: I am very new to all these technologies and could be still too inexperienced to even properly ask a question on this stuff.

Note 2: I have seen the graphaware UUID library before, it does seem to be fairly up to date, but I assumed that there may be a preferred way to make UUIDs if I was working with spring data.


Answer:

You can use the GraphAware Neo4j UUID library.

The docs says:

GraphAware UUID is a simple library that transparently assigns a UUID to newly created nodes and relationships in the graph and makes sure nobody can (accidentally or intentionally) change or delete them.

Simply download GraphAware Neo4j Framework and GraphAware Neo4j UUID .jar files to /plugins directory, modify few lines in neo4j.conf file and restart Neo4j. After it, UUIDs will be assigned to each node / relationships created in the Neo4j graph.

This approach does not depends on Spring or Spring Data.

Question:

I am using spring boot application mvc with graphDb (Neo4j) as my database. And I have problem when I have to do internationalization for my app. I have this code on my application.java

public class Application extends Neo4jConfiguration {

    @Autowired
    GraphDatabase graphDatabase;

    @Bean
    GraphDatabaseService graphDatabaseService() {
        return new GraphDatabaseFactory().newEmbeddedDatabase("my-graphdb");
    }

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

When I tried to implement internationalization, the tutor says that I need to implement:

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(localeChangeInterceptor());
}

This code above need the class is extended by WebMvcConfigurerAdapter. This is the problem, I didn't use WebMvcConfigurerAdapter so I cannot add the method above. Do I have another option to make my internationalization work well with Neo4jConfiguration?


Answer:

I just found out how to make it work. I just have to add new file that extend WebMvcConfigurerAdapter and put the addInterceptors module into that new file.

Question:

Trying to run the neo4j spring data example on http://projects.spring.io/spring-data-neo4j/

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

And

@NodeEntity
public class Movie {

  @GraphId Long id;

  @Indexed(type = FULLTEXT, indexName = "search")
  String title;

  Person director;

  @RelatedTo(type="ACTS_IN", direction = INCOMING)
  Set<Person> actors;

  @RelatedToVia(type = "RATED")
  Iterable<Rating> ratings;

  @Query("start movie=node({self}) match 
          movie-->genre<--similar return similar")
  Iterable<Movie> similarMovies;
}

but the @Indexed(type = FULLTEXT, indexName = "search") does not seem to exist in <artifactId>spring-data-neo4j</artifactId> Do I have to add anything else to the pom ? or is this deprecated and if so how should I do it?


Answer:

@Indexed was present in SDN 3, but not anymore in SDN 4. You have to manage indices and constraints yourself, using Cypher queries.

Shameless plug: you can use Liquigraph to manage your migrations.