Overview This article describes Greendao support for relational data. respectively, Toone, Tomany. Two-Way Association tree Relationsto-one is equivalent to a foreign key relationship.
The variables "user" and "picture" is just regular entitiesproperty pictureidproperty = User.addlongproperty ("Picturei D "). GetProperty (); User.addtoone (picture, pictureidproperty);
This causes the resulting user entity class to have a picture attribute (getpicture/setpicture); Relation Names and multiple relations each association has a name. By default, the name of the association is the name of the target entity. So the general recommendation
proactively set the name of the association to avoid duplicate names。 Can be set by SetName ().
Property Pictureidproperty = User.addlongproperty ("Pictureid"). GetProperty (); Property Thumbnailidproperty = User.addlongproperty ("Thumbnailid"). GetProperty (); User.addtoone (picture, Pictureidproperty);//Use the default relationship name User.addtoone (picture, Thumbnailidproperty, "thumbnail");//To prevent duplicate names, set the relationship named thumbnail
Property CustomerId = Order.addlongproperty ("CustomerId"). Notnull (). GetProperty (); Tomany customertoorders = Customer.addtomany (order, customerId); Customertoorders.setname ("Orders"); OPTIONALCUSTOMERTOORDERS.ORDERASC (orderDate); Optional
The resulting code in the customer class will have one more getorders ()
List orders = Customer.getorders ();
Resolving and Updating To-many relations
To-many parsing uses lazy loading for the first time, but once loaded, the To-many list is cached in a list, and subsequent requests are no longer passed through the database, but are returned directly from the cache, so once modified, the data in the cache needs to be updated. Because of the role of caching, the following code can produce confusing results:
List orders1 = Customer.getorders (); int size1 = Orders1.size (); Order order = New Order (); Order.setcustomerid ( Customer.getid ());d Aosession.insert (order); Listorders2 = Customer.getorders ();//Size1 = = Orders2.size (); Not updated//orders1 = = orders2; Same List Object
So we need to updating the cache to correct the following code:
List orders = Customer.getorders (); Neworder.setcustomerid (Customer.getid ());d Aosession.insert (neworder); o Rders.add (Neworder);//Update cache
The same is true for delete operations. :
List orders = customer.getorders ();d aosession.delete (Neworder); Orders.remove (neworder);//Update cache
But if there's a time when it's not going to meet your expectations or it's difficult to update the cache, it's okay Greendao also provides the following methods Resetxxx ()Resetting the
cache:
Customer.resetorders (); List orders2 = Customer.getorders ();
Bi-directional correlation to-one and To-many use
Entity customer = schema.addentity ("customer"); Customer.addidproperty (); Customer.addstringproperty ("name"). Notnull (); Entity order = schema.addentity ("order"); Order.settablename ("ORDERS"); "ORDER" is a reserved keywordorder.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);
This creates a two-way association.
List Allordersofcustomer = Order.getcustomer (). getorders ();
Many-to-many relations (N:M) is currently Greendao not implemented. Modelling tree relations You can model a Tree relation by modelling an entity
Have 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");
Then we can navigate through the generated code:
Treeentity parent = Child.getparent (); List grandchildren = Child.getchildren ();
Android Orm-greendao Learning Shijing Chapter