Search code examples
greendao

Can greendao automatically add changes done in dao model to database.


I have added a ToMany relationship in below code and I want to insert 10 items in a store using storedao. GreenDao suggests something like this.

//Create Store Entity
Entity store = schema.addEntity("Store");
store.addIntProperty("StoreId");
store.addStringProperty("StoreName");


//Create Item Entity
Entity item = schema.addEntity("StoreItem");
AssetKit.addIntProperty("ItemId");
AssetKit.addStringProperty("ItemName");

//Add ToMany property in store.
Property property = item.addLongProperty("storeId").getProperty();
store.addToMany(Item, property,"itemsList");

//greenDAO suggests:
storeDao = daoSession.getStoreDAO();
itemDao = daoSession.getItemDao();

Store store = storeDao.queryBuilder().limit(1).list();
List<Item> list = store.getItemList();

for(int i= 0 ; i < 10; i++)
{
    Item item = new Item(i, "name"+"i", store.storeId);
    daoSession.insert(item);
    list.insert(item);
}

The above code will add the items list in database and to temporarily created list. I do not want to add 10 items to database. I need the storeDAO to this for me. Is there a way to do this.

// I need something list this

for(int i= 0 ; i < 10; i++)
{
    Item item = new Item(i, "name"+"i", i);
    list.insert(item);
}
storeDao.update(store);

Is there a way for storeDAO to insert all the items to ITEMS table too. Please suggest.


Solution

  • I never used it, but according to the documentation, it seems possible with this:

    insertInTx

    public void insertInTx(java.lang.Iterable<T> entities)
    

    Inserts the given entities in the database using a transaction. Parameters: entities - The entities to insert.