如何使用Java Document API为OrientDB数据库创建自动递增索引/序列? [英] How do I create an auto-incrementing index/a sequence for an OrientDB database using the Java Document API?

查看:147
本文介绍了如何使用Java Document API为OrientDB数据库创建自动递增索引/序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using OrientDB with Java via its Document API. I have a simple Class called items which has an attribute ID. I explicitly declare the schema like this:

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

然后在 ID 上创建一个索引: CREATE INDEX items.ID ON项目(ID)UNIQUE

and then create an index on ID: CREATE INDEX items.ID ON items (ID) UNIQUE.

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

Now when I create a new item (something like ODocument doc = new ODocument("items") etc.), I would like the ID for the new item to be generated on the database (something like a sequence in RDBMS). How do I do this with the Java Document API for OrientDB?

推荐答案

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

OrientDB doesn't support serial (we've an issue for that), so you can manage your own counter in this way (example using SQL):

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

然后每次需要新号码时都可以:

And then every time you need a new number you can do:

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

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

This works in a SQL batch in this way:

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,你可以做同样的事情:创建单身类计数器,每次我增加文档值并保存。

By using Java you can make the same: create singleton class "Counters" that everytime increment the document value and save it.

这篇关于如何使用Java Document API为OrientDB数据库创建自动递增索引/序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