android 房间数据库视图无法从 table select
android room databaseview can't select from table
我创建了一个 DatabaseView,以加入两个 table。当我进行查询时,我无法从数据库视图 table select。但它可以是 return 类型。
它给我无法解析符号 EmployeeWithRole。
我正在使用 roomVersion = '2.1.0-alpha04
我的实体:
@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)
我的道:
@Dao
Interface EmployeeWithRoleDao {
@Query(" SELECT * FROM EmployeeWithRole ")
fun getAllEmployees(): List<EmployeeWithRole>
}
我的数据库视图:
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
"""
可能是由于命名问题,您在实体中将 table 命名为 EmployeeRole
,而在 Dao 中您使用 EmployeeWithRole
访问它。您需要保持两个名称相同。
尝试使用 @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
"""
我创建了一个 DatabaseView,以加入两个 table。当我进行查询时,我无法从数据库视图 table select。但它可以是 return 类型。 它给我无法解析符号 EmployeeWithRole。 我正在使用 roomVersion = '2.1.0-alpha04
我的实体:
@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)
我的道:
@Dao
Interface EmployeeWithRoleDao {
@Query(" SELECT * FROM EmployeeWithRole ")
fun getAllEmployees(): List<EmployeeWithRole>
}
我的数据库视图:
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
"""
可能是由于命名问题,您在实体中将 table 命名为 EmployeeRole
,而在 Dao 中您使用 EmployeeWithRole
访问它。您需要保持两个名称相同。
尝试使用 @Entity("EmployeeWithRole")
注释 EmployeeWithRoleimport 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
"""