comparable java_java rectangle
在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();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/182219.html原文链接:https://javaforall.cn
相关文章
- Java项目毕业设计:基于springboot+vue的电影视频网站系统「建议收藏」
- java转换字符串为时间_JAVA字符串转日期或日期转字符串
- java控制台输入数组_Java控制台输入数组并逆序输出的方法实例
- java webservice 实例_Java WebService 简单实例(附实例代码)
- java random.nextint_java Random.nextInt()方法的具体使用
- java url加密_Java实现url加密处理的方法示例
- java mutator,Java – 使用Accessor和Mutator方法「建议收藏」
- delphi 进程通信_多线程实现java
- 【Java基础】JDK和JRE的安装+配置环境变量
- 【Java】什么是线程?Thread和Runnable区别
- java控制流程语句switch
- java并发编程(1):Java多线程-基本线程类-基础知识复习笔记
- IntelliJ IDEA 2023 for Mac(编程语言、Java开发工具)
- Hbase三Java,python操作Hbase详解大数据
- Java中CountDownLatch用法详解编程语言
- Java学习笔记之三java中的变量和常量详解编程语言
- Java中的枚举用法详解编程语言
- Java操作MySQL数据库快速入门(java访问mysql)
- 时间Java中Redis实现的设置过期时间策略(redisjava过期)
- 机制Redis Java实现的过期机制(redisjava过期)
- 测试Java操作Redis实例(java测试redis)
- 构建Java应用程序中Redis集群的方法(java连redis集群)
- Java中使用Redis包实现高效缓存(redis包java)
- 在Linux上编译Java 实用教程指南(linux上编译java)
- 市场Oracle公司助力Java攻克市场(oracle公司java)
- 通往成功的道路通过Java考证Oracle获取更高的成就(java考证oracle)
- 项目Java项目中展现Redis新型性能优势(redis运用在java)
- PHP与已存在的Java应用程序集成
- 一个合格的程序员应该读过哪些书(偏java)
- Java中new关键字和newInstance方法的区别分享