创建一个内部有Pojo的Pojo?

时间:2019-10-28 20:32:16

标签: android database sqlite android-room entity-relationship-model

所以我正在开发一个聊天应用程序。我有一部分兴趣是要一次从数据库中检索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来做到这一点??

1 个答案:

答案 0 :(得分:1)

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

我相信3号符合您的极简/精简数据,因此类似于:-

let questions = [
    {
        question: "What's your name?",
        answer: null
    },
    {
        question: "How old are you?",
        answer: null
    }
];

for(let i = 0; i < questions.length; i++){
    // Send the question in the current channel
    message.channel.send(questions[i].question);
    // Await for the answer
    let answer = await message.awaitMessages(response => response.author.id !== myBotId, {
        max: 1,
        time: 6000,
        errors: ['time']
    }).catch(() => {
        // If the user doesn't answer to the question, save "The user didn't respond" as the answer
        questions[i].answer = "The user didn't respond";
    });
    // If the user provides a answer, save it
    questions[i].answer = answer.first().content;
}

// Display the result
console.log("First question: "+questions[0].question); // What's your name?
console.log("Answer: "+questions[0].answer); // The answer of the user, or "The user didn't respond" if he didn't respond

与:-

一起使用
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>

POJO的:-

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

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
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
        )
    }
}
  • 您可能会注意到,要使用ContactServerAccount POJO使用6毫秒,而使用ContactServerAccountLimited使用1毫秒,这可能部分是由于@Relates运行查询以构建相关对象,而后者使用的是Contact对象。核心查询。

  • 此外,ContractServerAccount返回的帐户ID 2不是1,这是因为account实际上应该是一个帐户列表,这将需要更复杂的查询或后续处理才能获得活动帐户。