0

I'm developing a Hotel-booking app and I use greenDao.

I create a Hotel entity. This is the entry in my MyGreenDaoGenerator:

//  Hotels
private Entity createHotels() {

    Entity hotels = createEntity("Hotel", "hotels");

    hotels.addStringProperty("hotel_id").notNull().primaryKey().unique();
    hotels.addStringProperty("name").notNull();
    hotels.addStringProperty("country").notNull();
    hotels.addStringProperty("city").notNull();
    hotels.addStringProperty("address").notNull();
    hotels.addStringProperty("lt");
    hotels.addStringProperty("lg");
    hotels.addStringProperty("location");
    hotels.addIntProperty("stars").notNull();
    hotels.addFloatProperty("vote").notNull();
    hotels.addBooleanProperty("viewed").notNull();
    hotels.addBooleanProperty("favorite").notNull();
    hotels.addLongProperty("timestamp");

    return hotels;
}

As you can see, there is no "pictures" node. But I know Hotels have pictures, so I create a Pictures entity and relate it to hotels

//  Pictures
private Entity createPictures() {

    Entity pictures = createEntity("HotelPicture", "hotel_pictures");

    pictures.addStringProperty("image_url").notNull().primaryKey();
    pictures.addStringProperty("image_type").notNull();
    pictures.addStringProperty("image_path");

    //  PK

    Entity hotels = getEntity("Hotel");
    ToMany hotelToPictures = hotels.addToMany(pictures, pictures.addStringProperty("hotel_id").notNull().getProperty());
    hotelToPictures.setName("pictures");

    return pictures;
}

Easy.

In the greenDAO-generated "Hotel.java" now I get a suitable property:

protected List<HotelPicture> pictures;

Now I run my app and get some hotels from a webservice. Hotels include a Pictures node with a picture.

 "pictures":[
      {
        "image_type":"medie",
        "image_url":"url_to_the_picture"
      }
    ],

Now, in the self-generated Hotel.java and HotelDao.java I can't see any method about setting a picture. I just have these:

public List<HotelPicture> getPictures() {
    if (pictures == null) {
        if (daoSession == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        HotelPictureDao targetDao = daoSession.getHotelPictureDao();
        List<HotelPicture> picturesNew = targetDao._queryHotel_Pictures(hotel_id);
        synchronized (this) {
            if(pictures == null) {
                pictures = picturesNew;
            }
        }
    }
    return pictures;
}

public synchronized void resetPictures() {
    pictures = null;
}

What I expect is one of the followings:

  • GreenDAO is clever enough to see that there's a picture node in my Hotel object and saves it in the database
  • GreenDAO is not clever enough. Pictures don't get saved in DB, should do that manually

What happens is that when I switch to the show-me-the-data activity, I see the picture. And I do after this:

  1. I get hotels from DB

    hotelsList = (ArrayList) daoSession.getHotelDao().queryRaw(", hotel_agreements A WHERE A.HOTEL_ID = T.HOTEL_ID AND A.AGREEMENT_ID bla bla bla... no mention of HotelPictures here");

  2. Show the pictures

    hotel.getPictures()

This leads to the before mentioned method, where the condition "if (pictures == null)" is evaluated and pictures is not null at all! It contains my HotelPicture object!

This seems to confirm my first idea: greenDAO is clever, saw the picture node and saved it in DB.

Too bad is that, if I do inspect the database, I see the hotels but don't see any pictures record

Inspecting my SQLite DB, Hotels is full while HotelPictures is empty

So, HOW does GreenDAO access this data, if they're not saved in the database? I'm getting crazy because it seems to me I'm losing the whole point of having a DAO, which means to me Objects and Relations mapped to Java objects, not Java objects mapped some-to-database-and-some-wherever.

Could anybody please help me to understand where I'm failing?

Thank you

4

0 回答 0