james04
james04

Reputation: 1920

Create a Pojo that has a Pojo inside?

So i am developing a chat application. There is a part in which i am interested on retrieving 3 entities from the Database but in once. There is a serverDto entity and accountDto entity. Every server has many accounts and there is a field in accounts called active, and if active = 1 means that is my account. I have created a POJO named ServerAccount like below

class ServerAccount {
    @Embedded
    var accountDto: AccountDto? = null

    // Server and Account have same column "serverId"
    @Relation(parentColumn = "serverId", entityColumn = "serverId")
    var rUserDto: RUserDto? = null
}

So since i have that i want every time i open the chatRoomActivity to have the Contact and the ServerAccount at once, but i want them seperated entities and not a big object with many fields. What i mean is something like below

class ContactServerAccount {
    @Embedded
    var contact: AccountDto? = null

    @Embedded
    var account: AccountDto? = null

    @Embedded
    var server: ServerDto? = null


////////////////////////////////////
    // OR something like that //
////////////////////////////////////


    @Embedded
    var serverAccount: ServerAccount = null

    @Relation
    var contact: AccountDto = null

}

How can i do that with given a serverId which gives me the serverAccount and the contactId which gives me the contact..?

Upvotes: 1

Views: 126

Answers (1)

MikeT
MikeT

Reputation: 57033

If I understand correctly you could use three methods, full embedding (all three tables embedded), hierarchial embedding (contact + ServerAccount) or limited Embedding (without @Relates)

I believe that the 3rd meets your minimalist/reduced data so something similar to :-

class ContactServerAccountLimited {

    var serverId: Long = 0 /* or Long? = null */
    var accountId: Long = 0

    @Embedded
    var contact: Contact = Contact()
}

used along with :-

@Query("SELECT serverId,accountId,contact.* FROM server JOIN account ON accountServerId = serverId JOIN contact ON contactServerId = serverId WHERE active = :active")
fun getContactServerAccountLimited(active :Boolean) :List<ContactServerAccountLimited>

Example

The Entities used :-

@Entity(tableName = "server")
class Server {

    @PrimaryKey
    var serverId: Long? = null;
    var servername: String? = null;

    constructor()

    @Ignore
    constructor(name: String) {
        serverId = null
        servername = name
    }
}

@Entity(
    tableName = "account",
    foreignKeys = [
        ForeignKey(
            entity = Server::class,
            parentColumns = ["serverId"],
            childColumns = ["accountServerId"])
    ]
)
class Account {

    @PrimaryKey
    var accountId: Long? = null

    var accountServerId: Long? = null
    var accountName: String? = null
    var active: Boolean = false

    constructor()

    @Ignore
    constructor(accountName: String, accountServerId: Long) {
        this.accountName = accountName
        this.accountServerId = accountServerId
        this.active = false
    }
}

@Entity(
    tableName = "contact",
    foreignKeys = [
        ForeignKey(
            entity = Server::class,
            parentColumns = ["serverId"],
            childColumns = ["contactServerId"]
        )
    ]
)
class Contact {

    @PrimaryKey
    var contactId: Long? = null
    var contactServerId: Long? = null
    var contactName: String? = null

    constructor()

    @Ignore
    constructor(name: String, serverId: Long) {
        contactId = null
        contactName = name
        contactServerId = serverId
    }
}

The POJO's :-

class ContactServerAccount  {

    @Embedded
    var serverAccount: ServerAccount? = ServerAccount()
    @Relation(entity = Contact::class,parentColumn = "serverId",entityColumn = "contactServerId")
    var contact: Contact = Contact()
}

class ContactServerAccountLimited {

    var serverId: Long = 0
    var accountId: Long = 0

    @Embedded
    var contact: Contact = Contact()
}

The Dao :-

@Dao
interface AllDao {

    @Insert
    fun insertServer(server: Server): Long

    @Insert
    fun insertAccount(account: Account): Long

    @Insert
    fun insertContact(contact: Contact): Long

    @Query("UPDATE account SET active = NOT active WHERE accountId = :accountId")
    fun toggleAccountActiveStatus(accountId: Long)

    @Query("SELECT * FROM server")
    fun getAllServers() :List<Server>

    @Query("SELECT * FROM account")
    fun getAllAccounts() :List<Account>

    @Query("SELECT * FROM contact")
    fun getAllContacts() :List<Contact>

    @Query("SELECT * FROM server JOIN account ON accountServerId = serverId JOIN contact ON contactServerId = serverId WHERE active = :active")
    fun getContactServerAccount(active: Boolean) :List<ContactServerAccount>

    @Query("SELECT serverId,accountId,contact.* FROM server JOIN account ON accountServerId = serverId JOIN contact ON contactServerId = serverId WHERE active = :active")
    fun getContactServerAccountLimited(active :Boolean) :List<ContactServerAccountLimited>

    @Query("DELETE FROM server")
    fun deleteAllServers() :Int

    @Query("DELETE FROM account")
    fun deleteAllAccounts() :Int
    @Query("DELETE FROM contact")
    fun deleteAllContacts() :Int
}

