2017-09-20 15 views
1

私は新しいプロジェクトでRoomを使用し始めました。データベースといくつかのDAOを作成した後、この問題が発生し始めました。現在、gradleはプロジェクトをコンパイルすることさえできません。私は、問題は、私はルームでやった間違いによるものであるかなり確信しているタスク ':app:compileDebugJavaWithJavac'の実行に失敗しました。 > java.util.NoSuchElementException:リストが空です

enter image description here

:下の画像を参照してください。私は以下のデータモデルのコードを添付しています。

基本的に、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); 
} 

何が起こっているかについての手掛かりはありますか?

答えて

1

この問題で数時間戦ったところ、問題はRecipeWithIngredients POJOにあることがわかりました。 Listに返すオブジェクトの種類を指定する必要があります。そうしないと、Roomに問題が発生します。部屋の開発者がこの点に関してどんな種類のメッセージを印刷してもかまいません。現在のエラーの説明はあまり役に立ちません。 変更されたコードは次のとおりです。

public class RecipeWithIngredients { 
    @Embedded 
    private RecipeEntry recipe; 
    @Relation(parentColumn = "id", entityColumn = "recipe_id", entity = IngredientEntry.class) 
    private List<IngredientEntry> ingredients; 

    public RecipeEntry getRecipe() { 
     return recipe; 
    } 

    public void setRecipe(RecipeEntry recipe) { 
     this.recipe = recipe; 
    } 

    public List<IngredientEntry> getIngredients() { 
     return ingredients; 
    } 

    public void setIngredients(List<IngredientEntry> ingredients) { 
     this.ingredients = ingredients; 
    } 
} 
関連する問題