mysql - How to deal with several columns linking the same entity -


this example schema database. have following table called recipes:

id | author_name | recipe1_id | recipe2_id | recipe3_id 

all recipes relations recipes_ingredients table:

id | recipe_id | ingredient1 | ingredient2 | ingredient3 

right 3 joins on recipe1_id recipe3_id this:

select recipe1.ingredient1 recipe1ingredient1, recipe1.ingredient2 recipe1ingredient2, recipe1.ingredient3 recipe1ingredient3, [...] recipes inner join recipe recipe1 on (recipes.recipe1_id = recipes.id) inner join recipe recipe2 on (recipes.recipe2_id = recipes.id) inner join recipe recipe3 on (recipes.recipe3_id = recipes.id) 

to map 3 recipes 1 bean in mybatis, have 3 resultsmaps map each recipe recipe object (recipe1, recipe2, recipe3) containing 3 ingredients.

but: want have recipes in list. how achieve that?

ps: sure, have 3 entities in table recipes , table recipes_ingredients. never change, don't see reason have in n:m table.

i think work.

<resultmap id="recipe1" type="recipe1">  ... </resultmap>  <resultmap id="recipe2" type="recipe2">  ... </resultmap>  <resultmap id="recipe3" type="recipe3">  ... </resultmap>  <select id="getrecipes" parametertype="list" resultmap="recipe1, recipe2, recipe3">    select recipe1.ingredient1 recipe1ingredient1, recipe1.ingredient2 recipe1ingredient2, recipe1.ingredient3 recipe1ingredient3, [...]    recipes    inner join recipe recipe1 on (recipes.recipe1_id = recipes.id)    inner join recipe recipe2 on (recipes.recipe2_id = recipes.id)    inner join recipe recipe3 on (recipes.recipe3_id = recipes.id) </select> 

source: mybatis multiple resultsets


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -