Android ORM-GreenDao学习二之进阶篇

概述

本文讲述GreenDao对关系数据的支持。分别为
ToOne, 
ToMany.
双向关联
Tree Relations

To-One

相当于外键关系。
// The variables "user" and "picture" are just regular entities
Property pictureIdProperty = user.addLongProperty("pictureId").getProperty();
user.addToOne(picture, pictureIdProperty);
这将导致产生的User实体类中有一个Picture属性(getPicture/setPicture);

Relation Names and multiple Relations

每一个关联都有一个名称。默认情况下关联的名称就是目标实体的名称。所以一般情况下建议主动设置该关联的名称以免重名。可以通过setName()来设置。
Property pictureIdProperty = user.addLongProperty("pictureId").getProperty();
Property thumbnailIdProperty = user.addLongProperty("thumbnailId").getProperty();
user.addToOne(picture, pictureIdProperty);//使用默认的关系名
user.addToOne(picture, thumbnailIdProperty, "thumbnail");//为了防止重名,设置关系名为thumbnail
Property customerId = order.addLongProperty("customerId").notNull().getProperty();
ToMany customerToOrders = customer.addToMany(order, customerId);
customerToOrders.setName("orders"); // Optional
customerToOrders.orderAsc(orderDate); // Optional
产生的代码中Customer类将多出一个getOrders()
List orders = customer.getOrders();

Resolving and Updating To-Many Relations

To-Many解析第一次使用懒加载,但是一旦加载之后to-many list就会被缓存到一个List当中,后续的请求不会再通过数据库,而是直接从缓存中返回,所以一旦修改之后,需要对缓存中的数据进行更新。
由于缓存的作用下面的代码会产生令人困惑的结果:
List orders1 = customer.getOrders();
int size1 = orders1.size();

Order order = new Order();
order.setCustomerId(customer.getId());
daoSession.insert(order);

Listorders2 = customer.getOrders();
// size1 == orders2.size(); // NOT updated
// orders1 == orders2; // SAME list object
所以我们需要对缓存进行Updating
改正后的代码如下:
List orders = customer.getOrders();
newOrder.setCustomerId(customer.getId());
daoSession.insert(newOrder);
orders.add(newOrder);//更新缓存
对于删除操作也是一样的。:
List orders = customer.getOrders();
daoSession.delete(newOrder);
orders.remove(newOrder);//更新缓存
但是如果有个时候这些没法达到你预期的要求或者是更新缓存比较困难的情况下,没关系greendao还提供如下方法resetXxx()重置缓存
customer.resetOrders();
List orders2 = customer.getOrders();

双向关联To-One与To-many结合使用

Entity customer = schema.addEntity("Customer");
customer.addIdProperty();
customer.addStringProperty("name").notNull();

Entity order = schema.addEntity("Order");
order.setTableName("ORDERS"); // "ORDER" is a reserved keyword
order.addIdProperty();
Property orderDate = order.addDateProperty("date").getProperty();
Property customerId = order.addLongProperty("customerId").notNull().getProperty();
order.addToOne(customer, customerId);

ToMany customerToOrders = customer.addToMany(order, customerId);
customerToOrders.setName("orders");
customerToOrders.orderAsc(orderDate);
这样便产生了双向关联了。
List allOrdersOfCustomer = order.getCustomer().getOrders();

Many-to-Many Relations (n:m)

目前greendao还没有实现。

Modelling Tree Relations

You can model a tree relation by modelling an entity having a to-one and a to-many relation pointing to itself:
Entity treeEntity = schema.addEntity("TreeEntity");
treeEntity.addIdProperty();
Property parentIdProperty = treeEntity.addLongProperty("parentId").getProperty();
treeEntity.addToOne(treeEntity, parentIdProperty).setName("parent");
treeEntity.addToMany(treeEntity, parentIdProperty).setName("children");
然后再生成的代码中我们就可以进行导航了:
TreeEntity parent = child.getParent();
List grandChildren = child.getChildren();


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。