如何从多个实体中选择带有房间数据库的新对象?
我有这个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
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
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用 @embedded 注释,它将从喜欢的类的字段/变量中确定列和输出字段,
例如: -
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. :-