3
votes

I am fairly new to Neo4J, and have been trying to build a social network app using Cypher queries as annotated queries in Spring Neo4J.

How can I restrict the user node to have only one relationship with the address node using a cypher command/query or any configuration? Basically, I don't want the user to have multiple address relationships.

2
You can't do this yet from the neo4j side, although I think you can from the SpringDataNeo4j side. We'll see what the Spring guys say.Eve Freeman
Can the Create Unique statement be used in any way in this case ?ayan_2587

2 Answers

0
votes

You can do this using a TransactionEventHandler. A TransactionEventHandler is a component written in Java and registered with your graphDatabaseService instance. In it's beforeCommit you can veto the transaction.

Be warned, writing and using TransactionEventHandler is a advanced concept in Neo4j.

0
votes

You can model your User entity like this:

@NodeEntity 
public class User {

@GraphId Long id;

@Indexed String name;

@RelatedTo(type="ADDRESS")
Address address;

}

Then SDN will make sure that there is only one ADDRESS relationship from an User to an Address.