You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Daniel Henrique Alves Lima <em...@yahoo.com.br> on 2008/09/25 20:32:18 UTC

ResultMap columns as a superset of select columns

	Hi everybody. First of all: I'm sorry for my bad English.

	How can define just one resultMap with all necessary columns and reuse
it in selects with different columns ?
	Something like this:

   <resultMap id="EntityMap" class="Entity">
     <result property="code" column="cd_entity" />
     <result property="name" column="nm_entity" />
   </resultMap>

   <select id="selectAllEntities" resultMap="EntityMap">
     select * from entity
   </select>

   <select id="selectAllEntitiesCodes" resultMap="EntityMap">
     select cd_entity from entity
   </select>


	Can I implement/extend some of the IBatis' interfaces and classes to
achieve this ?


	Thanks in advance !



Re: ResultMap columns as a superset of select columns

Posted by Sundar Sankar <fa...@gmail.com>.
If you carefully design your resultmaps, the "extends" property of the
result map could help you. The only precaution you might have to take is
that, The order of declaration should be the order of definition in the
sqlmap file.



On Thu, Sep 25, 2008 at 4:11 PM, Daniel Henrique Alves Lima <
email_daniel_h@yahoo.com.br> wrote:

>        I'm trying to re-use the same ResultMap multiple times, but in the
> second query (with one column) the JDBC driver will raise an exception
> because "nm_entity" does not exist in the returned ResultSet.
>        I want to avoid to define a lot of ResultMaps for the same table...
> Is
> there an way to force IBatis to return null for missing columns instead
> of raise an exception (yes, i know that is the driver that is raising
> the exception because IBatis is asking for property/column
> name/nm_entity) ?
>
>        Thanks.
>
> ===========================================
>
> Exception in thread "main"
> com.ibatis.common.jdbc.exception.NestedSQLException:
> --- The error occurred in Entity.xml.
> --- The error occurred while applying a result map.
> --- Check the EntityMap.
> --- Check the result mapping for the 'name' property.
> --- Cause: org.postgresql.util.PSQLException: The column name nm_entity
> was not found in this ResultSet.
>        at
>
> com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryWithCallback(MappedStatement.java:201)
>        at
>
> com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryForList(MappedStatement.java:139)
>        at
>
> com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:567)
>        at
>
> com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:541)
>        at
>
> com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(SqlMapSessionImpl.java:118)
>        at
>
> com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(SqlMapSessionImpl.java:122)
>        at
>
> com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.queryForList(SqlMapClientImpl.java:98)
>        at Test.main(Test.java:23)
>
>
> On Thu, 2008-09-25 at 14:05 -0500, Shannon, Bryan wrote:
> > You can alias your columns in your select.
> >
> >
> >  Select foo as cd_entity, bar as nm_entity from table
> >
> > iBatis will use the alias to map the results in the resultMap.
> >
> > -----Original Message-----
> > From: Daniel Henrique Alves Lima [mailto:email_daniel_h@yahoo.com.br]
> > Sent: Thursday, September 25, 2008 2:32 PM
> > To: user-java@ibatis.apache.org
> > Subject: ResultMap columns as a superset of select columns
> >
> >       Hi everybody. First of all: I'm sorry for my bad English.
> >
> >       How can define just one resultMap with all necessary columns and
> > reuse it in selects with different columns ?
> >       Something like this:
> >
> >    <resultMap id="EntityMap" class="Entity">
> >      <result property="code" column="cd_entity" />
> >      <result property="name" column="nm_entity" />
> >    </resultMap>
> >
> >    <select id="selectAllEntities" resultMap="EntityMap">
> >      select * from entity
> >    </select>
> >
> >    <select id="selectAllEntitiesCodes" resultMap="EntityMap">
> >      select cd_entity from entity
> >    </select>
> >
> >
> >       Can I implement/extend some of the IBatis' interfaces and
> > classes to achieve this ?
> >
> >
> >       Thanks in advance !
> >
> >
> --
> "If there must be trouble, let it be in my day,
>  that my child may have peace."
>
> Thomas Paine
>
>

