extremedb java_在JAVA中使用eXtremeDB autoid

在JAVA中使用eXtremeDB autoid,主要有以下几个问题:

定义

插入数据

已经获取记录,如何获得autoid

知道autoid,如何获取记录

定义autoid

在类的定义前加入注释:@Persistent(autoid=true)注意,eXtremeDB中的autoid并不需要单独的定义出一列表示,只需要在类级别定义即可。

插入数据

正常的使用insert方法插入即可,无需关注autoid。如果需要知道系统为记录生成的autoid值,可以通过insert方法的返回值。

通过记录获得autoid

可以先通过Cursor类的find方法,先把游标定位到指定记录,然后通过Cursor类的getAutoId()方法获取。

通过autoid获取记录

创建一个不指定索引名的Cursor,直接把autoid传递到find方法即可

Cursor cp = new Cursor(con, Person.class);

Person p3 = cp.find(prh2.personID);完整的样例程序如下

import com.mcobject.extremedb.Connection;

import com.mcobject.extremedb.Cursor;

import com.mcobject.extremedb.Database;

import com.mcobject.extremedb.Indexable;

import com.mcobject.extremedb.Persistent;

@Persistent(autoid=true) // class will be stored in eXtremeDB database, declare autoid indexes

class Person {

Person(String name) {

this.name = name;

}

@Indexable(unique=true, type=Database.IndexType.Hashtable)

String name;

}

@Persistent(autoid=true) // class will be stored in eXtremeDB database, declare autoid indexes

class House {

House(String address) {

this.address = address;

}

@Indexable(unique=true, type=Database.IndexType.Hashtable)

String address;

}

@Persistent(autoid=true)

class Person_R_House {

Person_R_House(long personID, long houseID) {

this.personID = personID;

this.houseID = houseID;

}

@Indexable(unique=false, type=Database.IndexType.Hashtable)

long personID;

@Indexable(unique=false, type=Database.IndexType.Hashtable)

long houseID;

}

public class TestAutoId {

static final int PAGE_SIZE = 128;

static final int DISK_PAGE_SIZE = 4096;

static final int DISK_CACHE_SIZE = 4*1024*1024;

static final int DATABASE_SIZE = 16*1024*1024;

static final int N_PERSON = 5;

static final int N_HOUSE = 5;

public static void main(String[] args) {

Database db = new Database();

Database.Parameters params = new Database.Parameters();

params.memPageSize = PAGE_SIZE;

params.classes = new Class[]{Person.class, House.class, Person_R_House.class};

db.open("testAutoIDdb", params, DATABASE_SIZE);

Connection con = new Connection(db);

//

// Insert data in the database

//

con.startTransaction(Database.TransactionType.ReadWrite);

for (int i = 1; i &lt= N_PERSON; i++) {

con.insert( new Person("Person-" + i));

con.insert( new House("House-" + i));

}

con.commitTransaction();

//

// associate existing Person and existing House

//

con.startTransaction(Database.TransactionType.ReadWrite);

Cursor cursorP = new Cursor(con, Person.class, "name");

Cursor cursorH = new Cursor(con, House.class, "address");

Person p1 = cursorP.find("Person-1");

House h1 = cursorH.find("House-2");

if( p1 != null && h1 != null) {

Person_R_House prh = new Person_R_House(cursorP.getAutoId(),cursorH.getAutoId());

con.insert(prh);

}

cursorP.close();

cursorH.close();

con.commitTransaction();

//

// associate new Person and new House

//

con.startTransaction(Database.TransactionType.ReadWrite);

Person p2 = new Person("Person-new");

House h2 = new House("House-new");

long pID = con.insert(p2);//because the class was annotated with (autoid=true),

//the insert method returns the generated AUTOID of the newly create object

long hID = con.insert(h2);

Person_R_House prh = new Person_R_House(pID,hID);

con.insert(prh);

con.commitTransaction();

//

// show the relationship between Person and House

//

con.startTransaction(Database.TransactionType.ReadOnly);

Cursor cursorPRH = new Cursor(con, Person_R_House.class);

for(Person_R_House prh2 : cursorPRH) {

Cursor cp = new Cursor(con, Person.class);

Cursor ch = new Cursor(con, House.class);

Person p3 = cp.find(prh2.personID);

House h3 = ch.find(prh2.houseID);

System.out.format("%s has a house, address is:%s \n", p3.name, h3.address);

cp.close();

ch.close();

}

cursorPRH.close();

con.commitTransaction();

con.disconnect();

db.close();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值