【发布时间】:2021-08-25 02:39:45
【问题描述】:
我遵循了来自 Android 的相同 tutorial 并在我的 Room 表中创建了多对多关系。但是我遇到了一个奇怪的行为,它一直有效到第一步,但不适用于关系。
主要是我创建了一些表并尝试获取数据直到两个级别。例如帐户 - 列表 - 列表。
我的表格如下:
@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;
}
在使用交叉引用创建关系后,我调用了以下方法来获取包含帐户、列表和列表的所有数据。
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 在内的所有表也有数据。
【问题讨论】:
标签: android android-room android-room-relation