私は新しいプロジェクトでRoomを使用し始めました。データベースといくつかのDAOを作成した後、この問題が発生し始めました。現在、gradleはプロジェクトをコンパイルすることさえできません。私は、問題は、私はルームでやった間違いによるものであるかなり確信しているタスク ':app:compileDebugJavaWithJavac'の実行に失敗しました。 > java.util.NoSuchElementException:リストが空です
:下の画像を参照してください。私は以下のデータモデルのコードを添付しています。
基本的に、2つのエンティティ(RecipeEntryとIngredientEntry)を持つデータベースがあります。私はあなたが下に見る方法で関係をモデル化しようとしました。また、RecipeDaoでモデル化されたクエリで返されるデータをカプセル化するために、RecipeWithIngredientsというPOJOを使用します。このアプローチを選択する理由は、Roomが他のORMと同じ方法で関係をモデル化することを禁止することです。
SQLiteはリレーショナルデータベースであるため、オブジェクト間の関係を指定できます。ほとんどのORMライブラリでは、エンティティオブジェクトが互いに参照することができますが、Roomはこれを明示的に禁止しています。
詳細here。
@Entity(tableName = "recipe")
public class RecipeEntry {
@PrimaryKey
private int id;
private String name;
private String image;
private int servings;
}
@Entity(tableName = "ingredient")
public class IngredientEntry {
@PrimaryKey(autoGenerate = true)
private int id;
private int quantity;
private String measure;
private String description;
@ColumnInfo(name = "recipe_id")
private int recipeId;
}
public class RecipeWithIngredients {
@Embedded
private RecipeEntry recipe;
@Relation(parentColumn = "id", entityColumn = "recipe_id", entity = IngredientEntry.class)
private List ingredients;
public RecipeEntry getRecipe() {
return recipe;
}
public void setRecipe(RecipeEntry recipe) {
this.recipe = recipe;
}
public List getIngredients() {
return ingredients;
}
public void setIngredients(List ingredients) {
this.ingredients = ingredients;
}
}
@Dao
public interface RecipeDao {
@Query("SELECT * FROM recipe, ingredient WHERE recipe.id = :recipeId AND ingredient.recipe_id = recipe.id")
List<RecipeWithIngredients> getRecipeWithIngredients(int recipeId);
}
何が起こっているかについての手掛かりはありますか?