我遵循了 Android 中的相同教程,并在我的 Room 表中创建了多对多关系。但是我得到了一个奇怪的行为,它一直工作到第一步,但没有为关系工作。
大多数情况下,我创建了一些表并尝试将数据获取到两个级别。例如 Accounts-List-List 。
我的表格如下:
@Dao
public abstract class AccountDAO {
/*Get accounts*/
@Transaction
@Query("SELECT * FROM AccountModel")
public abstract LiveData<DataWithAccounts> getAccountList();
/*Get the account with respected channels*/
@Transaction
@Query("SELECT * FROM AccountEntity")
public abstract LiveData<AccountWithChannels> getAccountWithChannels();
/*Get the account with respected channels and vpubs*/
@Transaction
@Query("SELECT * FROM AccountEntity")
public abstract LiveData<AccountWithChannelsAndVpubs> getAccountWithChannelsAndVpubs();
@Transaction
@Query("SELECT * FROM AccountEntity WHERE accId = :id")
public abstract AccountWithChannelsAndVpubs loadData(long id);
/*
*Insert the object in database
* @param account list, object to be inserted
*/
@Insert(onConflict = OnConflictStrategy.REPLACE)
public abstract void insertAccountModel(AccountModel accountModel);
@Insert(onConflict = OnConflictStrategy.REPLACE)
public abstract void insertAccountList(List<AccountEntity> data);
/*
* update the object in database
* @param account, object to be updated
*/
@Update
public abstract void updateAccountList(AccountModel repos);
/*
* delete the object from database
* @param account, object to be deleted
*/
@Delete
public abstract void deleteAccountList(AccountModel note);
}
AccountWithChannels:
public class AccountWithChannels {
@Embedded public AccountEntity accounts;
@Relation(
parentColumn = "accId",
entityColumn = "channelId",
associateBy = @Junction(AccountChannelCrossRef.class)
)
public List<ChannelEntity> channels;
public AccountEntity getAccounts() {
return accounts;
}
public void setAccounts(AccountEntity accounts) {
this.accounts = accounts;
}
public List<ChannelEntity> getChannels() {
return channels;
}
public void setChannels(List<ChannelEntity> channels) {
this.channels = channels;
}
}
ChannelWithVpubs:
public class ChannelWithVpubs {
@Embedded
public ChannelEntity channel;
@Relation(
parentColumn = "channelId",
entityColumn = "vPubId",
associateBy = @Junction(ChannelVpubCrossRef.class)
)
public List<VPubEntity> vPubs;
public ChannelEntity getChannel() {
return channel;
}
public void setChannel(ChannelEntity channel) {
this.channel = channel;
}
public List<VPubEntity> getvPubs() {
return vPubs;
}
public void setvPubs(List<VPubEntity> vPubs) {
this.vPubs = vPubs;
}
}
AccountWithChannelsAndVpubs:
public class AccountWithChannelsAndVpubs {
@Embedded
public AccountEntity account;
@Relation(
entity = ChannelEntity.class,
parentColumn = "accId",
entityColumn = "accountId"
)
public List<ChannelWithVpubs> channelWithVpubs;
public AccountEntity getAccount() {
return account;
}
public void setAccount(AccountEntity account) {
this.account = account;
}
public List<ChannelWithVpubs> getChannelWithVpubs() {
return channelWithVpubs;
}
public void setChannelWithVpubs(List<ChannelWithVpubs> channelWithVpubs) {
this.channelWithVpubs = channelWithVpubs;
}
}
ChannelVpubCrossRef:
@Entity(primaryKeys = {"channelId", "vPubId"})
public class ChannelVpubCrossRef {
public int channelId;
public int vPubId;
}
在创建具有交叉引用的关系后,我调用了以下方法来获取包含 Account、List 和 List 的所有数据。
private List<ChannelWithvPub> getAllVpubs(AccountEntity mAccountEntity){
List<ChannelWithvPub> mData = new ArrayList<>();
AccountWithChannelsAndVpubs accWithChnlNvpubs = database.accountDao().loadData(mAccountEntity.getAccId());
List<ChannelWithVpubs> mdata = accWithChnlNvpubs.getChannelWithVpubs();
for(int i=0; i<mdata.size(); i++){
ChannelWithVpubs mChannelWithVpubs = mdata.get(i);
for(int j=0; j<mChannelWithVpubs.getvPubs().size(); j++){
VPubEntity mVpub = mChannelWithVpubs.getvPubs().get(j);
ChannelWithvPub newData = new ChannelWithvPub(mAccountEntity.getAccId(),
mChannelWithVpubs.getChannel().getChannelId(), mVpub);
mData.add(newData);
}
}
return mData;
}
但奇怪的是我得到了帐户和频道列表。但 Vpub 列表始终返回为 0。
我也尝试过外国人,但没有帮助。我确定我在这里做错了什么,我无法检测到。但是数据正确插入,包括 Vpub 在内的所有表也都有数据。