创建一个内部有 Pojo 的 Pojo?
Create a Pojo that has a Pojo inside?
所以我正在开发一个聊天应用程序。有一部分我感兴趣的是一次从数据库中检索 3 个实体。有一个 serverDto 实体和 accountDto 实体。每个服务器都有很多账户,账户里有一个字段叫做active,如果active = 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 时立即拥有联系人和 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 给我联系人..?
如果我理解正确,您可以使用三种方法,完全嵌入(嵌入所有三个表)、分层嵌入(联系人 + ServerAccount)或有限嵌入(不带 @Relates)
我相信 3rd 符合您的 minimalist/reduced 数据,所以类似于 :-
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
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 已返回帐户 ID 2 而不是 1,这是因为帐户实际上应该是一个帐户列表,这将需要更复杂的查询或后续处理才能获得活动帐户。
所以我正在开发一个聊天应用程序。有一部分我感兴趣的是一次从数据库中检索 3 个实体。有一个 serverDto 实体和 accountDto 实体。每个服务器都有很多账户,账户里有一个字段叫做active,如果active = 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 时立即拥有联系人和 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 给我联系人..?
如果我理解正确,您可以使用三种方法,完全嵌入(嵌入所有三个表)、分层嵌入(联系人 + ServerAccount)或有限嵌入(不带 @Relates)
我相信 3rd 符合您的 minimalist/reduced 数据,所以类似于 :-
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
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 已返回帐户 ID 2 而不是 1,这是因为帐户实际上应该是一个帐户列表,这将需要更复杂的查询或后续处理才能获得活动帐户。