Question

I am playing around with GreenDAO and I think I like the way it works. I do not like to write SQL code, so this will help me avoid it ;)

But still, I think it really is very 'sql'-based thinking of how you set it up. Not sure if this is a bad thing (to know how things work), but I'd rather just say: here is my object! Store it! But I haven't found anything for this yet...

But ok, what I am doing is sort of the following: I have an object, let's say a bookshelve. I put some books on the shelve.

This would look something like:

class BookShelve {
    List<Book> books;

    void add(Book b) {
        books.add(b);
    }
}


BookShelve bs = new BookShelve();
Book b1 = new Book();
Book b2 = new Book();
Book b3 = new Book();
bs.add(b1);
bs.add(b2);
bs.add(b3);

ok, now I want to store this using GreenDAO. Do I need to use 'addToMany' for this? So:

Property bookDataProperty = bookData.addLongProperty("bookShelve").getProperty();
ToMany books = BookShelve.addToMany(bookData, bookDataProperty); 
books.setName("Books");

and then do a:

bs.getBooks()

to get them? Or is there a better way?

I think it should be done like this, but I am not fully convinced yet.

ps: there may be typo's in the code above, I just wrote it here, haven't compiled it :)

Edit: This came up after I gave the 'answer' check:

so, to add books to a shelve, I need to:

b1.setBookShelve(bs.getId());
daoSession.insert(b1);

right?

Was it helpful?

Solution

I think you don't have to know SQL, but you'll have to know basic Database knowledge and be clear to your E-R Model, which I think you already have.

If you use greenDAO and implement the relationship between Book and BookShelf, you don't need to keep the list of books in your BookShelf enity.

In your DaoGenerator.java , you simply declare the schema

    Entity bookShelf = schema.addEntity("BookShelf");
    bookShelf.addIdProperty();

    Entity book = schema.addEntity("Book");
    book.addIdProperty();
    book.addStringProperty("name");
    Property bookFKShelves = book.addLongProperty("bookShelf_id").notNull().getProperty();

    book.addToOne(bookShelf, bookFKShelves);
    bookShelf.addToMany(book, bookFKShelves);

Once your generate the code. Use them in your Android code:

        DevOpenHelper helper = new DaoMaster.DevOpenHelper(this.getActivity(),
                "BookStore", null);
        db = helper.getWritableDatabase();
        mDaoMaster = new DaoMaster(db);
        mDaoSession = mDaoMaster.newSession();
        BookDao mBookDao = mDaoSession.getBookDao();
        BookShelfDao mBookShelfDao = mDaoSession.getBookShelfDao();

        // Insert
        BookShelf bs = new BookShelf();
        Book b1 = new Book();   b1.setName("B1");   b1.setBookShelf(bs);
        Book b2 = new Book();   b2.setName("B2");   b2.setBookShelf(bs);
        Book b3 = new Book();   b3.setName("B3");   b3.setBookShelf(bs);

        mBookDao.insert(b1);
        mBookDao.insert(b2);
        mBookDao.insert(b3);
        mBookShelfDao.insert(bs);

        // Select
        for(BookShelf bss :mBookShelfDao.loadAll()){
            for (Book b :bss.getBookList())
                Lod.d(TAG,"Book Name: "+b.getName());
        }

OTHER TIPS

Basically you don't need to know sql for greendao:

You just define your entities (object-classes) and relations between them.

To your question:

There are two possibilities:

  • Bookshelve can contain many books and one book can only be in one bookshelve: Your approach is correct.
  • bookshelve can contain many books and one book can be in many bookshelves: You should define an emtity bookshelve2book which stores the pairs.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top