Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何嵌入非';没有直接关系吗?_Android_Android Room - Fatal编程技术网

Android 如何嵌入非';没有直接关系吗?

Android 如何嵌入非';没有直接关系吗?,android,android-room,Android,Android Room,我有三个基本实体来表示我房间数据库中的表:国家、州和城市 为了在一个州内嵌入城市列表,我创建了一个POJO,名为StateWithCities: package org.example.roomtest; import java.util.List; import androidx.room.Embedded; import androidx.room.Relation; public class StateWithCities { @Embedded public Sta

我有三个基本实体来表示我房间数据库中的表:国家城市

为了在一个州内嵌入城市列表,我创建了一个POJO,名为StateWithCities

package org.example.roomtest;

import java.util.List;
import androidx.room.Embedded;
import androidx.room.Relation;

public class StateWithCities {

    @Embedded
    public State state;

    @Relation(
            parentColumn = "_id", // This is the `states`.`_id` column.
            entityColumn = "state_id" // This is the `cities`.`state_id` column.
    )
    public List<City> cities;
}
@Query("SELECT * FROM `countries`")
LiveData<List<CountryWithStatesWithCities>> getAllCountriesWithStatesWithCities();
在这一点上,一切都很好。但当我将此查询添加到CountryDao时:

package org.example.roomtest;

import java.util.List;
import androidx.room.Embedded;
import androidx.room.Relation;

public class StateWithCities {

    @Embedded
    public State state;

    @Relation(
            parentColumn = "_id", // This is the `states`.`_id` column.
            entityColumn = "state_id" // This is the `cities`.`state_id` column.
    )
    public List<City> cities;
}
@Query("SELECT * FROM `countries`")
LiveData<List<CountryWithStatesWithCities>> getAllCountriesWithStatesWithCities();

…CountryWithStatesWithCities.java:16:错误:在org.team\u love.shapethefuture.roomtest.StateWithCities中找不到子实体列'country\u id'。选项:
>公共城市名单;
>                                  ^

这些错误似乎告诉我,我的CountryDao接口需要引用实体(或数据库视图),而不是POJO。那么,解决我的需求的正确方法是什么:如何嵌入不直接相关的文件室实体?

您需要设置实体类来明确描述
CountryWithStatesWithCities
,因为文件室在从上下文推断时存在问题:

public class CountryWithStatesWithCities {

    @Embedded
    public Country country;

    @Relation(
            entity = State.class, <- this should be added explicitly
            parentColumn = "_id", // This is the `country`.`_id` column.
            entityColumn = "country_id" // This is the `states`.`country_id` column.
    )
    public List<StateWithCities> stateWithCities;
}
公共类国家和州/市{
@嵌入
公共国家;
@关系(
实体=State.class,
> ...CountryWithStatesWithCities.java:16: error: Cannot find the child entity column `country_id` in org.team_love.shapethefuture.roomtest.StateWithCities. Options: 
>     public List<StateWithCities> stateWithCities;
>                                  ^
public class CountryWithStatesWithCities {

    @Embedded
    public Country country;

    @Relation(
            entity = State.class, <- this should be added explicitly
            parentColumn = "_id", // This is the `country`.`_id` column.
            entityColumn = "country_id" // This is the `states`.`country_id` column.
    )
    public List<StateWithCities> stateWithCities;
}