RE: ResultMap columns as a superset of select columns

Posted by Daniel Henrique Alves Lima <em...@yahoo.com.br>.
	I'm trying to re-use the same ResultMap multiple times, but in the
second query (with one column) the JDBC driver will raise an exception
because "nm_entity" does not exist in the returned ResultSet. 
	I want to avoid to define a lot of ResultMaps for the same table... Is
there an way to force IBatis to return null for missing columns instead
of raise an exception (yes, i know that is the driver that is raising
the exception because IBatis is asking for property/column
name/nm_entity) ?

	Thanks.

===========================================

Exception in thread "main"
com.ibatis.common.jdbc.exception.NestedSQLException:   
--- The error occurred in Entity.xml.  
--- The error occurred while applying a result map.  
--- Check the EntityMap.  
--- Check the result mapping for the 'name' property.  
--- Cause: org.postgresql.util.PSQLException: The column name nm_entity
was not found in this ResultSet.
	at
com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryWithCallback(MappedStatement.java:201)
	at
com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryForList(MappedStatement.java:139)
	at
com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:567)
	at
com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:541)
	at
com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(SqlMapSessionImpl.java:118)
	at
com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(SqlMapSessionImpl.java:122)
	at
com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.queryForList(SqlMapClientImpl.java:98)
	at Test.main(Test.java:23)


On Thu, 2008-09-25 at 14:05 -0500, Shannon, Bryan wrote:
> You can alias your columns in your select.
> 
> 
>  Select foo as cd_entity, bar as nm_entity from table
> 
> iBatis will use the alias to map the results in the resultMap.
> 
> -----Original Message-----
> From: Daniel Henrique Alves Lima [mailto:email_daniel_h@yahoo.com.br] 
> Sent: Thursday, September 25, 2008 2:32 PM
> To: user-java@ibatis.apache.org
> Subject: ResultMap columns as a superset of select columns
> 
> 	Hi everybody. First of all: I'm sorry for my bad English.
> 
> 	How can define just one resultMap with all necessary columns and
> reuse it in selects with different columns ?
> 	Something like this:
> 
>    <resultMap id="EntityMap" class="Entity">
>      <result property="code" column="cd_entity" />
>      <result property="name" column="nm_entity" />
>    </resultMap>
> 
>    <select id="selectAllEntities" resultMap="EntityMap">
>      select * from entity
>    </select>
> 
>    <select id="selectAllEntitiesCodes" resultMap="EntityMap">
>      select cd_entity from entity
>    </select>
> 
> 
> 	Can I implement/extend some of the IBatis' interfaces and
> classes to achieve this ?
> 
> 
> 	Thanks in advance !
> 
> 
-- 
"If there must be trouble, let it be in my day, 
 that my child may have peace."

Thomas Paine


RE: ResultMap columns as a superset of select columns

Posted by "Shannon, Bryan" <BS...@Tribune.com>.
You can alias your columns in your select.


 Select foo as cd_entity, bar as nm_entity from table

iBatis will use the alias to map the results in the resultMap.

-----Original Message-----
From: Daniel Henrique Alves Lima [mailto:email_daniel_h@yahoo.com.br] 
Sent: Thursday, September 25, 2008 2:32 PM
To: user-java@ibatis.apache.org
Subject: ResultMap columns as a superset of select columns

	Hi everybody. First of all: I'm sorry for my bad English.

	How can define just one resultMap with all necessary columns and
reuse it in selects with different columns ?
	Something like this:

   <resultMap id="EntityMap" class="Entity">
     <result property="code" column="cd_entity" />
     <result property="name" column="nm_entity" />
   </resultMap>

   <select id="selectAllEntities" resultMap="EntityMap">
     select * from entity
   </select>

   <select id="selectAllEntitiesCodes" resultMap="EntityMap">
     select cd_entity from entity
   </select>


	Can I implement/extend some of the IBatis' interfaces and
classes to achieve this ?


	Thanks in advance !