Spring Data Neo4J @Index(unique = true) does not working

neo4j drop index
neo4j limitations
neo4j constraints

I want to make userName property in User node as a unique. I used below code but it doesn't create a unique constraint in the Neo4j database.

@Property(name = "name")
@Index(unique = true)
private String usreName;

FYI, I'm using the Neo4j Server version: 3.3.6 (community) With Spring Boot 2. but if I create a constraint in the Neo4j Browser by myself, it works.


Is there a way to force Spring Data Neo4J to create unique properties, without creating them by myself in Database?

You need to configure the auto index manager if you want the application code create the constraints. You can find the best fitting option in the documentation: https://docs.spring.io/spring-data/neo4j/docs/current/reference/html/#reference:indexing:creation

Just a note on this topic: Think about the auto index creation like Hibernate's DDL support. It is a helper at development time. You should not use assert and update in production environments but only validate.

Spring Data Neo4J @Indexed(unique = true) not working, I'm new to Neo4J and I have, probably an easy question. There're NodeEntitys in my application, a property (name) is annotated with @Indexed(unique = true)  In Spring Data Neo4j this is done by setting the unique=true property on the @Indexed annotation. Methods for programmatically getting and/or creating unique entities is available on the Neo4jTemplate class, namely getOrCreateNode and getOrCreateRelationship for legacy indexes, and merge for schema based unique entities.


In Spring Data Neo4j 4, index management concerns were removed from the mapping framework entirely.

(from Index Management in Spring Data Neo4j)

private SessionFactory sessionFactory;

public void createIndexesAndConstraints() {
    Session session = sessionFactory.openSession();
    Result result = session.query("CREATE INDEX ON :User(userName)", Collections.EMPTY_MAP);

Spring Data Neo4J @Index(unique = true) does not work · Issue , In the documentation, it is said that to make a property as a unique, you should use @INDEX(unique = true) above your property. But when I  The implementation itself does not depend on Spring Data and can be a regular Spring bean. Consequently, you can use standard dependency injection behavior to inject references to other beans (such as a JdbcTemplate), take part in aspects, and so on.

You can configure the mode our auto index manager works in through application.properties

spring.data.neo4j.auto-index=validate # or
# spring.data.neo4j.auto-index=update
# spring.data.neo4j.auto-index=assert

Default mode is none. Apart from that, what @meistermeier says applies.

Also, Neo4jOperations was deprecated in SDN 4 something and has been removed in SDN 5. Use Session instead for operations "near" the database.

@CompositeIndex not working, Hi, I'm using @CompositeIndex for creating index on multiple attributes on a node. {"indexValue1","indexValue2"}, unique=true) public class CompIndTestNode indexValue1 does not exists. at org.springframework.beans​.factory.support. The Spring Data Neo4J project applies core Spring concepts to the development of solutions using the Neo4j graph data store. We provide "repositories" as a high-level abstraction for storing and querying documents. You will notice similarities to the JPA/Hibernate support in the Spring Framework.

Thank you @ThirstForKnowledg for your answer. But I have 3 other Questions: 1- I'm using Spring Boot 2, and I can not see Neo4jOperations in my classpath to import it.

2- Should I put this in my Entity node or in another bean? 3- What about after running my application two or more times? I think it would cause an exception for the second time or more.

Chapter 3. Reference - Neo4j-OGM, This reference documentation is broken down into sections to help the user understand Spring Boot Version, Spring Data Release Train, Spring Data Neo4j Version, Neo4j-OGM Version For @Index(unique=true) a constraint is created. The Spring Data Neo4j 3 release required a significant update of the code-base in order to support the new features and changes in Neo4j 2.0. Many of these changes included refactoring to accommodate mandatory read transactions and the removal of the reference node, as well as the required transaction-separation for schema operations.

5.4. Constraints - Chapter 5. Administration, This section explains how to manage constraints used for ensuring data Working with null Unique constraints do not mean that all nodes have to have a unique value The same is true for queries that try to remove the mandatory property. If a node key constraint is dropped and the composite-property index on the  In Spring Data Neo4j 4, a mandatory specific field for entity id had to be used (of type java.lang.Long, either named id, or annotated with the @GraphId annotation). If an application wanted to lookup entities by another attribute, it had to add a primary index via @Index(primary = true, unique = true).

A Big Step Forward: Spring Data Neo4j 5.0 Release, Learn what's new in the latest Spring Data Neo4j (SDN) 5.0 release and the Neo4j For over half a year the SDN/OGM team has been working on new Your browser does not currently recognize any of the video formats available. it had to add a primary index via @Index(primary = true, unique = true) . In this tutorial we've covered some core concepts of working with Spring Data MongoDB – indexing, common annotations and converters. The implementation of all these examples and code snippets can be found in my github project – this is an Eclipse based project, so it should be easy to import and run as it is.

Spring Data Neo4j, A working example of using the BOMs can be found in our Spring Data examples repository. Using a unique Spring Data module in your application makes things simple, For @Index(unique=true) a constraint is created. We do not choose Spring Data Neo4j here, as it will show the previous generation of Spring Data Neo4j that has only imperative support, OGM, and additional abstraction over the driver. Once those steps are complete, we can click the Generate button at the bottom to create the skeleton for our project and download it.

  • @HadiMohammadi To your first question: Neo4jOperations was deprecated and removed in the meantime. Sorry for the inconvenience you may had. Please execute your index building query by means of a Neo4j session. It is reflected in the updated answer now.
  • @HadiMohammadi To your second question: You can place the code snippet in an arbitrary class, that is annotated with @Component. During the Spring Boot application start the bean is scanned and the method annotated by @PostConstruct is run once just after all services initialized.
  • @HadiMohammadi To your third question: It is answered in the separated question: StackOverflow: launching-a-create-index-on-when-the-index-already-exists-in-neo4j).
  • @meistermeier @MichaelSimons Unfortunately the activation of the auto index manager in update or assert mode works neither through application.properties nor ogm.properties for me. Using Neo4j 3.4.7 I get no related warn/info message or exception, the indexes are simply not created despite @NodeEntity and @Index(unique = true) annotations. It seems as if the property entry is ignored. Executing CALL db.indexes(); and CALL db.constraints(); produces no result.