2016-03-18 242 views
2

この問題を解決するために、私は多くのコードを作成しました。MyBatis一見正しいマッピングのためのTooManyResultsException

私はこのコレクションの作業を取得するために別のものを試してみて、このエラーを取得し続ける:1つの 結果(またはnull)を期待する:

ネストされた例外は org.apache.ibatis.exceptions.TooManyResultsExceptionです)(selectOneによって返されるが、見出される:

class Recipe { 
    String name 
    List<RecipeIngredient> ingredients 
} 
class RecipeIngredient { 
    Double measurementAmount 
} 
を以下のように2

関連するオブジェクトであります

public interface CookbookDao { 
    public Recipe getRecipe(@Param("id")int id) 
} 

そして、私の結果マッパー:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
     "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 

<mapper namespace="cookbook.daos.CookbookDao"> 
    <select id="getRecipe" resultMap="Recipe"> 
     SELECT 
      r.name, 
      ri.measurement_amount 
     FROM 
      recipe r 
      INNER JOIN recipe_ingredient ri on ri.recipe_id = r.id 
     WHERE 
      r.id = #{id} 
    </select> 

    <resultMap id="Recipe" type="cookbook.domain.Recipe"> 
     <result property="name" column="r.name" /> 
     <collection property="ingredients" ofType="cookbook.domain.RecipeIngredient"> 
      <result property="measurementAmount" column="ri.measurement_amount"/> 
     </collection> 
    </resultMap> 
</mapper> 

問合せは(次の結果を返します。注:上記のコードは唯一の「measurement_amount」を持っていますが、私はしました

私は私のメソッドの呼び出しでインターフェースを持っています)実際、最終結果セットは、私は/が戻ってこれらの2行を取得する必要がありますする理由を示して支援するためにどのように見えるか含ま:私はコレクションの0を取るとき

name | measurement_amount | name | abbreviation | name 
------+--------------------+------+--------------+------- 
Rice |     1 | cup |    | rice 
Rice |     2 | cups |    | water 

私はマッパーが仕事を得ることができますut。 javaTypeを使用しようとしましたが、コレクションの複合キーを使用しようとしましたが、それでも動作しませんでした。私はアイデアを使い果たして、1トンのヘルプ記事を見たが、何も目立たなかった。私はMyBatisのとMyBatisのスプリングのUTDのバージョンで春のブートを使用してい

+0

オブジェクトマップは、複数のレコードを1つのオブジェクトに挿入しようとしているようです。クラスresultMapがコレクションを持つオブジェクトに変更する必要があるか、クエリが単一の結果を返す必要があります。結果としてあなたが期待するものを複数のライスと一緒に持っているテーブルですか? –

+0

@TahTatsumoto私は元の質問の結果を更新して、2行が返ってくる理由を示しました。現実のレシピを想像してください - 複数の食材があります。したがって、リレーショナル表は、照会された各レシピごとにRecipeIngredientから複数の行を戻します。 私は_does_にマップしているレシピオブジェクトにList型があるので、ネストされたコレクションが機能することを理解している限り、コレクションにマップする必要があります。 –

+0

MyBatisは、両方の行が** 1つの成分を含む** 2つの**レシピではなく、** 1 ** **レシピであることを認識する必要がありますか?まず、私が試してみたいのは、単に "': ''の代わりにidを設定することです。 –

答えて

2

それはMyBatisのは、エイリアステーブルと私の関連を理解していないことが判明したので、私は私のクエリを変更:

SELECT 
    r.name as recipe_name, 
    ri.measurement_amount as measurement_amount 
FROM 
    recipe r 
    INNER JOIN recipe_ingredient ri on ri.recipe_id = r.id 
WHERE 
    r.id = #{id} 

と(recipe_name代わりri.nameの、など)のカラムのエイリアスを使用するマッパーを更新、など:

<resultMap id="Recipe" type="cookbook.domain.Recipe"> 
    <result property="name" column="recipe_name" /> 
    <collection property="ingredients" ofType="cookbook.domain.RecipeIngredient"> 
     <result property="measurementAmount" column="measurement_amount"/> 
    </collection> 
</resultMap> 

、それが働きました!

+0

どのように注釈を使用しますか? – Coder17

0

あなたが使用Listを試すことができます助けるためにコメントした者に

感謝。

public interface CookbookDao { 
    public List<Recipe> getRecipe(@Param("id")int id); 
} 
関連する問題