java - How to maintain ManyToMany -
there entities cookbook
, recipe
. it's manytomany relation: cookbook can have multiple recipes , recipe can assigned multiple cookbooks. added entity cookbookrecipe
, connected entities onetomany.
where put method add/remove relation between cookbook
, recipe
- method has add new cookbookrecipe
, add cookbookrecipe
cookbook
, recipe
? in understanding expect
public void addrecipe(recipe recipe) { cookbookrecipe relation = new cookbookrecipe(); relation.setcookbook(this); relation.setrecipe(recipe); this.cookbookrecipes.add( relation ); }
is right direction? add method cookbook dao or recipe dao or put service?
@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 = (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; } }
are there github projects handling suggest?
this is, me, right direction. code looks good
i think should add in cookbookdao because cookbook contains recipes not opposite
Comments
Post a Comment