Database Reference
In-Depth Information
Applying unicity constraints with Java
Let's create/retrieve/delete the unique constraints using the Java API.
The following Java code creates a unique constraint on the Artist label:
// Get Object of Graph Service
GraphDatabaseService graphDb = new GraphDatabaseFactory()
.newEmbeddedDatabase(DBLocation);
// Open a Transaction
try (Transaction tx = graphDb.beginTx()) {
//Get Schema from Graph Database Service
Schema schema = graphDb.schema();
//Define Unique Constraint and Label and Property
ConstraintCreator creator =
schema.constraintFor(DynamicLabel.label("Artist")).assertPropertyIsUnique("Age");
//Create the Constraint
creator.create();
tx.success();
tx.close();
} catch (Exception e) {e.printStackTrace();}
The following Java code lists all constraints on the Artist label:
// Get Object of Graph Service
GraphDatabaseService graphDb = new GraphDatabaseFactory()
.newEmbeddedDatabase(DBLocation);
// Open a Transaction
try (Transaction tx = graphDb.beginTx()) {
Schema schema = graphDb.schema();
for (ConstraintDefinition def :
schema.getConstraints(DynamicLabel.label("Artist"))) {
System.out.println("Label = Artist," + "Type of
Constraint = "+ def.getConstraintType());
System.out.print("ON Properties = ");
for (String keys : def.getPropertyKeys()) {
System.out.print(keys);
}
System.out.println();
Search WWH ::




Custom Search