如何使用Java Document API为OrientDB数据库创建自动递增索引/序列?

我通过其Document API使用OrientDB和Java。 我有一个名为items的简单类,它有一个属性ID 。 我明确地声明了这样的架构:

  OSchema schema = db.getMetadata().getSchema(); OClass itemsClass = schema.createClass("items"); itemsClass.createProperty("ID", OType.LONG); 

然后在ID CREATE INDEX items.ID ON items (ID) UNIQUECREATE INDEX items.ID ON items (ID) UNIQUE

现在,当我创建一个新项目(类似于ODocument doc = new ODocument("items")等)时,我希望在数据库上生成新项目的ID(类似于RDBMS中的序列)。 如何使用OrientDB的Java Document API执行此操作?

OrientDB不支持串行(我们有一个问题 ),因此您可以通过这种方式管理自己的计数器(例如使用SQL):

 create class counter insert into counter set name='mycounter', value=0 

然后,每当您需要新号码时,您可以执行以下操作:

 update counter incr value = 1 where name = 'mycounter' 

这以这种方式在SQL批处理中工作:

 begin let $counter = update counter incr value = 1 where name = 'mycounter' return after insert into items set id = $counter.value, qty = 10, price = 1000 commit 

通过使用Java,您可以创建相同的内容:创建单个类“计数器”,每次递增文档值并保存它。

从OrientDB 2.2开始现在支持序列

http://orientdb.com/docs/2.2/Sequences-and-auto-increment.html