Я пытаюсь создать базу данных, которая имеет две таблицы с отношением один ко многим. (один рецепт относится ко многим ингредиентам)
Кажется, я все настроил правильно, я думаю, но теперь, когда я создаю базу данных, я хочу предварительно заполнить ее некоторыми рецептами и ингредиентами, которые относятся к рецепту. Но я не знаю, как реализовать это в конструкторе баз данных.
Вот моя таблица рецептов:
@Entity(tableName = "recipe_table") //Represents the table in SQLite database
public class Recipe {
@PrimaryKey(autoGenerate = true)
private int id; //Holds the id of the recipe
private String title; //Holds the name of the recipe
@Ignore
private List<Ingredient> ingredientsList;
//Generate constructor to create objects later
public Recipe(String title, List<Ingredient> ingredientsList) {
this.title = title;
this.ingredientsList = ingredientsList;
}
//Generate getters to persist values to the database
public int getId() {
return id;
}
//Generate setter so that room can recreate later
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public List<Ingredient> getIngredientsList() {
return ingredientsList;
}
public void setIngredientsList(List<Ingredient> ingredientsList) {
this.ingredientsList = ingredientsList;
}
}
Таблица ингредиентов:
package com.example.kookrecepten;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;
import java.util.List;
import static androidx.room.ForeignKey.CASCADE;
@Entity(foreignKeys = @ForeignKey(entity = Recipe.class, parentColumns = "id", childColumns = "recipeId", onDelete = CASCADE))
public class Ingredient {
@PrimaryKey
private int id;
private int recipeId;
private String title; //Name of the ingredient
@Ignore
private List<Ingredient> ingredientsList;
public Ingredient(String title, int recipeId) {
this.title = title;
this.recipeId = recipeId;
}
public void setId(int id) {
this.id = id;
}
public void setRecipeId(int recipeId) {
this.recipeId = recipeId;
}
public int getId() {
return id;
}
public int getRecipeId() {
return recipeId;
}
public String getTitle() {
return title;
}
}
Это мой файл дао
@Dao
public abstract class RecipeDao {
//Insert recipe
@Insert
public abstract void insertRecipe(Recipe recipe);
//Insert ingredients list
@Insert
public abstract void insertIngredientList(List<Ingredient> ingredients);
@Query("SELECT * FROM recipe_table WHERE id =:id")
public abstract Recipe getRecipe(int id);
@Query("SELECT * FROM Ingredient WHERE recipeId =:recipeId")
public abstract List<Ingredient> getIngredientList(int recipeId);
public void insertRecipeWithIngredients(Recipe recipe) {
List<Ingredient> ingredients = recipe.getIngredientsList();
for (int i = 0; i < ingredients.size(); i++) {
ingredients.get(i).setRecipeId(recipe.getId());
}
insertIngredientList(ingredients);
insertRecipe(recipe);
}
public Recipe getRecipeWithIngredients(int id) {
Recipe recipe = getRecipe(id);
List<Ingredient> ingredients = getIngredientList(id);
recipe.setIngredientsList(ingredients);
return recipe;
}
}
Но мой проблема в том, что я понятия не имею, как предварительно заполнить свою базу данных.
private static class PopulateDbAsyncTask extends AsyncTask<Void, Void, Void> {
private RecipeDao recipeDao;
private PopulateDbAsyncTask(RecipeDatabase db) {
recipeDao = db.recipeDao();
}
@Override
protected Void doInBackground(Void... voids) {
recipeDao.insertRecipeWithIngredients(
//insert a recipe and a list of ingredients?
);
return null;
}
}