Annotation Type Unique
@Retention(CLASS)
@Target({TYPE,FIELD})
@Documented
@Repeatable(UniqueProperties.class)
public @interface Unique
Annotation to mark one or more fields of a class as unique properties for a given node or relationship. You will want
to use it on a type for building composite constraints (constraints spanning more than one field) and on single fields
for single property constraints.
When is this annotation useful?
- You have a plain Java application with some models but no object mapper on the classpath but you still want to derive content
- You are a Spring Data Neo4j (SDN6+) user and want to enrich your models with unique or existential constraints
- You are an Neo4j-OGM user but are looking for an object mapper agnostic way of enriching your model
Determining labels or types: Depending on your approach and combination with other annotations (such as SDN6+ or OGM annotations), the process is as following
- Explicitly use
label()
- On a plain class: The simple class name is used as label (we default to targeting nodes)
- If combined any of the following SDN6+ annotations
org.springframework.data.neo4j.core.schema.Node
ororg.springframework.data.neo4j.core.schema.RelationshipProperties
, we default to the SDN6 approach - If combined with an Neo4j-OGM annotation, we default to the Neo4j-OGM approach
Unique constraints are only supported for Nodes / Labels, not for Relationships / Types.
This annotation can also be repeated on a class. If you decide to put contradicting information on a class, for example targeting labels with one annotation and relationships with another, we won't stop you as long as we can resolve both error free: You might end up with a catalog that defines several constraints coming from one type and aiming for both nodes and relationship constraints.
- Since:
- 1.15.0
- Author:
- Michael J. Simons
- Soundtrack
- Juse Ju - Shibuya Crossing
-
Optional Element Summary
Modifier and TypeOptional ElementDescriptionIf this is not null it has precedence over an implicit label (either no class annotations or one without a dedicated label) but not over OGM or SDN6 annotations specifying the label or type explicitly.String[]
Use this if you want to define composite, unique constraint when usingUnique
on the class level.
-
Element Details
-
label
String labelIf this is not null it has precedence over an implicit label (either no class annotations or one without a dedicated label) but not over OGM or SDN6 annotations specifying the label or type explicitly. Its use must be consistent throughout the class.- Returns:
- The target label
- Default:
- ""
-
properties
String[] propertiesUse this if you want to define composite, unique constraint when usingUnique
on the class level. Leave it empty when using on field level, otherwise an exception will be thrown.- Returns:
- The list of properties to include in the composite.
- Default:
- {}
-