Java GreenDao向我提供数据,我可以';在数据库中看不到
我正在开发一个酒店预订应用程序,我使用greenDao 我创建了一个酒店实体。这是MyGreenDaoGenerator中的条目:Java GreenDao向我提供数据,我可以';在数据库中看不到,java,android,android-sqlite,greendao,greendao-generator,Java,Android,Android Sqlite,Greendao,Greendao Generator,我正在开发一个酒店预订应用程序,我使用greenDao 我创建了一个酒店实体。这是MyGreenDaoGenerator中的条目: // Hotels private Entity createHotels() { Entity hotels = createEntity("Hotel", "hotels"); hotels.addStringProperty("hotel_id").notNull().primaryKey().unique(); hotels.a
// 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;
}
如您所见,没有“图片”节点。但我知道酒店有图片,所以我创建了一个图片实体并将其与酒店关联
// 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;
}
简单
在greenDAO生成的“Hotel.java”中,现在我得到了一个合适的属性:
protected List<HotelPicture> pictures;
现在,,
在自行生成的Hotel.java和HotelDao.java中,我看不到任何设置图片的方法。我只有这些:
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;
}
公共列表getPictures(){
如果(图片==null){
if(daoSession==null){
抛出新的DaoException(“实体与DAO上下文分离”);
}
HotelPictureDao targetDao=daoSession.getHotelPictureDao();
列出图片新建=目标岛。\查询酒店图片(酒店id);
已同步(此){
如果(图片==null){
图片=图片新建;
}
}
}
返回图片;
}
公共同步的无效图片(){
图片=空;
}
我所期望的是以下之一:
- GreenDAO足够聪明,可以看到我的Hotel对象中有一个图片节点,并将其保存在数据库中
- 绿刀不够聪明。图片无法保存在DB中,应手动保存
谢谢如果您将存储web服务信息的代码附加到数据库Hello Jofre中,将会有所帮助。非常简单:ArrayList listOfHotels=MainSearchFragment.kalimaResponse.getData().getAvailability()//这将获取酒店对象列表HCUtilsApp.Database.getDaoSession().getHotelDao().insertoreplaceintx(酒店列表)//HCUtilsApp仅用于检索正确的DaoSession,但insertOrReplaceInTx是GreenDAO的一个。是否存储图片对象?有了这个代码,你只存储了酒店的物品,失去了图片参考。是的,我知道。这就是为什么我想知道:为什么我要用daoSession.getHotelDao().queryRaw(“,hotel_协议A其中A.hotel_ID=T.hotel_ID和A.AGREEMENT_ID bla bla bla bla bla bla…这里没有提到HotelPictures”);在另一项活动中?因为我是这样做的,所以通过上述查询可以在酒店对象内部检索图片!我知道现在我不存储图片,但我看到我检索了它们。所以我问自己:在我明确地存储它们之前,我应该学习如何不经验证地存储它们,这可能就是我现在正在做的。但是它们存储在哪里?这是否发生在获取web服务信息和创建de picture对象的同一活动中?我不知道,但也许你可以在内存中检索图片对象,尽管它不在数据库中。
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;
}