Instead of this:

person.createIndex("nameIdx", OClass.INDEX_TYPE.UNIQUE, "name");


Try using this:

final Parameter<?, ?> UNIQUE_INDEX = new Parameter<String, String>("type",
"UNIQUE_HASH_INDEX");
graph.createKeyIndex("name", Vertex.class, new Parameter<String,
String>("class", "Person"), UNIQUE_INDEX);


Which will result in a unique index (Person.name) on the Person class.
Another way to achieve the same objective would be this:

OCommandSQL cmd = new OCommandSQL();
cmd.setText("create index Person.name on Person (name) unique" );
graph.command(cmd).execute();


I would also recommend using an OrientGraphFactory to get your graphs from,
as follows:


OrientGraphFactory factory = new OrientGraphFactory("memory:test", 'admin',
'admin');
OrientGraphNoTx graph = factory.getNoTx();
// ... create database ...
graph.shutdown();

OrientGraph graphtx = factory.getTx();
// ... populate data ...
graphtx.shutdown();

I've seen errors when trying to manipulate the database schema when using a
transactional graph...

- Craig -



On Wed, May 13, 2015 at 4:49 PM, Timm Baumeister <[email protected]>
wrote:

> Hi, I am new to orientdb. So far I could not figure out how to make unique
> indices work. This is the test code:
>
>
>
> import java.util.HashMap;
> import java.util.Iterator;
> import java.util.Map;
>
> import com.orientechnologies.orient.core.metadata.schema.OClass;
> import com.orientechnologies.orient.core.metadata.schema.OType;
> import com.tinkerpop.blueprints.Vertex;
> import com.tinkerpop.blueprints.impls.orient.*;
>
> public class OrientDBTest {
> public static void main(String[] args) {
> OrientGraph graph = new
> OrientGraph("memory:test");//"plocal:c:/tmp/orientdb"
> OrientVertexType person = graph.createVertexType("Person");
> person.createProperty("name", OType.STRING);
> person.createProperty("age", OType.INTEGER);
> person.createIndex("nameIdx", OClass.INDEX_TYPE.UNIQUE, "name");
> graph.commit();
> long t = System.currentTimeMillis();
> for (int i=0;i<10000;i++) {
> OrientVertex v1 = graph.addVertex("class:Person");
>  Map<String,Object> props = new HashMap<String,Object>();
> props.put("name", "Jill");
> props.put("age", 33);
> v1.setProperties(props);
> if (i%1000==0)
> graph.commit();
> }
> graph.commit();
> System.out.println("Time: "+(System.currentTimeMillis()-t));
> Iterator<Vertex> it = graph.getVerticesOfClass("Person").iterator();
> System.out.println(it.next().getProperty("name") + " "
> +it.next().getProperty("name"));
>  }
> }
>
> Which generates 10000 records with name 'Jill' even though there is a
> unique index on name. Why does this not fail? Is there anything else I need
> to set?
>
> Thanks
> Timm
>
>  --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "OrientDB" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to