简体   繁体   中英

android room databaseview can't select from table

I created a DatabaseView, to join two tables. When I am doing my query, I am not being able to select from the database view table. But it can be a return type. It's giving me cannot resolve symbol EmployeeWithRole. I'm using roomVersion = '2.1.0-alpha04

My entities:

@Entity(tableName = "EmployeeRole")
   data class EmployeeRole(
    @PrimaryKey
    val id: Id,
    @ColumnInfo(name = "role")
    val role: String,
    @ColumnInfo(name = "parentRole")
    val parentRole: Id?)


@Entity(tableName = "Employee",
    foreignKeys = [
        ForeignKey(entity = EmployeeRole::class,
                onDelete = ForeignKey.CASCADE,
                parentColumns = ["id"],
                childColumns = ["currentRoleId"]
        )])
 data class Employee(
    @PrimaryKey
    val id: Id,
    @ColumnInfo(name = "firstName")
    val firstName: String,
    @ColumnInfo(name = "lastName")
    val lastName: String,
    @ColumnInfo(name = "currentRoleId")
    var currentRoleId: Id,
    @ColumnInfo(name = "pictureUrl")
    var pictureUrl: String)

My Dao:

@Dao
Interface EmployeeWithRoleDao {
  @Query(" SELECT * FROM EmployeeWithRole ")
   fun getAllEmployees(): List<EmployeeWithRole>
}

My database view:

import androidx.room.DatabaseView
import androidx.room.Embedded

@DatabaseView("""
SELECT Employee.*, $employeeRoleParams FROM Employee
  INNER JOIN EmployeeRole ON Employee.currentRoleId = EmployeeRole.id
""")

 data class EmployeeWithRole(
    @Embedded
    val employee: Employee,
    @Embedded(prefix = employeeRoleP)
    val employeeRole: EmployeeRole
)

private const val employeeRoleP = "EmployeeRole"
private const val employeeRoleParams = """
$employeeRoleP.id as ${employeeRoleP}_id,
$employeeRoleP.role as ${employeeRoleP}_role,
$employeeRoleP.parentRole as ${employeeRoleP}_parentRole
"""

It might be due to naming issue, You have named your table in Entity as EmployeeRole where as in you Dao you are accessing it using EmployeeWithRole . You need to keep both the names same.

尝试用@Entity(“ EmployeeWithRole”)注释EmployeeWithRole

import androidx.room.DatabaseView
import androidx.room.Embedded

 @DatabaseView(baseQuery)
 data class EmployeeWithRole(
    @Embedded
    val employee: Employee,
    @Embedded(prefix = employeeRoleP + "_")
    val employeeRole: EmployeeRole)

 const val employeeRoleP = "EmployeeRole"
 private const val employeeRoleParams = """
        $employeeRoleP.id as ${employeeRoleP}_id,
        $employeeRoleP.role as ${employeeRoleP}_role,
        $employeeRoleP.parentRole as ${employeeRoleP}_parentRole
    """

 const val baseQuery = """
        SELECT Employee.*, $employeeRoleParams FROM Employee
        INNER JOIN EmployeeRole ON Employee.currentRoleId = EmployeeRole.id
    """

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM