Android ORM-GreenDao学习二之进阶篇
概述
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
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)
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();
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。