您的位置:首页 > 其它

Db4o的级联(Activation-Concept)概念

2012-01-16 13:47 162 查看
场景:在地球上任意找一个人然后让他找出他认识的任意五个人,这五个人成为初始条件……递归。

结果:通过这样的查询方式会把地球上的每个人都找出来。

假设:任何数据库执行这样的操作恐怕没有不崩溃的。

现实:Db4o数据库的默认查询方式就是这样的!查询一个对象的时候如果这个对象和其他对象有关联那么被关联的对象也一并查询出来。

 

Db4o通过 引入Activation-Concept理念 来解决上面的问题。解决问题的思路:限定级联的“级”数。

关键字:“级”,depth

Db4o已经使用了默认查询级联“级数”是 4 级——不包括自身在内。

改变级联“级数”的方法

private static void increaseActivationDepth() {
// #example: Increase the activation depth to 10
EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();
configuration.common().activationDepth(10);
ObjectContainer container = Db4oEmbedded.openFile(configuration,DATABASE_FILE);
// #end example
try {
Person joelle = queryForJoelle(container);
Person julia = joelle.getMother().getMother().getMother().getMother().getMother();

boolean isActivated = container.ext().isActive(julia);
System.out.println("Is activated? "+ isActivated);

} finally {
container.close();
}
}


private static EmbeddedConfiguration moreActivationOptions() {
// #example: More activation options
EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();
// At least activate persons to a depth of 10
configuration.common().objectClass(Person.class).minimumActivationDepth(10);
// Or maybe we just want to activate all referenced objects
configuration.common().objectClass(Person.class).cascadeOnActivate(true);
// #end example
return configuration;
}


db4o向数据库中存储未发现有“级数”限制

db4o级联更新要明确指定是否可级联更新,默认不能级联更新。

 

//修改数据库配置对每个操作的级联更新级数设置为2
EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();
configuration.common().updateDepth(2);

//对“类”设置级联:
EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();
// Update all referenced objects for the Driver class
configuration.common().objectClass(Driver.class).cascadeOnUpdate(true);


 

看看Db4o官方教程上是怎么说的 Db4o Reference book (java) Activation

Db4o的官方教程是一个“家谱”的例子



以上是不设置级联“级数”的场景



设置级联“级数”的场景
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: