在应用 mybatis 进行一对多嵌套查问时, 报错如下
org.apache.ibatis.exceptions.PersistenceException:
Error querying database. Cause: org.apache.ibatis.executor.ExecutorException: Statement returned more than one row, where no more than one was expected.
The error may exist in com/bigdata/mapper/UserMapper.xml
The error may involve com.bigdata.mapper.UserMapper.findAllOrders
The error occurred while handling results
SQL: select * from orders where uid = ?
Cause: org.apache.ibatis.executor.ExecutorException: Statement returned more than one row, where no more than one was expected.
谬误波及代码如下
谬误大体是说, 箭头指向的 sql 语句返回的数据行数大于 1.
这个在业务上是 OK 的, 因为 orders 表是订单表, 一个用户可能有多个订单.
然而在这里后果多行这件事被作为谬误看待, 阐明应该是哪里没有告知 mybatis 应该应用汇合 (或数组) 承接后果
最终查找到问题在 <collection property="">
, property
属性值在 com.bigdata.domain.User 的定义中是 Orders, 而不是能够进行遍历的 List 类型
所以解决形式也很显著了, 将该成员变量改为可迭代类型, 将相应地批改 get/set 办法以及 toString()办法即可