Создать Pojo, в котором есть Pojo? - PullRequest
1 голос
/ 28 октября 2019

Итак, я разрабатываю приложение для чата. Есть часть, в которой я заинтересован в извлечении 3 сущностей из базы данных, но один раз. Существует сущность serverDto и сущность accountDto. Каждый сервер имеет много учетных записей, и в учетных записях есть поле, называемое активным, и если активен = 1, это означает, что это моя учетная запись. Я создал POJO с именем ServerAccount, как показано ниже

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

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

Так что, поскольку у меня есть это, я хочу каждый раз, когда я открываю chatRoomActivity, иметь контакт и ServerAccount одновременно, но я хочу, чтобы они были отдельными сущностями, а небольшой объект со многими полями. Я имею в виду что-то вроде ниже

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

}

Как я могу сделать это с заданным serverId, который дает мне serverAccount и contactId, который дает мне контакт ..?

1 Ответ

1 голос
/ 29 октября 2019

Если я правильно понимаю, вы можете использовать три метода: полное встраивание (все три таблицы включены), иерархическое встраивание (contact + ServerAccount) или ограниченное вложение (без @Relates)

Я считаю, что 3-й вариант соответствует вашемуминималистичные / уменьшенные данные, что-то похожее на: -

class ContactServerAccountLimited {

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

    @Embedded
    var contact: Contact = Contact()
}

используется вместе с: -

@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>

Пример

Используемые объекты: -

@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
    }
}

POJO: -

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()
}

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
}

и: -

    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
        )
    }
}

Тогда вывод будет: -

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
  • Вы можете заметить, что для использования ContactServerAccount POJO использовал 6 мс, в то время как ContactServerAccountLimited использовал 1 мс, это может быть частично связано с выполнением запросов @Relates для создания связанных объектов. в то время как с последним контактный объект создается из основного запроса.

  • Кроме того, ContractServerAccount вернул идентификатор учетной записи 2, а не 1, это потому, что учетная запись действительно должна быть списком учетных записейпочемуДля получения активной учетной записи ch потребуется более сложный запрос или последующая обработка.

...