ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Clinton Begin" <clinton.be...@gmail.com>
Subject Re: Trying to use HashMap as a param object for an UPDATE - getting an exception
Date Sun, 14 Dec 2008 03:09:48 GMT
Now, now... :-)

Maps are useful for more than just ad hoc non-types... they're good for
passing multiple (typed) params as well.

But I believe the typing Nathan was talking about was the parameter type
itself... without specifying the Map as the expected parameter, you could
happily pass someObjectOfAnyClass to the statement and it would work.

That said, (to further argue with myself), I actually prefer the flexibility
of not typing the statement. It's a sort of duck typing for iBATIS.

Clinton

On Sat, Dec 13, 2008 at 5:48 PM, Larry Meadors <larry.meadors@gmail.com>wrote:

> If you're passing a Map, you've already lost it...in more ways than one.
> ;-)
>
> Larry
>
>
> On Sat, Dec 13, 2008 at 3:56 PM, Nathan Maves <nathan.maves@gmail.com>
> wrote:
> > sure you could do that put you lose a bit of type safety
> >
> > On Fri, Dec 12, 2008 at 4:08 PM, Larry Meadors <larry.meadors@gmail.com>
> wrote:
> >> Actually...it's even easier.
> >>
> >> <update id="updateSector">
> >>
> >> No parameter class or map required.
> >>
> >> Larry
> >>
> >>
> >> On Fri, Dec 12, 2008 at 12:12 PM, joeweder <joeweder@gmail.com> wrote:
> >>>
> >>> You do not have to map a Map as a parameter. Just tell iBatis that the
> >>> parameter is a map and use the key-names you used to populate it in
> your
> >>> update (note: use parameterClass instead of parameterMap).
> >>>
> >>> Map s1 = new HashMap();
> >>> s1.put("code", "001");
> >>> s1.put("description", "Region Transaction upd");
> >>> s1.put("fiscalYear", "2005");
> >>> s1.put("id", "1501");
> >>> client.update("updateSector", s1);
> >>>
> >>> <update id="updateSector" parameterClass="map">
> >>>        UPDATE SECTOR SET SECTOR_DESC = #description# WHERE SECTOR_ID =
> #id#
> >>> </update>
> >>>
> >>> That's all there is to it.
> >>>
> >>>
> >>> Alex Savitsky-2 wrote:
> >>>>
> >>>> Hi,
> >>>>
> >>>> I'm trying to execute an update, with a HashMap for parameter (there's
> no
> >>>> business object to pass). The mapping file is as follows (iBATIS
> >>>> v.2.3.0.677)
> >>>>
> >>>> <?xml version="1.0" encoding="UTF-8" ?>
> >>>> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
> >>>> "http://ibatis.apache.org/dtd/sql-map-2.dtd">
> >>>> <sqlMap namespace="Sector">
> >>>> <resultMap class="java.util.HashMap" id="sector">
> >>>>       <result property="id" column="SECTOR_ID"
> javaType="java.lang.Integer" />
> >>>>       <result property="code" column="SECTOR_CD" />
> >>>>       <result property="description" column="SECTOR_DESC" />
> >>>>       <result property="fiscalYear" column="FISCAL_YR"
> >>>> javaType="java.lang.Integer" />
> >>>> </resultMap>
> >>>> <parameterMap class="java.util.HashMap" id="sector">
> >>>>       <parameter property="id" jdbcType="NUMERIC" />
> >>>>       <parameter property="code" jdbcType="VARCHAR" />
> >>>>       <parameter property="description" jdbcType="VARCHAR" />
> >>>>       <parameter property="fiscalYear" jdbcType="NUMERIC" />
> >>>> </parameterMap>
> >>>> <select id="getSectorList" resultMap="sector" parameterMap="sector">
> >>>>       SELECT * FROM SECTOR WHERE FISCAL_YR = #fiscalYear#
> >>>>       <isNotEmpty property="code">AND SECTOR_CD LIKE
> #code#||'%'</isNotEmpty>
> >>>>       <isNotEmpty property="description">AND SECTOR_DESC LIKE
> >>>> #description#||'%'</isNotEmpty>
> >>>> </select>
> >>>> <update id="updateSector" parameterMap="sector">
> >>>>       UPDATE SECTOR SET SECTOR_DESC = #description# WHERE SECTOR_ID
=
> #id#
> >>>> </update>
> >>>> </sqlMap>
> >>>>
> >>>> Both parameters and results are Maps, and the select works when I pass
> it
> >>>> a parameter map, whether it has all the properties, or not. Update,
> >>>> however, fails:
> >>>>
> >>>> Map s1 = new HashMap();
> >>>> s1.put("code", "001");
> >>>> s1.put("description", "Region Transaction upd");
> >>>> s1.put("fiscalYear", "2005");
> >>>> s1.put("id", "1501");
> >>>> client.update("updateSector", s1);
> >>>>
> >>>> com.ibatis.common.jdbc.exception.NestedSQLException:
> >>>> --- The error occurred in ca/cihi/cmdb/model/Sector.xml.
> >>>> --- The error occurred while applying a parameter map.
> >>>> --- Check the Sector.sector.
> >>>> --- Check the parameter mapping for the 'id' property.
> >>>> --- Cause: java.sql.SQLException: Invalid column index
> >>>>       at
> >>>>
> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeUpdate(GeneralStatement.java:91)
> >>>>       at
> >>>>
> com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.update(SqlMapExecutorDelegate.java:505)
> >>>>       at
> >>>>
> com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.update(SqlMapSessionImpl.java:90)
> >>>>       at
> >>>>
> com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.update(SqlMapClientImpl.java:67)
> >>>>       at
> >>>>
> ca.cihi.cmdb.service.MaintenanceService.update(MaintenanceService.java:52)
> >>>>       ... 29 more
> >>>> Caused by: java.sql.SQLException: Invalid column index
> >>>>       at
> >>>>
> oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
> >>>>       at
> >>>>
> oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:162)
> >>>>       at
> >>>>
> oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:227)
> >>>>       at
> >>>>
> oracle.jdbc.driver.OraclePreparedStatement.setStringInternal(OraclePreparedStatement.java:4754)
> >>>>       at
> >>>>
> oracle.jdbc.driver.OraclePreparedStatement.setString(OraclePreparedStatement.java:4717)
> >>>>       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> >>>>       at
> >>>>
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> >>>>       at
> >>>>
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> >>>>       at java.lang.reflect.Method.invoke(Method.java:585)
> >>>>       at
> >>>>
> com.ibatis.common.jdbc.logging.PreparedStatementLogProxy.invoke(PreparedStatementLogProxy.java:70)
> >>>>       at $Proxy2.setString(Unknown Source)
> >>>>       at
> >>>>
> com.ibatis.sqlmap.engine.type.StringTypeHandler.setParameter(StringTypeHandler.java:30)
> >>>>       at
> >>>>
> com.ibatis.sqlmap.engine.type.UnknownTypeHandler.setParameter(UnknownTypeHandler.java:69)
> >>>>       at
> >>>>
> com.ibatis.sqlmap.engine.mapping.parameter.BasicParameterMap.setParameter(BasicParameterMap.java:165)
> >>>>       at
> >>>>
> com.ibatis.sqlmap.engine.mapping.parameter.BasicParameterMap.setParameters(BasicParameterMap.java:125)
> >>>>       at
> >>>>
> com.ibatis.sqlmap.engine.execution.SqlExecutor.executeUpdate(SqlExecutor.java:79)
> >>>>       at
> >>>>
> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.sqlExecuteUpdate(GeneralStatement.java:200)
> >>>>       at
> >>>>
> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeUpdate(GeneralStatement.java:78)
> >>>>       ... 33 more
> >>>>
> >>>> Am I doing something wrong?
> >>>>
> >>>> Thanks,
> >>>>
> >>>> Alex
> >>>>
> >>>
> >>> --
> >>> View this message in context:
> http://www.nabble.com/Trying-to-use-HashMap-as-a-param-object-for-an-UPDATE---getting-an-exception-tp20922966p20981745.html
> >>> Sent from the iBATIS - User - Java mailing list archive at Nabble.com.
> >>>
> >>>
> >>
> >
>

Mime
View raw message