I followed the same tutorial from Android and created a many to many relation in my Room tables. But I am getting a strange behavior which is working till the first step, but not working for the relations.
Mostly I created some tables and trying to get data till two levels. for example Accounts - List - List .
My tables are like below:
@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;
}
After creating the relation with cross reference I called below method to get all data with Account, List and 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;
}
But the strange thing is I am getting Account and also List of Channels. But the Vpub list is always returned as 0.
I tried with foreign also but not helped. I am sure I am doing something wrong here, which I am not able to detect. But the data is properly inserting and all tables including Vpub has data as well.