如何从多个实体中选择带有房间数据库的新对象?

发布于 2025-02-04 08:42:50 字数 1511 浏览 3 评论 0原文

我有这个tow pojos for RoomDB实体,

@Entity
public class Favorite {...}
@Entity
public class ProductCart {
    Integer productQuantity;
    ...
}

我需要应用此选择 statment,然后将其返回对象fairitiTeadapterData

SELECT favorite.*,
       (SELECT CASE count(productQuantity)
               WHEN 0 THEN 0
                      ELSE productQuantity
               END  FROM ProductCart 
        WHERE favorite.id = productId) AS cartQuantity
FROM Favorite favorite

sqlite Statment Test

public class FavoriteAdapterData {
    int cartQuantity = 0;
    Favorite favorite;
    ...
}

我尝试以下代码

@Query("SELECT  " +
            "      (    SELECT CASE count(productQuantity) " +
            "           WHEN 0  THEN 0 " +
            "                   ELSE productQuantity  " +
            "           END  " +
            "           FROM ProductCart " +
            "           WHERE productId = favorite.id" +
            "       ) AS cartQuantity" +
            "       ,favorite.* " +
            "FROM Favorite favorite")
    LiveData<List<FavoriteAdapterData>> getFavoriteProducts();

但它不起作用,Android Studio notfi我这个错误:

Cannot figure out how to read this field from a cursor. 
Favorite favorite; 
             ^

I Have this tow POJOs For RoomDB Entity

@Entity
public class Favorite {...}
@Entity
public class ProductCart {
    Integer productQuantity;
    ...
}

I need to apply this select statment and return it inside the object FavoriteAdapterData

SELECT favorite.*,
       (SELECT CASE count(productQuantity)
               WHEN 0 THEN 0
                      ELSE productQuantity
               END  FROM ProductCart 
        WHERE favorite.id = productId) AS cartQuantity
FROM Favorite favorite

Sqlite statment test

public class FavoriteAdapterData {
    int cartQuantity = 0;
    Favorite favorite;
    ...
}

I try the following code

@Query("SELECT  " +
            "      (    SELECT CASE count(productQuantity) " +
            "           WHEN 0  THEN 0 " +
            "                   ELSE productQuantity  " +
            "           END  " +
            "           FROM ProductCart " +
            "           WHERE productId = favorite.id" +
            "       ) AS cartQuantity" +
            "       ,favorite.* " +
            "FROM Favorite favorite")
    LiveData<List<FavoriteAdapterData>> getFavoriteProducts();

But It didn't work and Android Studio notfi me this error:

Cannot figure out how to read this field from a cursor. 
Favorite favorite; 
             ^

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十秒萌定你 2025-02-11 08:42:50

您需要使用 @embedded 注释,它将从喜欢的类的字段/变量中确定列和输出字段,

例如: -

public class FavoriteAdapterData {
    int cartQuantity = 0;
    @Embedded
    Favorite favorite;
    ...
}

You need to Embed the Favorite class within the FavoriteAdapterData class using the @Embedded annotation, then it will determine the columns and outputfields from the Favorite class's fields/variables

e.g. :-

public class FavoriteAdapterData {
    int cartQuantity = 0;
    @Embedded
    Favorite favorite;
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文