标签:
// The variables "user" and "picture" are just regular entities Property pictureIdProperty = user.addLongProperty("pictureId").getProperty(); user.addToOne(picture, pictureIdProperty);这将导致产生的User实体类中有一个Picture属性(getPicture/setPicture);
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();
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();
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();
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();
标签:
原文地址:http://blog.csdn.net/xbynet/article/details/45394403