使用 ORMLite 按列搜索编写查询

Writing a query with ORMLite search by column

我必须列 User 和 MyDream。我需要使用 firebase_id 显示 my_dream 的 OrmLite 创建查询。 这是我的 sql 查询

select my_dream_id, title_dream,descryption_dream,saveDataDream from MyDream,User where User.user_id = MyDream.user_user_id and User.firebaseId='56789'

我不知道 Java

中有写查询

首先,你需要定义你的类:

public class User {
    @DatabaseField(id = true)
    private Integer user_id;
    @DatabaseField
    private String email;
    @DatabaseField
    private Integer firebase_id;
    @DatabaseField
    private String fullname;
    @DatabaseField
    private String photo_url;
    @ForeignCollectionField
    private LazyForeignCollection<Dream, Integer> dreams;
}

public class Dream {
    @DatabaseField(id = true)
    private Integer my_dream_id;
    @DatabaseField
    private String description_dream;
    @DatabaseField
    private ?? saveDataDream;
    @DatabaseField
    private String titleDream;
    @DatabaseField(columnName = "user_user_id", foreign = true)
    private User user;
}

然后,按照我的要求here, you need to use Join查询结果

其解释相同,但这是您示例的代码

Dao<User, Integer> userDao = DaoManager.createDao(getConnectionSource(), User.class);
Dao<Dream, Integer> dreamDao = DaoManager.createDao(getConnectionSource(), Dream.class);

QueryBuilder<User, Integer> userQa= userDao.queryBuilder();
userQa.where().eq("firebaseId", 56789);
QueryBuilder<Dream, Integer> dreamQa = dreamDao.queryBuilder();

users = userQa.join(dreamQa).query();

然后您可以使用 iterator() 从用户的对象 users.get(i).getDreams() 访问梦想,如下所示:

CloseableIterator<Dream> itDream = users.get(id).getDreams().closeableIterator();
try {
    while (itDream.hasNext()) {
        Dream dream = itDream.next();
    }
} finally {
    try {
        itDream.close();
    } catch (IOException e) {
        Log.e(TAG, "FAIL", e);
    }
}