0
エンティティCookbook
,があります。それはManyToManyの関係です:クックブックは複数のレシピを持つことができ、レシピは複数の料理ブックに割り当てることができます。そこで、エンティティCookbookRecipe
を追加し、エンティティをOneToManyとして接続しました。ManyToManyを維持する方法
どこCookbook
とRecipe
との関係を追加/削除する方法を置くためには - このメソッドは、新しいCookbookRecipe
を追加し、Cookbook
とRecipe
にこのCookbookRecipe
を追加する必要がありますでしょうか? 私の理解では、私は
public void addRecipe(Recipe recipe) {
CookbookRecipe relation = new CookbookRecipe();
relation.setCookbook(this);
relation.setRecipe(recipe);
this.cookbookRecipes.add(relation);
}
が、これは正しい方向です期待しますか? このメソッドをCookbook DAOまたはRecipe DAOに追加するか、またはこれをサービスに追加しますか?
@Entity
public class Cookbook {
private Integer id;
private String title;
private Collection<CookbookRecipe> cookbookRecipes;
private Collection<CookbookSortlevel> cookbookSortlevels;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Basic
@Column(name = "title")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Cookbook cookbook = (Cookbook) o;
if (id != null ? !id.equals(cookbook.id) : cookbook.id != null) return false;
if (title != null ? !title.equals(cookbook.title) : cookbook.title != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (title != null ? title.hashCode() : 0);
return result;
}
@OneToMany(mappedBy = "cookbook", fetch = FetchType.EAGER)
public Collection<CookbookRecipe> getCookbookRecipes() {
return cookbookRecipes;
}
public void setCookbookRecipes(Collection<CookbookRecipe> cookbookRecipes) {
this.cookbookRecipes = cookbookRecipes;
}
@OneToMany(mappedBy = "cookbook")
public Collection<CookbookSortlevel> getCookbookSortlevels() {
return cookbookSortlevels;
}
public void setCookbookSortlevels(Collection<CookbookSortlevel> cookbookSortlevels) {
this.cookbookSortlevels = cookbookSortlevels;
}
}
@Entity
@Table(name = "cookbook_recipe", schema = "", catalog = "")
public class CookbookRecipe {
private Integer id;
private Recipe recipe;
private Cookbook cookbook;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CookbookRecipe that = (CookbookRecipe) o;
if (id != null ? !id.equals(that.id) : that.id != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
return result;
}
@ManyToOne
@JoinColumn(name = "recipe_id", referencedColumnName = "id")
public Recipe getRecipe() {
return recipe;
}
public void setRecipe(Recipe recipe) {
this.recipe = recipe;
}
@ManyToOne
@JoinColumn(name = "cookbook_id", referencedColumnName = "id", nullable=false,insertable=false,updatable=false)
public Cookbook getCookbook() {
return cookbook;
}
public void setCookbook(Cookbook cookbook) {
this.cookbook = cookbook;
}
}
@Entity
public class Recipe {
private Integer id;
private String title;
private String text;
private Collection<RecipeIngredient> recipeIngredients;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Basic
@Column(name = "title")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Basic
@Column(name = "text")
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Recipe recipe = (Recipe) o;
if (id != null ? !id.equals(recipe.id) : recipe.id != null) return false;
if (title != null ? !title.equals(recipe.title) : recipe.title != null) return false;
if (text != null ? !text.equals(recipe.text) : recipe.text != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (title != null ? title.hashCode() : 0);
result = 31 * result + (text != null ? text.hashCode() : 0);
return result;
}
@OneToMany(mappedBy = "recipe")
public Collection<RecipeIngredient> getRecipeIngredients() {
return recipeIngredients;
}
public void setRecipeIngredients(Collection<RecipeIngredient> recipeIngredients) {
this.recipeIngredients = recipeIngredients;
}
}
これを処理するgithubプロジェクトはありますか?