大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
在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 <= 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();
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/182219.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...