如何搜索共享外键的两个表(我想我问这个正确的吗?)?
技术问答
194 人阅读
|
0 人回复
|
2023-09-12
|
Dog entity3 s! p6 A$ H) C# u6 {) I
@Entity(tableName = "dog_table")public class DogEntity { private int mId; private String mName,mBreed; etc..}Toy entity1 N- Z! M* C( {
@Entity(tableName = "toy_table")public class ToyEntity { private int mId; private String mName,mBrand; etc..}DogAndToy join table entity. I U+ `+ F1 \. W8 z
@Entity(tableName = "dog_and_toy_join_table", primaryKeys = {"mDogID","mToyId"}, foreignKeys = @ForeignKey( entity = DogEntity.class, parentColumns = "mId", childColumns = "mDogID", onDelete = ForeignKey.CASCADE, onUpdate = ForeignKey.CASCADE @ForeignKey( entity = ToyEntity.class, parentColumns = "mId", childColumns = "mToyId", onDelete = ForeignKey.CASCADE, onUpdate = ForeignKey.CASCADE indices = {@Index("mDogID"),@Index("mToyId")})public class DogAndToyJoinEntity{ private final int mDogID,mToyId; public DogAndToyJoinEntity(int mDogID,int mToyId) this.mDogID = mDogID; this.mToyId = mToyId; } etc..}DogAndToy数据类- M# y% r# w- v# w
public class DogAndToy { @Embedded public Dog mDog; @Relation( parentColumn = "mId", entityColumn = "mId", entity = ToyEntity.class, associateBy = @Junction( value = DogAndToyJoinEntity.class, parentColumn = "mDogId", entityColumn = "mToyId" public List toyList;}注:所有的狗都可以有多个玩具,玩具可以与多个狗有关。狗和玩具实体不共享任何字段(例如-狗没有toyId等)
' p& C, {$ i, O0 t7 e/ Y* p我已经试着缠住我的头好几天了
: }! K+ s) J: t( g" Q" J6 u7 D- n如何查询/获取所有与玩具相关的狗(按名称)$ e! i. t5 k" ]+ m( }
我将DogAndToy数据类用于RecyclerView显示中的目的。
6 q# P# Z- [; mJOIN和INNER JOIN查询让我很困惑。我一直在尝试各种变体,但最终以零搜索结果告终。这是我最近的尝试:- `6 |; C. `2 O% ]7 q: Z! x
@Transaction @Query("SELECT dog_table.* FROM dog_table" "INNER JOIN dog_and_toy_join_table ON dog_table.mId = dog_and_toy_join_table.mDogId" "INNER JOIN toy_table ON toy_table.mId = dog_and_toy_join_table.mToyId " "WHERE toy_table.mName LIKE :query") LiveData> findDogsByToyName(String query);谁能在Android Room建议这些查询分步说明?我在这里或互联网上找到的任何地方JOIN没有文章/示例7 ]' J, J' ^ h8 B V3 j% C% X
join参考(外键)…我以正确的方式尝试过吗?
% J! m" W0 N) L1 u" I6 C更新 : 澄清一下,我有FTS表,我的基本搜索工作正常(比如-按名称搜索,等等)
s) ?% N1 K9 Z. f- b6 p
1 Z! Z) \$ y* Q0 L" f( W2 W 解决方案: |
|
|
|
|
|