Android ORM-GreenDao学习二之进阶篇
来源:程序员人生 发布时间:2015-05-12 09:01:10 阅读次数:5278次
概述
本文讲述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实体类中有1个Picture属性(getPicture/setPicture);
Relation Names and multiple Relations
每个关联都有1个名称。默许情况下关联的名称就是目标实体的名称。所以1般情况下建议主动设置该关联的名称以避免重名。可以通过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类将多出1个getOrders()
List orders = customer.getOrders();
Resolving and Updating To-Many Relations
To-Many解析第1次使用懒加载,但是1旦加载以后to-many list就会被缓存到1个List当中,后续的要求不会再通过数据库,而是直接从缓存中返回,所以1旦修改以后,需要对缓存中的数据进行更新。
由于缓存的作用下面的代码会产生使人困惑的结果:
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);//更新缓存
对删除操作也是1样的。:
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();
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