15 Jul 2015

GreenDao ORM in Android Studio Part 2

Hello!

As I promised in part 1 I will show how to use greendao in your project. I will give some examples of One-To-One and One-To-Many relations

Here is GreenDaoGeneratorM class from greendao module as i showed in part1, fill it with following data

The data structure is pretty simple.

public class GreenDaoGeneratorM {
    public static void main(String args[]) throws Exception {
        generateTaskEntities();
    }

    private static void generateTaskEntities(){
        Schema schema = new Schema(1, "com.somepackage.somepackage.android.dbmodel");

        Entity introWindow=schema.addEntity("IntroWindow");
        introWindow.addIdProperty();
        introWindow.addStringProperty("name");
       

        Entity card = schema.addEntity("Card");
        card.addIdProperty();
        card.addStringProperty("name");
        card.addStringProperty("description");
         

        Entity taskSheet = schema.addEntity("TaskSheet");
        taskSheet.addIdProperty();
        taskSheet.addStringProperty("name");
        taskSheet.addStringProperty("description");
        
        //Creating One-To-One relation, TaskSheet has "one" introWindow
        Property introWindowIdProperty = taskSheet.addLongProperty("introWindowId").getProperty();
        taskSheet.addToOne(introWindow, introWindowIdProperty);

        
        //Creating One-To-Mane relation, TaskSheet has "many" cards
        Property taskSheetIdCard = card.addLongProperty("taskSheetId").notNull().getProperty();
        ToMany taskSheetToCards = taskSheet.addToMany(card, taskSheetIdCard);
        taskSheetToCards.setName("cards"); // Optional

        try {
            new DaoGenerator().generateAll(schema, "../main/src/java");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The TaskSheet instance has one IntroWindow and many Card instances

Just go to Gradle panel and make "run" command and generator will generate the Dao for you (see part1)

Now it's time to use it in the project

DataBaseExample.java

public class DataBaseExample{
	 public void initDatabase(){

        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "task-db", null);
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession daoSession = daoMaster.newSession();

         TaskSheet taskSheetDb = new TaskSheet();
		 taskSheetDb.setName("some name");

		 IntroWindow introWindow = new IntroWindow();
		 introWindow.setName("intro");

		 IntroWindowDao introDao=daoSession.getIntroWindowDao();
		 introDao.insert(introWindow);
		 //method intro Window was generated automatically, id is generated by greendao, but you can also set it by yourself using setId() method
		 taskSheetDb.setIntroWindow(introWindow);
		 taskSheetDd.setIntroWindowId(introWindow.getId());

		  TaskSheetDao taskSheetDao = daoSession.getTaskSheetDao();
          taskSheetDao.insertOrReplace(taskSheetDb);
          
          Card card1=new Card();
          card1.setName("card1");
          //generated method
          card1.setTaskSheetId(taskSheetDb.getId());

          Card card2=new Card();
          card2.setName("card2");
          //generated method
          card2.setTaskSheetId(taskSheetDb.getId());

          CardDao cardDao = daoSession.getCardDao();
          cardDao.insertOrReplace(card1);
          cardDao.insertOrReplace(card2);
    }
    public void checkData(){
    	TaskSheetDao taskSheetDao= daoSession.getTaskSheetDao();
        List taskList = taskSheet.loadAll();
        List cards=taskList.get(0).getCards();
        IntroWindow introwindow=taskList.get(0).getIntroWindow();	
    }
    public void removeAllData(DaoSession daoSession){
        daoSession.getCardDao().deleteAll();
        daoSession.getTaskSheetDao().deleteAll();
    }
}

P.S. maybe you will find some mistakes in code, because it was modified in notepad, but still the most important thing is the logic of how it works

Here i showed simple usage of greendao. There are a lot of other important staff, but all of it you can find in documentation of greendao.

Good luck!

blog comments powered by Disqus