and :-

    val allDao = database.allDao();
    allDao.deleteAllContacts()
    allDao.deleteAllAccounts()
    allDao.deleteAllServers()
    currentServer = allDao.insertServer(Server("Server 1"))
    allDao.insertAccount(Account("FRED",currentServer))
    allDao.insertAccount(Account("MARY",currentServer))
    allDao.insertContact(Contact("C1",currentServer))
    currentServer = allDao.insertServer(Server("Server 2"))
    allDao.insertAccount(Account("JANE",currentServer))
    allDao.insertAccount(Account("ANNE",currentServer))
    allDao.insertContact(Contact("C2",currentServer))
    currentServer = allDao.insertServer(Server("Server 3"))
    allDao.insertAccount(Account("VERA",currentServer))
    allDao.insertAccount(Account("JOHN",currentServer))
    allDao.insertContact(Contact("C3",currentServer))
    allDao.insertContact(Contact("C4",currentServer))
    allDao.toggleAccountActiveStatus(1)
    var serverList: List<Server> = allDao.getAllServers()
    for (s: Server in serverList) {
        Log.d("SERVERINFO","Server ID is " + s.serverId + " Name is " + s.servername)
    }

    var accountList = allDao.getAllAccounts()
    for (a: Account in accountList) {
        Log.d("ACCOUNTINFO","Account ID is " + a.accountId +
                " Account Name is " + a.accountName +
                " Account Server ID is " + a.accountServerId +
                " active is " + a.active)
    }

    var contactList = allDao.getAllContacts()
    for (c: Contact in contactList) {
        Log.d("CONTACTINFO","Contact ID is " + c.contactId + " Contact Name is " + c.contactName + " Contact Server ID is " + c.contactServerId)
    }
    val csaList = allDao.getContactServerAccount(true)
    for (c: ContactServerAccount in csaList) {
        Log.d(
            "CSAINFO",
            "SERVER ID is " + c.serverAccount!!.server!!.serverId +
                    " ACCOUNT ID is " + c.serverAccount!!.account!!.accountId +
                    " CONTACT ID is " + c.contact.contactId +
                " CONTACT NAME is " + c.contact.contactName
        )
    }
    val csalList = allDao.getContactServerAccountLimited(true)
    for (c: ContactServerAccountLimited in csalList) {
        Log.d(
            "CSALINFO",
            "SERVER ID is " + c.serverId +
                    " ACCOUNT ID is " + c.accountId +
                    " CONTACT ID is " + c.contact.contactId +
                    " CONTACT NAME is " + c.contact.contactName
        )
    }
}

Then the output would be :-

2019-10-29 20:56:59.942 D/SERVERINFO: Server ID is 1 Name is Server 1
2019-10-29 20:56:59.943 D/SERVERINFO: Server ID is 2 Name is Server 2
2019-10-29 20:56:59.943 D/SERVERINFO: Server ID is 3 Name is Server 3
2019-10-29 20:56:59.945 D/ACCOUNTINFO: Account ID is 1 Account Name is FRED Account Server ID is 1 active is true
2019-10-29 20:56:59.945 D/ACCOUNTINFO: Account ID is 2 Account Name is MARY Account Server ID is 1 active is false
2019-10-29 20:56:59.945 D/ACCOUNTINFO: Account ID is 3 Account Name is JANE Account Server ID is 2 active is false
2019-10-29 20:56:59.945 D/ACCOUNTINFO: Account ID is 4 Account Name is ANNE Account Server ID is 2 active is false
2019-10-29 20:56:59.945 D/ACCOUNTINFO: Account ID is 5 Account Name is VERA Account Server ID is 3 active is false
2019-10-29 20:56:59.945 D/ACCOUNTINFO: Account ID is 6 Account Name is JOHN Account Server ID is 3 active is false
2019-10-29 20:56:59.947 D/CONTACTINFO: Contact ID is 1 Contact Name is C1 Contact Server ID is 1
2019-10-29 20:56:59.947 D/CONTACTINFO: Contact ID is 2 Contact Name is C2 Contact Server ID is 2
2019-10-29 20:56:59.947 D/CONTACTINFO: Contact ID is 3 Contact Name is C3 Contact Server ID is 3
2019-10-29 20:56:59.947 D/CONTACTINFO: Contact ID is 4 Contact Name is C4 Contact Server ID is 3


2019-10-29 20:56:59.953 D/CSAINFO: SERVER ID is 1 ACCOUNT ID is 2 CONTACT ID is 1 CONTACT NAME is C1
2019-10-29 20:56:59.954 D/CSALINFO: SERVER ID is 1 ACCOUNT ID is 1 CONTACT ID is 1 CONTACT NAME is C1
  • You may notice that to use the ContactServerAccount POJO used 6ms whilst the ContactServerAccountLimited used 1ms, this could be partly due to the @Relates running queries to build the related objects, whilst with the latter the contact object is built from the core query.

  • Additionally the ContractServerAccount has returned account id 2 not 1, this is because account is should really be a list of accounts, which would require a more complex query or subsequent processing to get the active account.

Upvotes: 2

Related Questions