0

我的应用程序中有一个数据库,其中包含 3 个表服务器、站点、组

@Entity(tableName = "servers")
data class Server(

    @ColumnInfo(name = "server_id")
    var serverId: Long,

    @ColumnInfo(name = "account_id")
    var accountId: Int,

    @ColumnInfo(name = "device_id")
    var deviceId: Int,

    @ColumnInfo(name = "company_id")
    var companyId: Int
   
    .......


@Entity(tableName = "sites")
data class Site(

    @ColumnInfo(name = "server_id")
    var serverId: Long,

    @ColumnInfo(name = "site_id")
    val siteId: Int,

    @ColumnInfo(name = "description", defaultValue = "")
    val description: String

)



@Entity(tableName = "groups")
data class Group(

    @ColumnInfo(name = "server_id")
    val serverId: Long,

    @ColumnInfo(name = "group_id")
    var groupId: Int,

    @ColumnInfo(name = "site_id")
    val siteId: Int,

    @ColumnInfo(name = "description", defaultValue = "")
    val description: String

    ......

因此,我们可以看到每个服务器都有站点,每个站点都有几个组。现在我制作了一个名为 ServerSiteWithGroup 的 POJO,其中包含一个服务器和另一个 POJO SiteWithGroup

data class ServerSiteWithGroup(
    @Embedded
    val server: Server,

    @Relation(parentColumn = "server_id", entityColumn = "server_id")
    val siteWithGroup: SiteWithGroup
)


data class SiteWithGroup(
    @Embedded
    val group: Group,

    @Relation(parentColumn = "site_id", entityColumn = "site_id")
    val site: Site
)

因此,鉴于我想对房间数据库进行一次查询并获取服务器、站点、组对象,给定serverId, siteId, groupId

我已经尝试过了,但它不起作用

    @Transaction
    @Query("Select * from groups 
        inner join servers on groups.server_id = servers.server_id 
        where groups.server_id = :serverId 
        and groups.site_id = :siteId 
        and groups.group_id = :groupId")
    fun getSiteWithGroup(serverId: Long, siteId: Int, groupId: Int): LiveData<ServerSiteWithGroup>

我该如何解决这个问题?

4

1 回答 1

1

首先,您似乎没有使用 @PrimaryKey 注释定义任何 PRIMARY 键,这将导致编译错误。

其次,@Query 不应该返回一个LiveData<ServerSiteWithGroup>但应该返回一个 LiveData 的数组,它们本身是(我相信)ServerSiteWithGroup 的数组,所以我认为应该是LiveData<List<ServerSiteWithGroup>>

  • Perahps 尝试这个 First但考虑在答案末尾添加的更多部分。.

我该如何解决这个问题?

  • 您没有提及/指定实际问题是什么。但是,以下是基于您提供的信息的解决方案。(但为了方便不使用LiveData

在您拥有 ServerId 的 Group 实体中,这是不必要的,因为作为 Group 父级的站点将 Server 作为其父级。这不是错误,但没有必要。

虽然理论上不需要,但id可以是 Long,因此我总是建议使用 Long 而不是 Int 来表示关系。

示例 这是一个基于您的代码的工作示例。

服务器

@Entity(tableName = "servers")
data class Server(

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "server_id")
    var serverId: Long?,

    @ColumnInfo(name = "account_id")
    var accountId: Int,

    @ColumnInfo(name = "device_id")
    var deviceId: Int,

    @ColumnInfo(name = "company_id")
    var companyId: Int
)
  • Primarykey已定义。
  • autogenerateLong?允许生成 id。

地点

@Entity(tableName = "sites")
data class Site(

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "site_id")
    val siteId: Long?,

    @ColumnInfo(name = "server_id")
    var serverId: Long,

    @ColumnInfo(name = "description", defaultValue = "")
    val description: String
)

团体

@Entity(tableName = "groups")
data class Group(

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "group_id")
    var groupId: Long?,

    @ColumnInfo(name = "server_id") //?
    val serverId: Long,

    @ColumnInfo(name = "site_id")
    val siteId: Long,

    @ColumnInfo(name = "description", defaultValue = "")
    val description: String
)

Alldao Dao 允许在各个级别进行插入和提取,尤其是使用@relationand JOIN

interface AllDao {
    @Insert
    fun insertServer(server: Server): Long
    @Insert
    fun insertSite(site: Site): Long
    @Insert
    fun insertGroup(group: Group): Long
    @Query("SELECT * FROM servers")
    fun getAllServers(): List<Server>
    @Query("SELECT * FROM sites")
    fun getAllSites(): List<Site>
    @Query("SELECT * FROM groups")
    fun getAllGroups(): List<Group>
    @Transaction
    @Query("SELECT * FROM sites")
    fun getAllSitesWithGroups(): List<SiteWithGroup>
    @Transaction
    @Query("SELECT * FROM servers")
    fun getAllServersWithSitesWithGroups(): List<ServerWithSiteWithGroup>
    @Transaction
    @Query("SELECT * FROM servers INNER JOIN sites ON servers.server_id = sites.server_id INNER JOIN groups on sites.site_id = groups.site_id")
    fun getAllServersWithSitesWithGroupsUsingJoin(): List<ServerWithSiteWithGroup>

    @Transaction
    @Query("Select * from groups " +
            "inner join servers on groups.server_id = servers.server_id " +
            "where groups.server_id = :serverId " +
            "and groups.site_id = :siteId " +
            "and groups.group_id = :groupId")
    fun getSiteWithGroup(serverId: Long, siteId: Int, groupId: Int): List<ServerWithSiteWithGroup>
}
  • 请注意原始查询之前的最后两个@Query类似但有细微差别(请参阅结果
  • 请注意,您查询仅返回 List 而不是 LiveData<List>

SiteWithGroup组与其父站点的 POJO 关系(一个站点可以有多个组)

data class SiteWithGroup(
    @Embedded
    var site: Site,

    @Relation(entity = Group::class, entityColumn = "site_id", parentColumn = "site_id")
    var groups: List<Group>
)
  • 虽然在这种情况下没有必要,但我更喜欢对实体(类)进行编码

ServerWithSiteWithGroup

data class ServerWithSiteWithGroup(
    @Embedded
    var server: Server,

    @Relation(entity = Site::class, entityColumn = "server_id",parentColumn = "server_id")
    var siteWithGroup: List<SiteWithGroup>
)
  • 请注意,我使用了与原始名称不同的名称,因为我相信它更能描述底层 POJO。

MyDatabse将实体和 Dao(s) 联系起来的 @Database 抽象类(为了方便,只有一个)

@Database(entities = arrayOf(Server::class,Site::class,Group::class),version = 1)
abstract class MyDatabase: RoomDatabase() {
    abstract fun getAllDao(): AllDao
}

用于演示/bervity/convenience 的MainActivity在主线程上运行

class MainActivity : AppCompatActivity() {
    val TAG = "MYDBINFO"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val db = Room.databaseBuilder(applicationContext,MyDatabase::class.java,"MyDatabase")
            .allowMainThreadQueries()
            .build()
        val allDao = db.getAllDao()
        val s1 = Server(null,1,2,3)
        val s2 = Server(null,4,5,6)
        allDao.insertServer(s1)
        allDao.insertServer(s2)
        val site1 = Site(null,1,"Site1 - Server1")
        val site2 = Site(null,1,"Site2 - Server1")
        val site3 = Site(null,2,"Site3 - Server2")
        val site4 = Site(null,2,"Site4 - Server2")
        val site5 = Site(null,1,"Site5 - Server 1")
        allDao.insertSite(site1)
        allDao.insertSite(site2)
        allDao.insertSite(site3)
        allDao.insertSite(site4)
        allDao.insertSite(site5)
        val g1 = Group(null,1,1,"Group1 - Site1 (impicitly Server 1)")
        val g2 = Group(null,10,1,"Group2!!! - Site1 (impicitly Server 1)") // OOOPS no server with ID 10
        val g2_2 = Group(null,1,1,"Group2_2 - Site1 (impicitly Server 1)")
        val g3 = Group(null,2,2,"Group3 - Site2 (impicitly Server 1)")
        allDao.insertGroup(g1)
        allDao.insertGroup(g2)
        allDao.insertGroup(g2_2)
        allDao.insertGroup(g3)
        val servers = allDao.getAllServers()
        Log.d(TAG,"Server List")
        for (s: Server in servers) {
            Log.d(TAG,"\tServerID = " + s.serverId + " CompanyID =" + s.companyId + " AccountID = " + s.accountId + " DeviceID = " + s.deviceId)
        }
        val sites = allDao.getAllSites()
        Log.d(TAG,"Site List")
        for (si: Site in sites) {
            Log.d(TAG,"\tSiteID = " + si.siteId + " Description = " + si.description + " Server ID = " + si.serverId)
        }
        val groups = allDao.getAllGroups()
        for(g: Group in groups) {
            Log.d(TAG,"\tGroup ID = " + g.groupId + " Description = " + g.description + " ServerID = " + g.serverId + " SiteID = " + g.siteId)
        }
        val sitesWithGroupsList = allDao.getAllSitesWithGroups()
        Log.d(TAG,"Site With Groups List")
        for(swgl: SiteWithGroup in sitesWithGroupsList) {
            Log.d(TAG,"\tSiteID = " + swgl.site.siteId + " ServerID = " + swgl.site.serverId + " Description " + swgl.site.description)
            for(grp: Group in swgl.groups) {
                Log.d(TAG,"\t\tGroup ID = " + grp.groupId + " SiteID = " + grp.siteId + " Description = " + grp.description + " ServerID = " + grp.serverId)
            }
        }
        Log.d(TAG,"****Servers with Sites with Groups using @Relation")
        val swswg = allDao.getAllServersWithSitesWithGroups()
        for(s: ServerWithSiteWithGroup in swswg) {
            Log.d(TAG,"ServerID is " + s.server.serverId)
            Log.d(TAG,"Sites in Server =" + s.siteWithGroup.size)
            for(swg: SiteWithGroup in s.siteWithGroup) {
                Log.d(TAG,"\tSite is " + swg.site.description)
                Log.d(TAG,"\tGroups in Site =" + swg.groups.size)
                for (g: Group in swg.groups) {
                    Log.d(TAG,"\t\tGroup is " + g.description)
                }
            }
        }
        Log.d(TAG,"****Servers with Sites with Groups using Joins")
        val swswg2 = allDao.getAllServersWithSitesWithGroupsUsingJoin()
        for(s: ServerWithSiteWithGroup in swswg2) {
            Log.d(TAG,"ServerID is " + s.server.serverId)
            Log.d(TAG,"Sites in Server =" + s.siteWithGroup.size)
            for(swg: SiteWithGroup in s.siteWithGroup) {
                Log.d(TAG,"\tSite is " + swg.site.description)
                Log.d(TAG,"\tGroups in Site =" + swg.groups.size)
                for (g: Group in swg.groups) {
                    Log.d(TAG,"\t\tGroup is " + g.description)
                }
            }
        }

        // ORIGINAL PROBLEM QUERY 
        Log.d(TAG,"****Servers With Sites With Groups using joins and where clauses (problem query)")
        val swswgsel = allDao.getSiteWithGroup(1,1,1)
        for (swswg3: ServerWithSiteWithGroup in swswgsel) {
            Log.d(TAG,"ServerID is " + swswg3.server.serverId)
            Log.d(TAG,"Sites in Server =" + swswg3.siteWithGroup.size)
            for(swg: SiteWithGroup in swswg3.siteWithGroup) {
                Log.d(TAG,"\tSite is " + swg.site.description)
                Log.d(TAG,"\tGroups in Site =" + swg.groups.size)
                for (g: Group in swg.groups) {
                    Log.d(TAG,"\t\tGroup is " + g.description)
                }
            }
        }
    }
}
  • 上面依次流过代码
  • 日志记录允许轻松监控结果。
  • 设计为运行一次(运行不止一次可能会导致一些混乱)
  • 为方便起见,使用了 LiveData。

结果最初运行时的日志

  • 您的原始查询位于日志末尾

:-

03-28 16:56:24.018 D/MYDBINFO: Server List
03-28 16:56:24.018 D/MYDBINFO: ServerID = 1 DeviceID = 2 AccountID = 1 CompanyID = 3
03-28 16:56:24.018 D/MYDBINFO: ServerID = 2 DeviceID = 5 AccountID = 4 CompanyID = 6

03-28 16:56:24.019 D/MYDBINFO: Site List
03-28 16:56:24.019 D/MYDBINFO:  SiteID = 1 Description = Site1 - Server1 ServerID = 1
03-28 16:56:24.019 D/MYDBINFO:  SiteID = 2 Description = Site2 - Server1 ServerID = 1
03-28 16:56:24.019 D/MYDBINFO:  SiteID = 3 Description = Site3 - Server2 ServerID = 2
03-28 16:56:24.019 D/MYDBINFO:  SiteID = 4 Description = Site4 - Server2 ServerID = 2
03-28 16:56:24.019 D/MYDBINFO:  SiteID = 5 Description = Site5 - Server 1 ServerID = 1

03-28 16:56:24.021 D/MYDBINFO: Group List
03-28 16:56:24.022 D/MYDBINFO:      GroupID = 1 Description = Group1 - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 1(May not be correct)
03-28 16:56:24.022 D/MYDBINFO:      GroupID = 2 Description = Group2!!! - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 10(May not be correct)
03-28 16:56:24.022 D/MYDBINFO:      GroupID = 3 Description = Group2_2 - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 1(May not be correct)
03-28 16:56:24.022 D/MYDBINFO:      GroupID = 4 Description = Group3 - Site2 (impicitly Server 1)Parent SiteID = 2 Parent ServerID = 2(May not be correct)

03-28 16:56:24.024 D/MYDBINFO: Site With Groups List
03-28 16:56:24.024 D/MYDBINFO:  SiteID = 1 Description = Site1 - Server1 ServerID = 1
03-28 16:56:24.024 D/MYDBINFO:      GroupID = 1 Description = Group1 - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 1(May not be correct)
03-28 16:56:24.024 D/MYDBINFO:      GroupID = 2 Description = Group2!!! - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 10(May not be correct)
03-28 16:56:24.024 D/MYDBINFO:      GroupID = 3 Description = Group2_2 - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 1(May not be correct)
03-28 16:56:24.024 D/MYDBINFO:  SiteID = 2 Description = Site2 - Server1 ServerID = 1
03-28 16:56:24.024 D/MYDBINFO:      GroupID = 4 Description = Group3 - Site2 (impicitly Server 1)Parent SiteID = 2 Parent ServerID = 2(May not be correct)
03-28 16:56:24.024 D/MYDBINFO:  SiteID = 3 Description = Site3 - Server2 ServerID = 2
03-28 16:56:24.024 D/MYDBINFO:  SiteID = 4 Description = Site4 - Server2 ServerID = 2
03-28 16:56:24.025 D/MYDBINFO:  SiteID = 5 Description = Site5 - Server 1 ServerID = 1

03-28 16:56:24.025 D/MYDBINFO: ****Servers with Sites with Groups using @Relation
03-28 16:56:24.027 D/MYDBINFO: ServerID = 1 DeviceID = 2 AccountID = 1 CompanyID = 3
03-28 16:56:24.028 D/MYDBINFO: Sites in Server =3
03-28 16:56:24.028 D/MYDBINFO:  SiteID = 1 Description = Site1 - Server1 ServerID = 1
03-28 16:56:24.028 D/MYDBINFO:  Groups in Site =3
03-28 16:56:24.028 D/MYDBINFO:      GroupID = 1 Description = Group1 - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 1(May not be correct)
03-28 16:56:24.028 D/MYDBINFO:      GroupID = 2 Description = Group2!!! - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 10(May not be correct)
03-28 16:56:24.028 D/MYDBINFO:      GroupID = 3 Description = Group2_2 - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 1(May not be correct)
03-28 16:56:24.028 D/MYDBINFO:  SiteID = 2 Description = Site2 - Server1 ServerID = 1
03-28 16:56:24.028 D/MYDBINFO:  Groups in Site =1
03-28 16:56:24.028 D/MYDBINFO:      GroupID = 4 Description = Group3 - Site2 (impicitly Server 1)Parent SiteID = 2 Parent ServerID = 2(May not be correct)
03-28 16:56:24.028 D/MYDBINFO:  SiteID = 5 Description = Site5 - Server 1 ServerID = 1
03-28 16:56:24.028 D/MYDBINFO:  Groups in Site =0
03-28 16:56:24.028 D/MYDBINFO: ServerID = 2 DeviceID = 5 AccountID = 4 CompanyID = 6
03-28 16:56:24.028 D/MYDBINFO: Sites in Server =2
03-28 16:56:24.028 D/MYDBINFO:  SiteID = 3 Description = Site3 - Server2 ServerID = 2
03-28 16:56:24.028 D/MYDBINFO:  Groups in Site =0
03-28 16:56:24.028 D/MYDBINFO:  SiteID = 4 Description = Site4 - Server2 ServerID = 2
03-28 16:56:24.028 D/MYDBINFO:  Groups in Site =0

03-28 16:56:24.028 D/MYDBINFO: ****Servers with Sites with Groups using Joins
03-28 16:56:24.029 D/MYDBINFO: ServerID = 1 DeviceID = 2 AccountID = 1 CompanyID = 3
03-28 16:56:24.029 D/MYDBINFO: Sites in Server =3
03-28 16:56:24.029 D/MYDBINFO:  SiteID = 1 Description = Site1 - Server1 ServerID = 1
03-28 16:56:24.029 D/MYDBINFO:  Groups in Site =3
03-28 16:56:24.029 D/MYDBINFO:      GroupID = 1 Description = Group1 - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 1(May not be correct)
03-28 16:56:24.029 D/MYDBINFO:      GroupID = 2 Description = Group2!!! - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 10(May not be correct)
03-28 16:56:24.029 D/MYDBINFO:      GroupID = 3 Description = Group2_2 - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 1(May not be correct)
03-28 16:56:24.029 D/MYDBINFO:  SiteID = 2 Description = Site2 - Server1 ServerID = 1
03-28 16:56:24.029 D/MYDBINFO:  Groups in Site =1
03-28 16:56:24.029 D/MYDBINFO:      GroupID = 4 Description = Group3 - Site2 (impicitly Server 1)Parent SiteID = 2 Parent ServerID = 2(May not be correct)
03-28 16:56:24.029 D/MYDBINFO:  SiteID = 5 Description = Site5 - Server 1 ServerID = 1
03-28 16:56:24.029 D/MYDBINFO:  Groups in Site =0
03-28 16:56:24.029 D/MYDBINFO: ServerID = 10 DeviceID = 2 AccountID = 1 CompanyID = 3
03-28 16:56:24.029 D/MYDBINFO: Sites in Server =0
03-28 16:56:24.029 D/MYDBINFO: ServerID = 1 DeviceID = 2 AccountID = 1 CompanyID = 3
03-28 16:56:24.029 D/MYDBINFO: Sites in Server =3
03-28 16:56:24.030 D/MYDBINFO:  SiteID = 1 Description = Site1 - Server1 ServerID = 1
03-28 16:56:24.030 D/MYDBINFO:  Groups in Site =3
03-28 16:56:24.030 D/MYDBINFO:      GroupID = 1 Description = Group1 - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 1(May not be correct)
03-28 16:56:24.030 D/MYDBINFO:      GroupID = 2 Description = Group2!!! - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 10(May not be correct)
03-28 16:56:24.030 D/MYDBINFO:      GroupID = 3 Description = Group2_2 - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 1(May not be correct)
03-28 16:56:24.030 D/MYDBINFO:  SiteID = 2 Description = Site2 - Server1 ServerID = 1
03-28 16:56:24.030 D/MYDBINFO:  Groups in Site =1
03-28 16:56:24.030 D/MYDBINFO:      GroupID = 4 Description = Group3 - Site2 (impicitly Server 1)Parent SiteID = 2 Parent ServerID = 2(May not be correct)
03-28 16:56:24.030 D/MYDBINFO:  SiteID = 5 Description = Site5 - Server 1 ServerID = 1
03-28 16:56:24.030 D/MYDBINFO:  Groups in Site =0
03-28 16:56:24.030 D/MYDBINFO: ServerID = 2 DeviceID = 2 AccountID = 1 CompanyID = 3
03-28 16:56:24.030 D/MYDBINFO: Sites in Server =2
03-28 16:56:24.030 D/MYDBINFO:  SiteID = 3 Description = Site3 - Server2 ServerID = 2
03-28 16:56:24.030 D/MYDBINFO:  Groups in Site =0
03-28 16:56:24.030 D/MYDBINFO:  SiteID = 4 Description = Site4 - Server2 ServerID = 2
03-28 16:56:24.030 D/MYDBINFO:  Groups in Site =0

03-28 16:56:24.030 D/MYDBINFO: ****Servers With Sites With Groups using joins and where claues (problem query)
03-28 16:56:24.031 D/MYDBINFO: ServerID = 1 DeviceID = 2 AccountID = 1 CompanyID = 3
03-28 16:56:24.031 D/MYDBINFO: Sites in Server =3
03-28 16:56:24.031 D/MYDBINFO:  SiteID = 1 Description = Site1 - Server1 ServerID = 1
03-28 16:56:24.031 D/MYDBINFO:  Groups in Site = 3
03-28 16:56:24.031 D/MYDBINFO:      GroupID = 1 Description = Group1 - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 1(May not be correct)
03-28 16:56:24.031 D/MYDBINFO:      GroupID = 2 Description = Group2!!! - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 10(May not be correct)
03-28 16:56:24.031 D/MYDBINFO:      GroupID = 3 Description = Group2_2 - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 1(May not be correct)
03-28 16:56:24.031 D/MYDBINFO:  SiteID = 2 Description = Site2 - Server1 ServerID = 1
03-28 16:56:24.032 D/MYDBINFO:  Groups in Site = 1
03-28 16:56:24.032 D/MYDBINFO:      GroupID = 4 Description = Group3 - Site2 (impicitly Server 1)Parent SiteID = 2 Parent ServerID = 2(May not be correct)
03-28 16:56:24.032 D/MYDBINFO:  SiteID = 5 Description = Site5 - Server 1 ServerID = 1
03-28 16:56:24.032 D/MYDBINFO:  Groups in Site = 0

额外 您可能希望考虑以下使用看似正确的关系:-

@Transaction
@Query("SELECT * FROM servers " +
        "JOIN sites ON sites.server_id = servers.server_id " +
        "JOIN groups ON groups.site_id = sites.site_id " +
        "WHERE servers.server_id = :serverId AND sites.site_id = :siteId AND groups.group_id = :groupId")
fun getSuggested(serverId: Long, siteId: Long, groupId: Long): List<ServerWithSiteWithGroup>

无论如何,这实际上对相同的测试用例实现了相同的结果:-

03-28 16:56:24.032 D/MYDBINFO: ????Servers With Sites With Groups and WHERE clause (suggested)
03-28 16:56:24.033 D/MYDBINFO: ServerID = 1 DeviceID = 2 AccountID = 1 CompanyID = 3
03-28 16:56:24.033 D/MYDBINFO: Sites in Server = 3
03-28 16:56:24.033 D/MYDBINFO:  SiteID = 1 Description = Site1 - Server1 ServerID = 1
03-28 16:56:24.033 D/MYDBINFO:  Groups in Site = 3
03-28 16:56:24.033 D/MYDBINFO:      GroupID = 1 Description = Group1 - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 1(May not be correct)
03-28 16:56:24.033 D/MYDBINFO:      GroupID = 2 Description = Group2!!! - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 10(May not be correct)
03-28 16:56:24.033 D/MYDBINFO:      GroupID = 3 Description = Group2_2 - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 1(May not be correct)
03-28 16:56:24.033 D/MYDBINFO:  SiteID = 2 Description = Site2 - Server1 ServerID = 1
03-28 16:56:24.033 D/MYDBINFO:  Groups in Site = 1
03-28 16:56:24.033 D/MYDBINFO:      GroupID = 4 Description = Group3 - Site2 (impicitly Server 1)Parent SiteID = 2 Parent ServerID = 2(May not be correct)
03-28 16:56:24.033 D/MYDBINFO:  SiteID = 5 Description = Site5 - Server 1 ServerID = 1
03-28 16:56:24.033 D/MYDBINFO:  Groups in Site = 0

在指定所有三个参数时,您可能并不期待结果,而是期待单个服务器/站点/组。如果是这样,那么我相信使用@Relation不是要走的路。相反,没有关系的 POJO 是要走的路。

例如,考虑后面查询中的 SQL:-

SELECT *,sites.description AS site_description, groups.description AS group_description FROM groups 
    INNER JOIN sites ON sites.site_id = groups.site_id 
    INNER JOIN servers ON servers.server_id = sites.server_id
    WHERE servers.server_id = 1 AND sites.site_id = 1 AND groups.group_id = 1;

使用上述数据(注意sites.description AS site_description, groups.description AS group_description是从不同的表中消除/区分相同的列名)。

您可能会期望(当 args 为 1,1,1 时):-

在此处输入图像描述

正如上面的日志,这不是 Room 提供的。

但是考虑 POJO:-

class AltServerSiteGroup {
    var server_id: Long = 0
    var device_id: Long = 0
    var account_id: Long = 0
    var company_id: Long = 0
    var site_id: Long = -1
    var site_description: String = ""
    var group_id: Long = -1
    var group_description: String = ""
}
  • 注意我改为使用 Long 而不是 Int (Server也)。

然后考虑道:-

@Transaction
@Query("SELECT *, sites.description AS site_description, groups.description AS group_description FROM groups " +
        "INNER JOIN sites ON sites.site_id = groups.site_id " +
        "INNER JOIN servers ON servers.server_id = sites.server_id " +
        "WHERE servers.server_id = :serverId AND sites.site_id = :siteId AND groups.group_id = :groupId")
fun getAlt(serverId: Long, siteId: Long, groupId: Long): List<AltServerSiteGroup>
  • 如果只能返回 1 个值,则可以使用JustAltServerSiteGroup而不是(如果 ID 是主键,则会出现这种情况)。List<AltServerSiteGroup>

因此,通过将以下内容添加到 MainActivity :-

   Log.d(TAG, "Alternative Perhaps what is wanted")
    val alt = allDao.getAlt(1,1,1)
    for(s: AltServerSiteGroup in alt) {
        logserver(Server(s.server_id,s.account_id,s.device_id,s.company_id))
        logsite(Site(s.site_id,s.server_id,s.site_description))
        loggroup(Group(s.group_id,s.server_id,s.site_id,s.group_description))
    }
  • 添加了记录实体的功能,因此 logserver(Server(....)) 等

然后该部分的结果将是:-

03-28 19:10:41.930 D/MYDBINFO: Alternative Perhaps what is wanted
03-28 19:10:41.931 D/MYDBINFO: ServerID = 1 DeviceID = 2 AccountID = 1 CompanyID = 3
03-28 19:10:41.931 D/MYDBINFO:  SiteID = 1 Description = Site1 - Server1 ServerID = 1
03-28 19:10:41.931 D/MYDBINFO:      GroupID = 1 Description = Group1 - Site1 (impicitly Server 1)Parent SiteID = 1 Parent ServerID = 1(May not be correct)

即匹配选择标准的单个服务器/站点/组。

您可以向 AltServerSiteGroup 添加函数以返回提取的服务器/站点/组对象,但请注意,这些对象与完整/完整对象不同,因为它们只有单个服务器站点组,例如:-

class AltServerSiteGroup {
    var server_id: Long = 0
    var device_id: Long = 0
    var account_id: Long = 0
    var company_id: Long = 0
    var site_id: Long = -1
    var site_description: String = ""
    var group_id: Long = -1
    var group_description: String = ""

    fun getServer(): Server {
        return Server(server_id,account_id,device_id,company_id)
    }

    fun getSite(): Site {
        return Site(site_id,server_id,site_description)
    }

    fun getGroup(): Group {
        return Group(group_id,server_id,site_id,group_description)
    }
}
  • 解释

  • 简而言之,Room 将根据 构建完整的对象@Relation,从而添加额外的不需要的站点和组。

  • 如果您查看 Java(生成)中 Dao 的代码(使用项目窗口中的 Android 视图),请注意文件名以 _Impl 为后缀(因此AllDao,如上所述,在生成的 java 中它是AllDao_Impl,你会看到 room 做了什么,并且构建的getAlt代码比getSiteWithGroup.

于 2021-03-28T03:10:19.970 回答