Closed
Description
there are two table with the same column name 'ID' and without the same type
<resultMap id="BaseResultMap" type="com.geco.domain.MdProduct">
<id column="ID" property="id" jdbcType="VARCHAR" />
...
</resultMap>
<resultMap id="InfoResultMap" extends="BaseResultMap" type="com.geco.service.product.dto.ProductResp">
<result column="INFO_ID" property="additionalInfo.id" jdbcType="DECIMAL"/>
....
<collection property="skus"
select="selectSkuByPk1" column="ID"/>
</resultMap>
<select id="select resultMap="InfoResultMap">
select p.id ,i.ID as INFO_ID from md_product,md_product_info i
where ....
</select>
there are error info
org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'ID' from result set. Cause: java.sql.SQLException: Invalid value for getLong() - '82620ef6659811e9809900163e02c6cf'
I debug it and try fix it
class: org.apache.ibatis.type.UnknownTypeHandler
method: private TypeHandler<?> resolveTypeHandler(ResultSet rs, String column)
old code:
private TypeHandler<?> resolveTypeHandler(ResultSet rs, String column) {
try {
Map<String, Integer> columnIndexLookup = new HashMap();
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
for (int i = 1; i <= count; ++i) {
String name = rsmd.getColumnName(i);
columnIndexLookup.put(name, i);
}
Integer columnIndex = (Integer) columnIndexLookup.get(column);
TypeHandler<?> handler = null;
if (columnIndex != null) {
handler = this.resolveTypeHandler(rsmd, columnIndex);
}
if (handler == null || handler instanceof UnknownTypeHandler) {
handler = OBJECT_TYPE_HANDLER;
}
return (TypeHandler) handler;
} catch (SQLException var8) {
throw new TypeException("Error determining JDBC type for column " + column + ". Cause: " + var8, var8);
}
}
this is my code:
private TypeHandler<?> resolveTypeHandler(ResultSet rs, String column) {
try {
Map<String, Integer> columnIndexLookup = new HashMap();
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
for (int i = 1; i <= count; ++i) {
String name = rsmd.getColumnLabel(i);
columnIndexLookup.put(name, i);
}
Integer columnIndex = (Integer) columnIndexLookup.get(column);
TypeHandler<?> handler = null;
if (columnIndex != null) {
handler = this.resolveTypeHandler(rsmd, columnIndex);
}
if (handler == null || handler instanceof UnknownTypeHandler) {
handler = OBJECT_TYPE_HANDLER;
}
return (TypeHandler) handler;
} catch (SQLException var8) {
throw new TypeException("Error determining JDBC type for column " + column + ". Cause: " + var8, var8);
}
}
getColumnName = > getColumnLabel
it works but i don't know there are any other bugs after i fixed this bug
Activity
harawata commentedon May 31, 2019
Hi @dezhishen ,
Could you provide a demo project with a minimum set of tables/columns?
Thanks in advance!
dezhishen commentedon Jun 22, 2019
@harawata
Table
ProductSku.class
ProductInfo.class
ProductResp.class
ProductMapper.xml
ProductMapper.class
Call function selectAllInfo
This code will use ID when there are two column that their names are ID without same type.
harawata commentedon Jun 22, 2019
Thanks for the update, @dezhishen !
I think I understood the problem.
Actually, MyBatis should check the value of
useColumnLabel
setting.useColumnLabel=true
(default),getColumnLabel()
should be used.useColumnLabel=false
,getColumnName()
should be used.I am not sure how to fix this cleanly yet.
huangdx0726 commentedon Jul 3, 2019
@dezhishen @harawata mysql驱动包5.1及以上会产生这个问题。按你的修改可以解决这个问题,如果怕引发其他bug可以改用5.0的驱动包试试。
dezhishen commentedon Jul 10, 2019
通过另外一种
<collection>
使用方式和sql关联,一次查出所有数据的方式,可以避免上述情况发生,也更好http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#Result_Maps
在resultMap中使用
There are another way to use the tag
<collection>
.We can query all columns only once call sql.
Like this:
http://www.mybatis.org/mybatis-3/sqlmap-xml.html#Result_Maps
resultMap:
kazuki43zoo commentedon Jul 10, 2019
@huangdx0726 @dezhishen , Please use English as much as possible.
Change to refers useColumnLabel at UnknownTypeHandler
Change filed name to 'config' for prevent dupricateing with parent cl…
8 remaining items