创建一个内部有Pojo的Pojo吗?

问题描述 投票:0回答:1

所以我正在开发聊天应用程序。我有一部分兴趣是要一次从数据库中检索3个实体。有一个serverDto实体和accountDto实体。每个服务器都有许多帐户,并且帐户中有一个名为“活动”的字段,如果活动= 1表示这是我的帐户。我已经创建了一个名为ServerAccount的POJO,如下所示

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

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

因此,由于我希望每次打开chatRoomActivity都具有一次Contact和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的serverId和给我联系人的contactId来做到这一点?

android database sqlite android-room entity-relationship-model
1个回答
1
投票

如果我理解正确,则可以使用三种方法:完全嵌入(嵌入所有三个表),分层嵌入(联系+ ServerAccount)或有限嵌入(不使用@Relates)

我相信3rd符合您的极简/简化数据,所以类似于:-

class ContactServerAccountLimited {

    var serverId: Long = 0
    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()
}

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
}

和:-

    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正在运行查询以构建相关对象,而后者使用的是从核心查询中构建联系人对象。
© www.soinside.com 2019 - 2024. All rights reserved.