この問題を解決するために、多くのコードを削減しました。
このコレクションを機能させるためにさまざまなことを試みると、このエラーが引き続き発生します。
ネストされた例外は org.apache.ibatis.exceptions.TooManyResultsException です: selectOne() によって 1 つの結果 (または null) が返されることが期待されていましたが、見つかりました: 2
関連するオブジェクトは次のとおりです。
class Recipe {
String name
List<RecipeIngredient> ingredients
}
class RecipeIngredient {
Double measurementAmount
}
メソッド呼び出しとのインターフェースがあります:
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 行を取得したい/必要な理由を示すのに役立ててください):
name | measurement_amount | name | abbreviation | name
------+--------------------+------+--------------+-------
Rice | 1 | cup | | rice
Rice | 2 | cups | | water
コレクションを取り出すと、マッパーを動作させることができます。javaType を使用してみましたが、コレクションで複合キーを使用してみましたが、それでも機能しませんでした。私はアイデアを使い果たし、たくさんのヘルプ投稿を調べましたが、何も目立ちませんでした.
mybatis および mybatis-spring の UTD バージョンで Spring Boot を使用しています