我创建了一个DatabaseView,以连接两个表。查询时,无法从数据库视图表中进行选择。但这可以是返回类型。 它给我无法解析符号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
"""
答案 0 :(得分:0)
这可能是由于命名问题所致,您已将实体中的表命名为EmployeeRole
,在Dao中,您正在使用EmployeeWithRole
访问它。您需要保持两个名称相同。
答案 1 :(得分:0)
尝试使用@Entity(“ EmployeeWithRole”)注释EmployeeWithRole
答案 2 :(得分:0)
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
"""