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 Pascal DeMilly <li...@newgenesys.com> on 2005/01/24 17:42:25 UTC

java.util.HashMap as resultClass

Hi,

I would like to retrieve a Map with whose key is the 1st column of my
query and the value is the 2nd column. For example right now I do:

<select id="getItemNameMap" resultClass="java.util.HashMap">
	select SKU, Description from Items
</select>

However this returns a list of maps with the key being the column name
and the value the column value which seems wasteful in term of space (I
am using remoting to retrieve that info).

I currently solve it by using a custom rowHandler in my DAO as follow:

    public Map getItemNames () throws DataAccessException {
	final KeyValueHandler rowHandler = new KeyValueHandler ();
        getSqlMapClientTemplate().queryWithRowHandler("getItemNameMap", null, rowHandler);
        return rowHandler.getMap();
    }
    
    private class KeyValueHandler implements RowHandler {
        final Map map = new HashMap ();

        public void handleRow(Object valueObject) {
            final Map row = (Map) valueObject; 
            map.put (row.get("SKU"), row.get("Description"));
        }
        
        public Map getMap () {
            return map;
        }
        
    }
    
But I would like to move possibly that code out of my DAO code and into
iBatis SqlMap file.

How could I do that

TIA

Pascal



Re: java.util.HashMap as resultClass

Posted by Brandon Goodin <br...@gmail.com>.
that's a nasty pattern... but hey if you like it... it's your
headache... not mine.


On Mon, 24 Jan 2005 10:32:39 -0800, Pascal DeMilly
<li...@newgenesys.com> wrote:
> Actually I solved it this way:
> 
> in my DAO code, I use a generic, ID and Name for key/value pair. Then in
> my iBatis SQLMap I used aliases to rename my columns to what my DAO
> expect.
> 
> <select id="..." resultClass="java.util.HashMap">
>         select SKU is ID, Description as Name from Items
> </select>
> 
> Now my DAO code is free again from Knowing how the tables are built.
> 
> Regards
> 
> Pascal
> 
> On Mon, 2005-01-24 at 09:22, Pascal DeMilly wrote:
> > Thanks Larry,
> >
> > I tried it and it works. However this still leave some dependencies in
> > my DAO code as I have to specify the column names there as in:
> >
> > return getSqlMapClientTemplate().queryForMap("getItemNameMap", null, "SKU", "Description");
> >
> > How will you do about moving that code into the SqlMap. Should I create
> > a custom resultMap with key/value as properties and refer to it in my
> > DAO code?
> >
> > I am going to give it a try and see what happened.
> >
> > Thanks for you help.
> >
> > Pascal
> >
> > On Mon, 2005-01-24 at 08:44, Larry Meadors wrote:
> > > Try executeQueryForMap() instead.
> > >
> > >
> > > On Mon, 24 Jan 2005 08:42:25 -0800, Pascal DeMilly
> > > <li...@newgenesys.com> wrote:
> > > > Hi,
> > > >
> > > > I would like to retrieve a Map with whose key is the 1st column of my
> > > > query and the value is the 2nd column. For example right now I do:
> > > >
> > > > <select id="getItemNameMap" resultClass="java.util.HashMap">
> > > >         select SKU, Description from Items
> > > > </select>
> > > >
> > > > However this returns a list of maps with the key being the column name
> > > > and the value the column value which seems wasteful in term of space (I
> > > > am using remoting to retrieve that info).
> > > >
> > > > I currently solve it by using a custom rowHandler in my DAO as follow:
> > > >
> > > >     public Map getItemNames () throws DataAccessException {
> > > >         final KeyValueHandler rowHandler = new KeyValueHandler ();
> > > >         getSqlMapClientTemplate().queryWithRowHandler("getItemNameMap", null, rowHandler);
> > > >         return rowHandler.getMap();
> > > >     }
> > > >
> > > >     private class KeyValueHandler implements RowHandler {
> > > >         final Map map = new HashMap ();
> > > >
> > > >         public void handleRow(Object valueObject) {
> > > >             final Map row = (Map) valueObject;
> > > >             map.put (row.get("SKU"), row.get("Description"));
> > > >         }
> > > >
> > > >         public Map getMap () {
> > > >             return map;
> > > >         }
> > > >
> > > >     }
> > > >
> > > > But I would like to move possibly that code out of my DAO code and into
> > > > iBatis SqlMap file.
> > > >
> > > > How could I do that
> > > >
> > > > TIA
> > > >
> > > > Pascal
> > > >
> > > >
> >
> 
>

Re: java.util.HashMap as resultClass

Posted by Pascal DeMilly <li...@newgenesys.com>.
Actually I solved it this way:

in my DAO code, I use a generic, ID and Name for key/value pair. Then in
my iBatis SQLMap I used aliases to rename my columns to what my DAO
expect.

<select id="..." resultClass="java.util.HashMap">
	select SKU is ID, Description as Name from Items
</select>

Now my DAO code is free again from Knowing how the tables are built.

Regards

Pascal

On Mon, 2005-01-24 at 09:22, Pascal DeMilly wrote:
> Thanks Larry,
> 
> I tried it and it works. However this still leave some dependencies in
> my DAO code as I have to specify the column names there as in:
> 
> return getSqlMapClientTemplate().queryForMap("getItemNameMap", null, "SKU", "Description");
> 
> How will you do about moving that code into the SqlMap. Should I create
> a custom resultMap with key/value as properties and refer to it in my
> DAO code?
> 
> I am going to give it a try and see what happened.
> 
> Thanks for you help.
> 
> Pascal
> 
> On Mon, 2005-01-24 at 08:44, Larry Meadors wrote:
> > Try executeQueryForMap() instead.
> > 
> > 
> > On Mon, 24 Jan 2005 08:42:25 -0800, Pascal DeMilly
> > <li...@newgenesys.com> wrote:
> > > Hi,
> > > 
> > > I would like to retrieve a Map with whose key is the 1st column of my
> > > query and the value is the 2nd column. For example right now I do:
> > > 
> > > <select id="getItemNameMap" resultClass="java.util.HashMap">
> > >         select SKU, Description from Items
> > > </select>
> > > 
> > > However this returns a list of maps with the key being the column name
> > > and the value the column value which seems wasteful in term of space (I
> > > am using remoting to retrieve that info).
> > > 
> > > I currently solve it by using a custom rowHandler in my DAO as follow:
> > > 
> > >     public Map getItemNames () throws DataAccessException {
> > >         final KeyValueHandler rowHandler = new KeyValueHandler ();
> > >         getSqlMapClientTemplate().queryWithRowHandler("getItemNameMap", null, rowHandler);
> > >         return rowHandler.getMap();
> > >     }
> > > 
> > >     private class KeyValueHandler implements RowHandler {
> > >         final Map map = new HashMap ();
> > > 
> > >         public void handleRow(Object valueObject) {
> > >             final Map row = (Map) valueObject;
> > >             map.put (row.get("SKU"), row.get("Description"));
> > >         }
> > > 
> > >         public Map getMap () {
> > >             return map;
> > >         }
> > > 
> > >     }
> > > 
> > > But I would like to move possibly that code out of my DAO code and into
> > > iBatis SqlMap file.
> > > 
> > > How could I do that
> > > 
> > > TIA
> > > 
> > > Pascal
> > > 
> > >
> 


Re: java.util.HashMap as resultClass

Posted by Brice Ruth <bd...@gmail.com>.
For the record, this recommendation is far better than using a
LabelValueBean in iBATIS SqlMaps ... I still do it, because its easy,
but Clinton's suggestion is far superior :)


On Mon, 24 Jan 2005 14:04:42 -0700, Clinton Begin
<cl...@gmail.com> wrote:
> Pascal,
> 
> Another approach is to use a wrapper class at your presentation layer.
>  After all, this is a presentation layer problem, that is probably
> best solved in the presentation layer -- not in the persistence layer.
> 
> By aliasing your columns to ID and Value, you've made a change to your
> persistence for the sake of a presentation requirement.
> 
> Here's my recommendation:
> 
> 1) Use a real domain model.  Avoid Maps.  Create a real class that
> represents your domain.  In this case it looks like a Product class or
> something (with properties such as  SKU, Description etc.)
> 
> 2) Map that class with iBATIS properly, using a real parameter class
> and result class, with columns mapped to the JavaBean properties.
> 
> 3) Use that domain object throughout your application.  When a
> requirement such as a droplist manifests itself, use a wrapper class
> to manage special behavior.  For example, given a Product class, you
> could have a ProductDropListItem that knows how to provide the
> appropriate identifier and value to the drop list.
> 
> Don't worry about having to make a matching DropListItem for each
> class.  It's very easy to make a generic one that will work with any
> domain class (passed into the constructor).  E.g. new DropListItem
> (aProduct).
> 
> This keeps presentation layer problems out of the persistence layer.
> It's also pretty easy to implement.
> 
> Cheers,
> Clinton
> 
> On Mon, 24 Jan 2005 12:44:27 -0800 (PST), Prashanth Sukumaran
> <pr...@yahoo.com> wrote:
> > Hi Pascal,
> >
> > I think i understand what Brandon is trying to say. He is talking about using the right pattern
> > for the kind of job you are trying to achieve.
> >
> > If my guess is right. Looks like you trying to do this for your dropdowns.  I do it this way and i
> > feel it is much cleaner this way.
> >
> > public interface ValueObject{
> >
> >     /**
> >      * Gets the iD attribute of the KeyValueObject object
> >      *
> >      * @return   The iD value
> >      */
> >     String getID();
> >
> >     /**
> >      * Gets the value attribute of the KeyValueObject object
> >      *
> >      * @return   The value value
> >      */
> >     String getValue();
> >
> >   /**
> >    * This method returns sort field
> >    *
> >    *
> >    * @return value of sort field
> >      */
> >     public Object getSortFieldValue();
> > }
> >
> > All Objects that you are using for Dropdowns will extend the ValueObject, and they will implement
> > the three methods.  In your case getID() will return SKU, getValue will return Description.  and
> > for the getSortFieldValue() have an empty implementation.  This way the bean can be used for other
> > things and also used for dropdowns.
> >
> > For the dropdowns have a common method that handles them or even struts has the LabelValue bean.
> >
> > Rgds
> > Prashanth.
> >
> >
> > --- Pascal DeMilly <li...@newgenesys.com> wrote:
> >
> > > Well. It seems logical to me. Why would iBatis go into great length at
> > > trying to map result in the SqlMap file then if it was not to limit the
> > > knowledge of your database to that XML file. The dependency have to stop
> > > somewhere and it seems to me the SqlMap file is the place where it
> > > should stop. The DAO implementation classes should only know about the
> > > iBatis map id and its expected POJO results not how your database
> > > columns are named or it seems to me.
> > >
> > > Anyway, in my case while this solve one of my problem, queryForMap is
> > > still not as efficient as it could be since it relies on queryForList.
> > > So basically it is building two list. One with each element being a Map
> > > of column name/value, then another map of only values. Would it be more
> > > efficient to rewrite queryForMap to use a RowHandler?
> > >
> > >
> > >
> > > On Mon, 2005-01-24 at 10:15, Brandon Goodin wrote:
> > > > Why is it a problem to have a code dependency in an "implementation"
> > > > class? It is suppossed to be aware of IBatis SQLMap semantics. You
> > > > interface over your DAO class should hide the specifics of your
> > > > implementation code within your Dao.
> > > >
> > > > Brandon
> > > >
> > > >
> > > >
> > > >
> > > > On Mon, 24 Jan 2005 09:22:49 -0800, Pascal DeMilly
> > > > <li...@newgenesys.com> wrote:
> > > > > Thanks Larry,
> > > > >
> > > > > I tried it and it works. However this still leave some dependencies in
> > > > > my DAO code as I have to specify the column names there as in:
> > > > >
> > > > > return getSqlMapClientTemplate().queryForMap("getItemNameMap", null, "SKU", "Description");
> > > > >
> > > > > How will you do about moving that code into the SqlMap. Should I create
> > > > > a custom resultMap with key/value as properties and refer to it in my
> > > > > DAO code?
> > > > >
> > > > > I am going to give it a try and see what happened.
> > > > >
> > > > > Thanks for you help.
> > > > >
> > > > > Pascal
> > > > >
> > > > > On Mon, 2005-01-24 at 08:44, Larry Meadors wrote:
> > > > > > Try executeQueryForMap() instead.
> > > > > >
> > > > > >
> > > > > > On Mon, 24 Jan 2005 08:42:25 -0800, Pascal DeMilly
> > > > > > <li...@newgenesys.com> wrote:
> > > > > > > Hi,
> > > > > > >
> > > > > > > I would like to retrieve a Map with whose key is the 1st column of my
> > > > > > > query and the value is the 2nd column. For example right now I do:
> > > > > > >
> > > > > > > <select id="getItemNameMap" resultClass="java.util.HashMap">
> > > > > > >         select SKU, Description from Items
> > > > > > > </select>
> > > > > > >
> > > > > > > However this returns a list of maps with the key being the column name
> > > > > > > and the value the column value which seems wasteful in term of space (I
> > > > > > > am using remoting to retrieve that info).
> > > > > > >
> > > > > > > I currently solve it by using a custom rowHandler in my DAO as follow:
> > > > > > >
> > > > > > >     public Map getItemNames () throws DataAccessException {
> > > > > > >         final KeyValueHandler rowHandler = new KeyValueHandler ();
> > > > > > >         getSqlMapClientTemplate().queryWithRowHandler("getItemNameMap", null,
> > > rowHandler);
> > > > > > >         return rowHandler.getMap();
> > > > > > >     }
> > > > > > >
> > > > > > >     private class KeyValueHandler implements RowHandler {
> > > > > > >         final Map map = new HashMap ();
> > > > > > >
> > > > > > >         public void handleRow(Object valueObject) {
> > > > > > >             final Map row = (Map) valueObject;
> > > > > > >             map.put (row.get("SKU"), row.get("Description"));
> > > > > > >         }
> > > > > > >
> > > > > > >         public Map getMap () {
> > > > > > >             return map;
> > > > > > >         }
> > > > > > >
> > > > > > >     }
> > > > > > >
> > > > > > > But I would like to move possibly that code out of my DAO code and into
> > > > > > > iBatis SqlMap file.
> > > > > > >
> > > > > > > How could I do that
> > > > > > >
> > > > > > > TIA
> > > > > > >
> > > > > > > Pascal
> > > > > > >
> > > > > > >
> > > > >
> > > > >
> > >
> > >
> >
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Tired of spam?  Yahoo! Mail has the best spam protection around
> > http://mail.yahoo.com
> >
>

Re: java.util.HashMap as resultClass

Posted by Clinton Begin <cl...@gmail.com>.
Pascal,

Another approach is to use a wrapper class at your presentation layer.
 After all, this is a presentation layer problem, that is probably
best solved in the presentation layer -- not in the persistence layer.

By aliasing your columns to ID and Value, you've made a change to your
persistence for the sake of a presentation requirement.

Here's my recommendation:

1) Use a real domain model.  Avoid Maps.  Create a real class that
represents your domain.  In this case it looks like a Product class or
something (with properties such as  SKU, Description etc.)

2) Map that class with iBATIS properly, using a real parameter class
and result class, with columns mapped to the JavaBean properties.

3) Use that domain object throughout your application.  When a
requirement such as a droplist manifests itself, use a wrapper class
to manage special behavior.  For example, given a Product class, you
could have a ProductDropListItem that knows how to provide the
appropriate identifier and value to the drop list.

Don't worry about having to make a matching DropListItem for each
class.  It's very easy to make a generic one that will work with any
domain class (passed into the constructor).  E.g. new DropListItem
(aProduct).

This keeps presentation layer problems out of the persistence layer. 
It's also pretty easy to implement.

Cheers,
Clinton  

On Mon, 24 Jan 2005 12:44:27 -0800 (PST), Prashanth Sukumaran
<pr...@yahoo.com> wrote:
> Hi Pascal,
> 
> I think i understand what Brandon is trying to say. He is talking about using the right pattern
> for the kind of job you are trying to achieve.
> 
> If my guess is right. Looks like you trying to do this for your dropdowns.  I do it this way and i
> feel it is much cleaner this way.
> 
> public interface ValueObject{
> 
>     /**
>      * Gets the iD attribute of the KeyValueObject object
>      *
>      * @return   The iD value
>      */
>     String getID();
> 
>     /**
>      * Gets the value attribute of the KeyValueObject object
>      *
>      * @return   The value value
>      */
>     String getValue();
> 
>   /**
>    * This method returns sort field
>    *
>    *
>    * @return value of sort field
>      */
>     public Object getSortFieldValue();
> }
> 
> All Objects that you are using for Dropdowns will extend the ValueObject, and they will implement
> the three methods.  In your case getID() will return SKU, getValue will return Description.  and
> for the getSortFieldValue() have an empty implementation.  This way the bean can be used for other
> things and also used for dropdowns.
> 
> For the dropdowns have a common method that handles them or even struts has the LabelValue bean.
> 
> Rgds
> Prashanth.
> 
> 
> --- Pascal DeMilly <li...@newgenesys.com> wrote:
> 
> > Well. It seems logical to me. Why would iBatis go into great length at
> > trying to map result in the SqlMap file then if it was not to limit the
> > knowledge of your database to that XML file. The dependency have to stop
> > somewhere and it seems to me the SqlMap file is the place where it
> > should stop. The DAO implementation classes should only know about the
> > iBatis map id and its expected POJO results not how your database
> > columns are named or it seems to me.
> >
> > Anyway, in my case while this solve one of my problem, queryForMap is
> > still not as efficient as it could be since it relies on queryForList.
> > So basically it is building two list. One with each element being a Map
> > of column name/value, then another map of only values. Would it be more
> > efficient to rewrite queryForMap to use a RowHandler?
> >
> >
> >
> > On Mon, 2005-01-24 at 10:15, Brandon Goodin wrote:
> > > Why is it a problem to have a code dependency in an "implementation"
> > > class? It is suppossed to be aware of IBatis SQLMap semantics. You
> > > interface over your DAO class should hide the specifics of your
> > > implementation code within your Dao.
> > >
> > > Brandon
> > >
> > >
> > >
> > >
> > > On Mon, 24 Jan 2005 09:22:49 -0800, Pascal DeMilly
> > > <li...@newgenesys.com> wrote:
> > > > Thanks Larry,
> > > >
> > > > I tried it and it works. However this still leave some dependencies in
> > > > my DAO code as I have to specify the column names there as in:
> > > >
> > > > return getSqlMapClientTemplate().queryForMap("getItemNameMap", null, "SKU", "Description");
> > > >
> > > > How will you do about moving that code into the SqlMap. Should I create
> > > > a custom resultMap with key/value as properties and refer to it in my
> > > > DAO code?
> > > >
> > > > I am going to give it a try and see what happened.
> > > >
> > > > Thanks for you help.
> > > >
> > > > Pascal
> > > >
> > > > On Mon, 2005-01-24 at 08:44, Larry Meadors wrote:
> > > > > Try executeQueryForMap() instead.
> > > > >
> > > > >
> > > > > On Mon, 24 Jan 2005 08:42:25 -0800, Pascal DeMilly
> > > > > <li...@newgenesys.com> wrote:
> > > > > > Hi,
> > > > > >
> > > > > > I would like to retrieve a Map with whose key is the 1st column of my
> > > > > > query and the value is the 2nd column. For example right now I do:
> > > > > >
> > > > > > <select id="getItemNameMap" resultClass="java.util.HashMap">
> > > > > >         select SKU, Description from Items
> > > > > > </select>
> > > > > >
> > > > > > However this returns a list of maps with the key being the column name
> > > > > > and the value the column value which seems wasteful in term of space (I
> > > > > > am using remoting to retrieve that info).
> > > > > >
> > > > > > I currently solve it by using a custom rowHandler in my DAO as follow:
> > > > > >
> > > > > >     public Map getItemNames () throws DataAccessException {
> > > > > >         final KeyValueHandler rowHandler = new KeyValueHandler ();
> > > > > >         getSqlMapClientTemplate().queryWithRowHandler("getItemNameMap", null,
> > rowHandler);
> > > > > >         return rowHandler.getMap();
> > > > > >     }
> > > > > >
> > > > > >     private class KeyValueHandler implements RowHandler {
> > > > > >         final Map map = new HashMap ();
> > > > > >
> > > > > >         public void handleRow(Object valueObject) {
> > > > > >             final Map row = (Map) valueObject;
> > > > > >             map.put (row.get("SKU"), row.get("Description"));
> > > > > >         }
> > > > > >
> > > > > >         public Map getMap () {
> > > > > >             return map;
> > > > > >         }
> > > > > >
> > > > > >     }
> > > > > >
> > > > > > But I would like to move possibly that code out of my DAO code and into
> > > > > > iBatis SqlMap file.
> > > > > >
> > > > > > How could I do that
> > > > > >
> > > > > > TIA
> > > > > >
> > > > > > Pascal
> > > > > >
> > > > > >
> > > >
> > > >
> >
> >
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>

Re: java.util.HashMap as resultClass

Posted by Brice Ruth <bd...@gmail.com>.
The Struts LabelValue bean is quite nice (and simple), though to use
it with iBATIS, I needed to created my own implementation that extends
the standard LabelValue bean, since what ships w/ Struts doesn't have
a default constructor (and hence is not useable with reflection). I
just extend the LabelValue bean and create a no arg constructor that
calls the (string,string) constructor with empty strings - then iBATIS
does the rest.

Using Struts html:optionsCollection is dead-simple then, just provide
it the property that contains the List populated from the iBATIS
queryForList(...) call.

By the way, I've recently really started using the common-beanutils
from Apache, they really are invaluable when working with an MVC
pattern, particularly Struts where you often have ActionForms,
DynaActionForms (map backed), etc. that you need to pull properties
out of and re-populate other beans to pass on to iBATIS or other
layers. Works great :)

Cheers!
Brice


On Mon, 24 Jan 2005 12:44:27 -0800 (PST), Prashanth Sukumaran
<pr...@yahoo.com> wrote:
> Hi Pascal,
> 
> I think i understand what Brandon is trying to say. He is talking about using the right pattern
> for the kind of job you are trying to achieve.
> 
> If my guess is right. Looks like you trying to do this for your dropdowns.  I do it this way and i
> feel it is much cleaner this way.
> 
> public interface ValueObject{
> 
>     /**
>      * Gets the iD attribute of the KeyValueObject object
>      *
>      * @return   The iD value
>      */
>     String getID();
> 
>     /**
>      * Gets the value attribute of the KeyValueObject object
>      *
>      * @return   The value value
>      */
>     String getValue();
> 
>   /**
>    * This method returns sort field
>    *
>    *
>    * @return value of sort field
>      */
>     public Object getSortFieldValue();
> }
> 
> All Objects that you are using for Dropdowns will extend the ValueObject, and they will implement
> the three methods.  In your case getID() will return SKU, getValue will return Description.  and
> for the getSortFieldValue() have an empty implementation.  This way the bean can be used for other
> things and also used for dropdowns.
> 
> For the dropdowns have a common method that handles them or even struts has the LabelValue bean.
> 
> Rgds
> Prashanth.
> 
> 
> --- Pascal DeMilly <li...@newgenesys.com> wrote:
> 
> > Well. It seems logical to me. Why would iBatis go into great length at
> > trying to map result in the SqlMap file then if it was not to limit the
> > knowledge of your database to that XML file. The dependency have to stop
> > somewhere and it seems to me the SqlMap file is the place where it
> > should stop. The DAO implementation classes should only know about the
> > iBatis map id and its expected POJO results not how your database
> > columns are named or it seems to me.
> >
> > Anyway, in my case while this solve one of my problem, queryForMap is
> > still not as efficient as it could be since it relies on queryForList.
> > So basically it is building two list. One with each element being a Map
> > of column name/value, then another map of only values. Would it be more
> > efficient to rewrite queryForMap to use a RowHandler?
> >
> >
> >
> > On Mon, 2005-01-24 at 10:15, Brandon Goodin wrote:
> > > Why is it a problem to have a code dependency in an "implementation"
> > > class? It is suppossed to be aware of IBatis SQLMap semantics. You
> > > interface over your DAO class should hide the specifics of your
> > > implementation code within your Dao.
> > >
> > > Brandon
> > >
> > >
> > >
> > >
> > > On Mon, 24 Jan 2005 09:22:49 -0800, Pascal DeMilly
> > > <li...@newgenesys.com> wrote:
> > > > Thanks Larry,
> > > >
> > > > I tried it and it works. However this still leave some dependencies in
> > > > my DAO code as I have to specify the column names there as in:
> > > >
> > > > return getSqlMapClientTemplate().queryForMap("getItemNameMap", null, "SKU", "Description");
> > > >
> > > > How will you do about moving that code into the SqlMap. Should I create
> > > > a custom resultMap with key/value as properties and refer to it in my
> > > > DAO code?
> > > >
> > > > I am going to give it a try and see what happened.
> > > >
> > > > Thanks for you help.
> > > >
> > > > Pascal
> > > >
> > > > On Mon, 2005-01-24 at 08:44, Larry Meadors wrote:
> > > > > Try executeQueryForMap() instead.
> > > > >
> > > > >
> > > > > On Mon, 24 Jan 2005 08:42:25 -0800, Pascal DeMilly
> > > > > <li...@newgenesys.com> wrote:
> > > > > > Hi,
> > > > > >
> > > > > > I would like to retrieve a Map with whose key is the 1st column of my
> > > > > > query and the value is the 2nd column. For example right now I do:
> > > > > >
> > > > > > <select id="getItemNameMap" resultClass="java.util.HashMap">
> > > > > >         select SKU, Description from Items
> > > > > > </select>
> > > > > >
> > > > > > However this returns a list of maps with the key being the column name
> > > > > > and the value the column value which seems wasteful in term of space (I
> > > > > > am using remoting to retrieve that info).
> > > > > >
> > > > > > I currently solve it by using a custom rowHandler in my DAO as follow:
> > > > > >
> > > > > >     public Map getItemNames () throws DataAccessException {
> > > > > >         final KeyValueHandler rowHandler = new KeyValueHandler ();
> > > > > >         getSqlMapClientTemplate().queryWithRowHandler("getItemNameMap", null,
> > rowHandler);
> > > > > >         return rowHandler.getMap();
> > > > > >     }
> > > > > >
> > > > > >     private class KeyValueHandler implements RowHandler {
> > > > > >         final Map map = new HashMap ();
> > > > > >
> > > > > >         public void handleRow(Object valueObject) {
> > > > > >             final Map row = (Map) valueObject;
> > > > > >             map.put (row.get("SKU"), row.get("Description"));
> > > > > >         }
> > > > > >
> > > > > >         public Map getMap () {
> > > > > >             return map;
> > > > > >         }
> > > > > >
> > > > > >     }
> > > > > >
> > > > > > But I would like to move possibly that code out of my DAO code and into
> > > > > > iBatis SqlMap file.
> > > > > >
> > > > > > How could I do that
> > > > > >
> > > > > > TIA
> > > > > >
> > > > > > Pascal
> > > > > >
> > > > > >
> > > >
> > > >
> >
> >
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>

Re: java.util.HashMap as resultClass

Posted by Pascal DeMilly <li...@newgenesys.com>.
Here is the way I finally wrote t using iBatis resultMap

>From my DAO:

return Map getItemNames () {
   return getSqlMapClientTemplate().queryForMap("getItemNameMap", null, "key", "value");	
}

my SqlMap has now a resultMap:

<resultMap id=map-item-name" class="java.util.HashMap">
   <result property="key" column="SKU"/>
   <result property="value" column="Description"/>
</resultMap>

<select id="getItemNameMap" resultMap="map-item-name">
   select SKU, Description from Items
</select>

Comments are welcome and appreciated

Regards

Pascal

On Mon, 2005-01-24 at 13:46, Pascal DeMilly wrote:
> Prashanth.
> 
> This is exactly what and why I am implementing it. Basically since my
> application would be remote to my business and DAO layer, I need to
> simplify the exchange. Moving the whole set of POJO just to get the ID
> and name doesn't seem efficient. My choice of keywords was certainly the
> best. I tried using key/value (I used to be a Smalltalk developer) but
> SQL didn't like that. I was going to use MapKey and MapValue as a
> replacement.
> 
> Contrary to Clinton remarks, I don't think I have brought my
> presentation layer into my persistance layer, since the keywords
> (ID=>name or MayKey,MapValue) are only a contract between the DAO and
> the SqlMap.
> 
> When it gets to the presentation layer or actually even the business
> logic layer, it is just a Map of object key/value with no reference to
> the key/value keyword used in the DAO layer.
> 
> On Mon, 2005-01-24 at 12:44, Prashanth Sukumaran wrote: 
> > Hi Pascal,
> > 
> > I think i understand what Brandon is trying to say. He is talking about using the right pattern
> > for the kind of job you are trying to achieve.
> > 
> > If my guess is right. Looks like you trying to do this for your dropdowns.  I do it this way and i
> > feel it is much cleaner this way.
> > 
> > 
> > public interface ValueObject{
> > 
> >     /**
> >      * Gets the iD attribute of the KeyValueObject object
> >      *
> >      * @return   The iD value
> >      */
> >     String getID();
> > 
> >     /**
> >      * Gets the value attribute of the KeyValueObject object
> >      *
> >      * @return   The value value
> >      */
> >     String getValue();
> > 
> >   /**
> >    * This method returns sort field
> >    *
> >    *
> >    * @return value of sort field
> >      */
> >     public Object getSortFieldValue();
> > }
> > 
> > All Objects that you are using for Dropdowns will extend the ValueObject, and they will implement
> > the three methods.  In your case getID() will return SKU, getValue will return Description.  and
> > for the getSortFieldValue() have an empty implementation.  This way the bean can be used for other
> > things and also used for dropdowns.
> > 
> > For the dropdowns have a common method that handles them or even struts has the LabelValue bean.
> > 
> > Rgds
> > Prashanth.
> > 
> > 
> > --- Pascal DeMilly <li...@newgenesys.com> wrote:
> > 
> > > Well. It seems logical to me. Why would iBatis go into great length at
> > > trying to map result in the SqlMap file then if it was not to limit the
> > > knowledge of your database to that XML file. The dependency have to stop
> > > somewhere and it seems to me the SqlMap file is the place where it
> > > should stop. The DAO implementation classes should only know about the
> > > iBatis map id and its expected POJO results not how your database
> > > columns are named or it seems to me.
> > > 
> > > Anyway, in my case while this solve one of my problem, queryForMap is
> > > still not as efficient as it could be since it relies on queryForList.
> > > So basically it is building two list. One with each element being a Map
> > > of column name/value, then another map of only values. Would it be more
> > > efficient to rewrite queryForMap to use a RowHandler?
> > > 
> > > 
> > > 
> > > On Mon, 2005-01-24 at 10:15, Brandon Goodin wrote:
> > > > Why is it a problem to have a code dependency in an "implementation"
> > > > class? It is suppossed to be aware of IBatis SQLMap semantics. You
> > > > interface over your DAO class should hide the specifics of your
> > > > implementation code within your Dao.
> > > > 
> > > > Brandon
> > > > 
> > > > 
> > > > 
> > > > 
> > > > On Mon, 24 Jan 2005 09:22:49 -0800, Pascal DeMilly
> > > > <li...@newgenesys.com> wrote:
> > > > > Thanks Larry,
> > > > > 
> > > > > I tried it and it works. However this still leave some dependencies in
> > > > > my DAO code as I have to specify the column names there as in:
> > > > > 
> > > > > return getSqlMapClientTemplate().queryForMap("getItemNameMap", null, "SKU", "Description");
> > > > > 
> > > > > How will you do about moving that code into the SqlMap. Should I create
> > > > > a custom resultMap with key/value as properties and refer to it in my
> > > > > DAO code?
> > > > > 
> > > > > I am going to give it a try and see what happened.
> > > > > 
> > > > > Thanks for you help.
> > > > > 
> > > > > Pascal
> > > > > 
> > > > > On Mon, 2005-01-24 at 08:44, Larry Meadors wrote:
> > > > > > Try executeQueryForMap() instead.
> > > > > >
> > > > > >
> > > > > > On Mon, 24 Jan 2005 08:42:25 -0800, Pascal DeMilly
> > > > > > <li...@newgenesys.com> wrote:
> > > > > > > Hi,
> > > > > > >
> > > > > > > I would like to retrieve a Map with whose key is the 1st column of my
> > > > > > > query and the value is the 2nd column. For example right now I do:
> > > > > > >
> > > > > > > <select id="getItemNameMap" resultClass="java.util.HashMap">
> > > > > > >         select SKU, Description from Items
> > > > > > > </select>
> > > > > > >
> > > > > > > However this returns a list of maps with the key being the column name
> > > > > > > and the value the column value which seems wasteful in term of space (I
> > > > > > > am using remoting to retrieve that info).
> > > > > > >
> > > > > > > I currently solve it by using a custom rowHandler in my DAO as follow:
> > > > > > >
> > > > > > >     public Map getItemNames () throws DataAccessException {
> > > > > > >         final KeyValueHandler rowHandler = new KeyValueHandler ();
> > > > > > >         getSqlMapClientTemplate().queryWithRowHandler("getItemNameMap", null,
> > > rowHandler);
> > > > > > >         return rowHandler.getMap();
> > > > > > >     }
> > > > > > >
> > > > > > >     private class KeyValueHandler implements RowHandler {
> > > > > > >         final Map map = new HashMap ();
> > > > > > >
> > > > > > >         public void handleRow(Object valueObject) {
> > > > > > >             final Map row = (Map) valueObject;
> > > > > > >             map.put (row.get("SKU"), row.get("Description"));
> > > > > > >         }
> > > > > > >
> > > > > > >         public Map getMap () {
> > > > > > >             return map;
> > > > > > >         }
> > > > > > >
> > > > > > >     }
> > > > > > >
> > > > > > > But I would like to move possibly that code out of my DAO code and into
> > > > > > > iBatis SqlMap file.
> > > > > > >
> > > > > > > How could I do that
> > > > > > >
> > > > > > > TIA
> > > > > > >
> > > > > > > Pascal
> > > > > > >
> > > > > > >
> > > > > 
> > > > >
> > > 
> > > 
> > 
> > 
> > __________________________________________________
> > Do You Yahoo!?
> > Tired of spam?  Yahoo! Mail has the best spam protection around 
> > http://mail.yahoo.com
> 


Re: Simple example of N+1 selects

Posted by Kris Jenkins <kr...@yahoo.co.uk>.
Magnus Stattin wrote:

>Hi
>
>How would I be able to avoid the N+1 selects if I have for example a
>departments table and a table with employees. All employees belongs to a
>department and I would like to get a list of department valueObjects where
>each departmentVo contains a collection of employee valueObjects. 
>
>Is it possible to do this with a single select with Ibatis and how would the
>Sqlmap look like. I am looking for a simple example to get me started.
>
>Thank you 
>
>Magnus
>  
>
Take a look at this wiki FAQ:

http://wiki.apache.org/ibatis/How_20do_20I_20get_20around_20the_20n_2b1_20selects_20problem_3f

Kris



-- 
Kris Jenkins
Email:  kris@jenkster.com
Blog:   http://cafe.jenkster.com/
Wiki:   http://wiki.jenkster.com/


Simple example of N+1 selects

Posted by Magnus Stattin <ma...@cue.se>.
Hi

How would I be able to avoid the N+1 selects if I have for example a
departments table and a table with employees. All employees belongs to a
department and I would like to get a list of department valueObjects where
each departmentVo contains a collection of employee valueObjects. 

Is it possible to do this with a single select with Ibatis and how would the
Sqlmap look like. I am looking for a simple example to get me started.

Thank you 

Magnus




Re: java.util.HashMap as resultClass

Posted by Clinton Begin <cl...@gmail.com>.
Final point:

Most any POJO that I can imagine is cheaper to serialize than any
typical Map implementation.  This stands to reason simply based on
that a serialized Map must contain both the metadata and the data...a
serialized POJO (JavaBean) only needs to save the data.

Cheers,
Clinton


On Mon, 24 Jan 2005 13:46:52 -0800, Pascal DeMilly
<li...@newgenesys.com> wrote:
> Prashanth.
> 
> This is exactly what and why I am implementing it. Basically since my
> application would be remote to my business and DAO layer, I need to
> simplify the exchange. Moving the whole set of POJO just to get the ID
> and name doesn't seem efficient. My choice of keywords was certainly the
> best. I tried using key/value (I used to be a Smalltalk developer) but
> SQL didn't like that. I was going to use MapKey and MapValue as a
> replacement.
> 
> Contrary to Clinton remarks, I don't think I have brought my
> presentation layer into my persistance layer, since the keywords
> (ID=>name or MayKey,MapValue) are only a contract between the DAO and
> the SqlMap.
> 
> When it gets to the presentation layer or actually even the business
> logic layer, it is just a Map of object key/value with no reference to
> the key/value keyword used in the DAO layer.
> 
> On Mon, 2005-01-24 at 12:44, Prashanth Sukumaran wrote:
> > Hi Pascal,
> >
> > I think i understand what Brandon is trying to say. He is talking about using the right pattern
> > for the kind of job you are trying to achieve.
> >
> > If my guess is right. Looks like you trying to do this for your dropdowns.  I do it this way and i
> > feel it is much cleaner this way.
> >
> >
> > public interface ValueObject{
> >
> >     /**
> >      * Gets the iD attribute of the KeyValueObject object
> >      *
> >      * @return   The iD value
> >      */
> >     String getID();
> >
> >     /**
> >      * Gets the value attribute of the KeyValueObject object
> >      *
> >      * @return   The value value
> >      */
> >     String getValue();
> >
> >   /**
> >    * This method returns sort field
> >    *
> >    *
> >    * @return value of sort field
> >      */
> >     public Object getSortFieldValue();
> > }
> >
> > All Objects that you are using for Dropdowns will extend the ValueObject, and they will implement
> > the three methods.  In your case getID() will return SKU, getValue will return Description.  and
> > for the getSortFieldValue() have an empty implementation.  This way the bean can be used for other
> > things and also used for dropdowns.
> >
> > For the dropdowns have a common method that handles them or even struts has the LabelValue bean.
> >
> > Rgds
> > Prashanth.
> >
> >
> > --- Pascal DeMilly <li...@newgenesys.com> wrote:
> >
> > > Well. It seems logical to me. Why would iBatis go into great length at
> > > trying to map result in the SqlMap file then if it was not to limit the
> > > knowledge of your database to that XML file. The dependency have to stop
> > > somewhere and it seems to me the SqlMap file is the place where it
> > > should stop. The DAO implementation classes should only know about the
> > > iBatis map id and its expected POJO results not how your database
> > > columns are named or it seems to me.
> > >
> > > Anyway, in my case while this solve one of my problem, queryForMap is
> > > still not as efficient as it could be since it relies on queryForList.
> > > So basically it is building two list. One with each element being a Map
> > > of column name/value, then another map of only values. Would it be more
> > > efficient to rewrite queryForMap to use a RowHandler?
> > >
> > >
> > >
> > > On Mon, 2005-01-24 at 10:15, Brandon Goodin wrote:
> > > > Why is it a problem to have a code dependency in an "implementation"
> > > > class? It is suppossed to be aware of IBatis SQLMap semantics. You
> > > > interface over your DAO class should hide the specifics of your
> > > > implementation code within your Dao.
> > > >
> > > > Brandon
> > > >
> > > >
> > > >
> > > >
> > > > On Mon, 24 Jan 2005 09:22:49 -0800, Pascal DeMilly
> > > > <li...@newgenesys.com> wrote:
> > > > > Thanks Larry,
> > > > >
> > > > > I tried it and it works. However this still leave some dependencies in
> > > > > my DAO code as I have to specify the column names there as in:
> > > > >
> > > > > return getSqlMapClientTemplate().queryForMap("getItemNameMap", null, "SKU", "Description");
> > > > >
> > > > > How will you do about moving that code into the SqlMap. Should I create
> > > > > a custom resultMap with key/value as properties and refer to it in my
> > > > > DAO code?
> > > > >
> > > > > I am going to give it a try and see what happened.
> > > > >
> > > > > Thanks for you help.
> > > > >
> > > > > Pascal
> > > > >
> > > > > On Mon, 2005-01-24 at 08:44, Larry Meadors wrote:
> > > > > > Try executeQueryForMap() instead.
> > > > > >
> > > > > >
> > > > > > On Mon, 24 Jan 2005 08:42:25 -0800, Pascal DeMilly
> > > > > > <li...@newgenesys.com> wrote:
> > > > > > > Hi,
> > > > > > >
> > > > > > > I would like to retrieve a Map with whose key is the 1st column of my
> > > > > > > query and the value is the 2nd column. For example right now I do:
> > > > > > >
> > > > > > > <select id="getItemNameMap" resultClass="java.util.HashMap">
> > > > > > >         select SKU, Description from Items
> > > > > > > </select>
> > > > > > >
> > > > > > > However this returns a list of maps with the key being the column name
> > > > > > > and the value the column value which seems wasteful in term of space (I
> > > > > > > am using remoting to retrieve that info).
> > > > > > >
> > > > > > > I currently solve it by using a custom rowHandler in my DAO as follow:
> > > > > > >
> > > > > > >     public Map getItemNames () throws DataAccessException {
> > > > > > >         final KeyValueHandler rowHandler = new KeyValueHandler ();
> > > > > > >         getSqlMapClientTemplate().queryWithRowHandler("getItemNameMap", null,
> > > rowHandler);
> > > > > > >         return rowHandler.getMap();
> > > > > > >     }
> > > > > > >
> > > > > > >     private class KeyValueHandler implements RowHandler {
> > > > > > >         final Map map = new HashMap ();
> > > > > > >
> > > > > > >         public void handleRow(Object valueObject) {
> > > > > > >             final Map row = (Map) valueObject;
> > > > > > >             map.put (row.get("SKU"), row.get("Description"));
> > > > > > >         }
> > > > > > >
> > > > > > >         public Map getMap () {
> > > > > > >             return map;
> > > > > > >         }
> > > > > > >
> > > > > > >     }
> > > > > > >
> > > > > > > But I would like to move possibly that code out of my DAO code and into
> > > > > > > iBatis SqlMap file.
> > > > > > >
> > > > > > > How could I do that
> > > > > > >
> > > > > > > TIA
> > > > > > >
> > > > > > > Pascal
> > > > > > >
> > > > > > >
> > > > >
> > > > >
> > >
> > >
> >
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Tired of spam?  Yahoo! Mail has the best spam protection around
> > http://mail.yahoo.com
> 
>

Re: java.util.HashMap as resultClass

Posted by Pascal DeMilly <li...@newgenesys.com>.
Prashanth.

This is exactly what and why I am implementing it. Basically since my
application would be remote to my business and DAO layer, I need to
simplify the exchange. Moving the whole set of POJO just to get the ID
and name doesn't seem efficient. My choice of keywords was certainly the
best. I tried using key/value (I used to be a Smalltalk developer) but
SQL didn't like that. I was going to use MapKey and MapValue as a
replacement.

Contrary to Clinton remarks, I don't think I have brought my
presentation layer into my persistance layer, since the keywords
(ID=>name or MayKey,MapValue) are only a contract between the DAO and
the SqlMap.

When it gets to the presentation layer or actually even the business
logic layer, it is just a Map of object key/value with no reference to
the key/value keyword used in the DAO layer.

On Mon, 2005-01-24 at 12:44, Prashanth Sukumaran wrote: 
> Hi Pascal,
> 
> I think i understand what Brandon is trying to say. He is talking about using the right pattern
> for the kind of job you are trying to achieve.
> 
> If my guess is right. Looks like you trying to do this for your dropdowns.  I do it this way and i
> feel it is much cleaner this way.
> 
> 
> public interface ValueObject{
> 
>     /**
>      * Gets the iD attribute of the KeyValueObject object
>      *
>      * @return   The iD value
>      */
>     String getID();
> 
>     /**
>      * Gets the value attribute of the KeyValueObject object
>      *
>      * @return   The value value
>      */
>     String getValue();
> 
>   /**
>    * This method returns sort field
>    *
>    *
>    * @return value of sort field
>      */
>     public Object getSortFieldValue();
> }
> 
> All Objects that you are using for Dropdowns will extend the ValueObject, and they will implement
> the three methods.  In your case getID() will return SKU, getValue will return Description.  and
> for the getSortFieldValue() have an empty implementation.  This way the bean can be used for other
> things and also used for dropdowns.
> 
> For the dropdowns have a common method that handles them or even struts has the LabelValue bean.
> 
> Rgds
> Prashanth.
> 
> 
> --- Pascal DeMilly <li...@newgenesys.com> wrote:
> 
> > Well. It seems logical to me. Why would iBatis go into great length at
> > trying to map result in the SqlMap file then if it was not to limit the
> > knowledge of your database to that XML file. The dependency have to stop
> > somewhere and it seems to me the SqlMap file is the place where it
> > should stop. The DAO implementation classes should only know about the
> > iBatis map id and its expected POJO results not how your database
> > columns are named or it seems to me.
> > 
> > Anyway, in my case while this solve one of my problem, queryForMap is
> > still not as efficient as it could be since it relies on queryForList.
> > So basically it is building two list. One with each element being a Map
> > of column name/value, then another map of only values. Would it be more
> > efficient to rewrite queryForMap to use a RowHandler?
> > 
> > 
> > 
> > On Mon, 2005-01-24 at 10:15, Brandon Goodin wrote:
> > > Why is it a problem to have a code dependency in an "implementation"
> > > class? It is suppossed to be aware of IBatis SQLMap semantics. You
> > > interface over your DAO class should hide the specifics of your
> > > implementation code within your Dao.
> > > 
> > > Brandon
> > > 
> > > 
> > > 
> > > 
> > > On Mon, 24 Jan 2005 09:22:49 -0800, Pascal DeMilly
> > > <li...@newgenesys.com> wrote:
> > > > Thanks Larry,
> > > > 
> > > > I tried it and it works. However this still leave some dependencies in
> > > > my DAO code as I have to specify the column names there as in:
> > > > 
> > > > return getSqlMapClientTemplate().queryForMap("getItemNameMap", null, "SKU", "Description");
> > > > 
> > > > How will you do about moving that code into the SqlMap. Should I create
> > > > a custom resultMap with key/value as properties and refer to it in my
> > > > DAO code?
> > > > 
> > > > I am going to give it a try and see what happened.
> > > > 
> > > > Thanks for you help.
> > > > 
> > > > Pascal
> > > > 
> > > > On Mon, 2005-01-24 at 08:44, Larry Meadors wrote:
> > > > > Try executeQueryForMap() instead.
> > > > >
> > > > >
> > > > > On Mon, 24 Jan 2005 08:42:25 -0800, Pascal DeMilly
> > > > > <li...@newgenesys.com> wrote:
> > > > > > Hi,
> > > > > >
> > > > > > I would like to retrieve a Map with whose key is the 1st column of my
> > > > > > query and the value is the 2nd column. For example right now I do:
> > > > > >
> > > > > > <select id="getItemNameMap" resultClass="java.util.HashMap">
> > > > > >         select SKU, Description from Items
> > > > > > </select>
> > > > > >
> > > > > > However this returns a list of maps with the key being the column name
> > > > > > and the value the column value which seems wasteful in term of space (I
> > > > > > am using remoting to retrieve that info).
> > > > > >
> > > > > > I currently solve it by using a custom rowHandler in my DAO as follow:
> > > > > >
> > > > > >     public Map getItemNames () throws DataAccessException {
> > > > > >         final KeyValueHandler rowHandler = new KeyValueHandler ();
> > > > > >         getSqlMapClientTemplate().queryWithRowHandler("getItemNameMap", null,
> > rowHandler);
> > > > > >         return rowHandler.getMap();
> > > > > >     }
> > > > > >
> > > > > >     private class KeyValueHandler implements RowHandler {
> > > > > >         final Map map = new HashMap ();
> > > > > >
> > > > > >         public void handleRow(Object valueObject) {
> > > > > >             final Map row = (Map) valueObject;
> > > > > >             map.put (row.get("SKU"), row.get("Description"));
> > > > > >         }
> > > > > >
> > > > > >         public Map getMap () {
> > > > > >             return map;
> > > > > >         }
> > > > > >
> > > > > >     }
> > > > > >
> > > > > > But I would like to move possibly that code out of my DAO code and into
> > > > > > iBatis SqlMap file.
> > > > > >
> > > > > > How could I do that
> > > > > >
> > > > > > TIA
> > > > > >
> > > > > > Pascal
> > > > > >
> > > > > >
> > > > 
> > > >
> > 
> > 
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com


Re: java.util.HashMap as resultClass

Posted by Prashanth Sukumaran <pr...@yahoo.com>.
Hi Pascal,

I think i understand what Brandon is trying to say. He is talking about using the right pattern
for the kind of job you are trying to achieve.

If my guess is right. Looks like you trying to do this for your dropdowns.  I do it this way and i
feel it is much cleaner this way.


public interface ValueObject{

    /**
     * Gets the iD attribute of the KeyValueObject object
     *
     * @return   The iD value
     */
    String getID();

    /**
     * Gets the value attribute of the KeyValueObject object
     *
     * @return   The value value
     */
    String getValue();

  /**
   * This method returns sort field
   *
   *
   * @return value of sort field
     */
    public Object getSortFieldValue();
}

All Objects that you are using for Dropdowns will extend the ValueObject, and they will implement
the three methods.  In your case getID() will return SKU, getValue will return Description.  and
for the getSortFieldValue() have an empty implementation.  This way the bean can be used for other
things and also used for dropdowns.

For the dropdowns have a common method that handles them or even struts has the LabelValue bean.

Rgds
Prashanth.


--- Pascal DeMilly <li...@newgenesys.com> wrote:

> Well. It seems logical to me. Why would iBatis go into great length at
> trying to map result in the SqlMap file then if it was not to limit the
> knowledge of your database to that XML file. The dependency have to stop
> somewhere and it seems to me the SqlMap file is the place where it
> should stop. The DAO implementation classes should only know about the
> iBatis map id and its expected POJO results not how your database
> columns are named or it seems to me.
> 
> Anyway, in my case while this solve one of my problem, queryForMap is
> still not as efficient as it could be since it relies on queryForList.
> So basically it is building two list. One with each element being a Map
> of column name/value, then another map of only values. Would it be more
> efficient to rewrite queryForMap to use a RowHandler?
> 
> 
> 
> On Mon, 2005-01-24 at 10:15, Brandon Goodin wrote:
> > Why is it a problem to have a code dependency in an "implementation"
> > class? It is suppossed to be aware of IBatis SQLMap semantics. You
> > interface over your DAO class should hide the specifics of your
> > implementation code within your Dao.
> > 
> > Brandon
> > 
> > 
> > 
> > 
> > On Mon, 24 Jan 2005 09:22:49 -0800, Pascal DeMilly
> > <li...@newgenesys.com> wrote:
> > > Thanks Larry,
> > > 
> > > I tried it and it works. However this still leave some dependencies in
> > > my DAO code as I have to specify the column names there as in:
> > > 
> > > return getSqlMapClientTemplate().queryForMap("getItemNameMap", null, "SKU", "Description");
> > > 
> > > How will you do about moving that code into the SqlMap. Should I create
> > > a custom resultMap with key/value as properties and refer to it in my
> > > DAO code?
> > > 
> > > I am going to give it a try and see what happened.
> > > 
> > > Thanks for you help.
> > > 
> > > Pascal
> > > 
> > > On Mon, 2005-01-24 at 08:44, Larry Meadors wrote:
> > > > Try executeQueryForMap() instead.
> > > >
> > > >
> > > > On Mon, 24 Jan 2005 08:42:25 -0800, Pascal DeMilly
> > > > <li...@newgenesys.com> wrote:
> > > > > Hi,
> > > > >
> > > > > I would like to retrieve a Map with whose key is the 1st column of my
> > > > > query and the value is the 2nd column. For example right now I do:
> > > > >
> > > > > <select id="getItemNameMap" resultClass="java.util.HashMap">
> > > > >         select SKU, Description from Items
> > > > > </select>
> > > > >
> > > > > However this returns a list of maps with the key being the column name
> > > > > and the value the column value which seems wasteful in term of space (I
> > > > > am using remoting to retrieve that info).
> > > > >
> > > > > I currently solve it by using a custom rowHandler in my DAO as follow:
> > > > >
> > > > >     public Map getItemNames () throws DataAccessException {
> > > > >         final KeyValueHandler rowHandler = new KeyValueHandler ();
> > > > >         getSqlMapClientTemplate().queryWithRowHandler("getItemNameMap", null,
> rowHandler);
> > > > >         return rowHandler.getMap();
> > > > >     }
> > > > >
> > > > >     private class KeyValueHandler implements RowHandler {
> > > > >         final Map map = new HashMap ();
> > > > >
> > > > >         public void handleRow(Object valueObject) {
> > > > >             final Map row = (Map) valueObject;
> > > > >             map.put (row.get("SKU"), row.get("Description"));
> > > > >         }
> > > > >
> > > > >         public Map getMap () {
> > > > >             return map;
> > > > >         }
> > > > >
> > > > >     }
> > > > >
> > > > > But I would like to move possibly that code out of my DAO code and into
> > > > > iBatis SqlMap file.
> > > > >
> > > > > How could I do that
> > > > >
> > > > > TIA
> > > > >
> > > > > Pascal
> > > > >
> > > > >
> > > 
> > >
> 
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Re: java.util.HashMap as resultClass

Posted by Pascal DeMilly <li...@newgenesys.com>.
Well. It seems logical to me. Why would iBatis go into great length at
trying to map result in the SqlMap file then if it was not to limit the
knowledge of your database to that XML file. The dependency have to stop
somewhere and it seems to me the SqlMap file is the place where it
should stop. The DAO implementation classes should only know about the
iBatis map id and its expected POJO results not how your database
columns are named or it seems to me.

Anyway, in my case while this solve one of my problem, queryForMap is
still not as efficient as it could be since it relies on queryForList.
So basically it is building two list. One with each element being a Map
of column name/value, then another map of only values. Would it be more
efficient to rewrite queryForMap to use a RowHandler?



On Mon, 2005-01-24 at 10:15, Brandon Goodin wrote:
> Why is it a problem to have a code dependency in an "implementation"
> class? It is suppossed to be aware of IBatis SQLMap semantics. You
> interface over your DAO class should hide the specifics of your
> implementation code within your Dao.
> 
> Brandon
> 
> 
> 
> 
> On Mon, 24 Jan 2005 09:22:49 -0800, Pascal DeMilly
> <li...@newgenesys.com> wrote:
> > Thanks Larry,
> > 
> > I tried it and it works. However this still leave some dependencies in
> > my DAO code as I have to specify the column names there as in:
> > 
> > return getSqlMapClientTemplate().queryForMap("getItemNameMap", null, "SKU", "Description");
> > 
> > How will you do about moving that code into the SqlMap. Should I create
> > a custom resultMap with key/value as properties and refer to it in my
> > DAO code?
> > 
> > I am going to give it a try and see what happened.
> > 
> > Thanks for you help.
> > 
> > Pascal
> > 
> > On Mon, 2005-01-24 at 08:44, Larry Meadors wrote:
> > > Try executeQueryForMap() instead.
> > >
> > >
> > > On Mon, 24 Jan 2005 08:42:25 -0800, Pascal DeMilly
> > > <li...@newgenesys.com> wrote:
> > > > Hi,
> > > >
> > > > I would like to retrieve a Map with whose key is the 1st column of my
> > > > query and the value is the 2nd column. For example right now I do:
> > > >
> > > > <select id="getItemNameMap" resultClass="java.util.HashMap">
> > > >         select SKU, Description from Items
> > > > </select>
> > > >
> > > > However this returns a list of maps with the key being the column name
> > > > and the value the column value which seems wasteful in term of space (I
> > > > am using remoting to retrieve that info).
> > > >
> > > > I currently solve it by using a custom rowHandler in my DAO as follow:
> > > >
> > > >     public Map getItemNames () throws DataAccessException {
> > > >         final KeyValueHandler rowHandler = new KeyValueHandler ();
> > > >         getSqlMapClientTemplate().queryWithRowHandler("getItemNameMap", null, rowHandler);
> > > >         return rowHandler.getMap();
> > > >     }
> > > >
> > > >     private class KeyValueHandler implements RowHandler {
> > > >         final Map map = new HashMap ();
> > > >
> > > >         public void handleRow(Object valueObject) {
> > > >             final Map row = (Map) valueObject;
> > > >             map.put (row.get("SKU"), row.get("Description"));
> > > >         }
> > > >
> > > >         public Map getMap () {
> > > >             return map;
> > > >         }
> > > >
> > > >     }
> > > >
> > > > But I would like to move possibly that code out of my DAO code and into
> > > > iBatis SqlMap file.
> > > >
> > > > How could I do that
> > > >
> > > > TIA
> > > >
> > > > Pascal
> > > >
> > > >
> > 
> >


Re: java.util.HashMap as resultClass

Posted by Brandon Goodin <br...@gmail.com>.
Why is it a problem to have a code dependency in an "implementation"
class? It is suppossed to be aware of IBatis SQLMap semantics. You
interface over your DAO class should hide the specifics of your
implementation code within your Dao.

Brandon




On Mon, 24 Jan 2005 09:22:49 -0800, Pascal DeMilly
<li...@newgenesys.com> wrote:
> Thanks Larry,
> 
> I tried it and it works. However this still leave some dependencies in
> my DAO code as I have to specify the column names there as in:
> 
> return getSqlMapClientTemplate().queryForMap("getItemNameMap", null, "SKU", "Description");
> 
> How will you do about moving that code into the SqlMap. Should I create
> a custom resultMap with key/value as properties and refer to it in my
> DAO code?
> 
> I am going to give it a try and see what happened.
> 
> Thanks for you help.
> 
> Pascal
> 
> On Mon, 2005-01-24 at 08:44, Larry Meadors wrote:
> > Try executeQueryForMap() instead.
> >
> >
> > On Mon, 24 Jan 2005 08:42:25 -0800, Pascal DeMilly
> > <li...@newgenesys.com> wrote:
> > > Hi,
> > >
> > > I would like to retrieve a Map with whose key is the 1st column of my
> > > query and the value is the 2nd column. For example right now I do:
> > >
> > > <select id="getItemNameMap" resultClass="java.util.HashMap">
> > >         select SKU, Description from Items
> > > </select>
> > >
> > > However this returns a list of maps with the key being the column name
> > > and the value the column value which seems wasteful in term of space (I
> > > am using remoting to retrieve that info).
> > >
> > > I currently solve it by using a custom rowHandler in my DAO as follow:
> > >
> > >     public Map getItemNames () throws DataAccessException {
> > >         final KeyValueHandler rowHandler = new KeyValueHandler ();
> > >         getSqlMapClientTemplate().queryWithRowHandler("getItemNameMap", null, rowHandler);
> > >         return rowHandler.getMap();
> > >     }
> > >
> > >     private class KeyValueHandler implements RowHandler {
> > >         final Map map = new HashMap ();
> > >
> > >         public void handleRow(Object valueObject) {
> > >             final Map row = (Map) valueObject;
> > >             map.put (row.get("SKU"), row.get("Description"));
> > >         }
> > >
> > >         public Map getMap () {
> > >             return map;
> > >         }
> > >
> > >     }
> > >
> > > But I would like to move possibly that code out of my DAO code and into
> > > iBatis SqlMap file.
> > >
> > > How could I do that
> > >
> > > TIA
> > >
> > > Pascal
> > >
> > >
> 
>

Re: java.util.HashMap as resultClass

Posted by Pascal DeMilly <li...@newgenesys.com>.
Thanks Larry,

I tried it and it works. However this still leave some dependencies in
my DAO code as I have to specify the column names there as in:

return getSqlMapClientTemplate().queryForMap("getItemNameMap", null, "SKU", "Description");

How will you do about moving that code into the SqlMap. Should I create
a custom resultMap with key/value as properties and refer to it in my
DAO code?

I am going to give it a try and see what happened.

Thanks for you help.

Pascal

On Mon, 2005-01-24 at 08:44, Larry Meadors wrote:
> Try executeQueryForMap() instead.
> 
> 
> On Mon, 24 Jan 2005 08:42:25 -0800, Pascal DeMilly
> <li...@newgenesys.com> wrote:
> > Hi,
> > 
> > I would like to retrieve a Map with whose key is the 1st column of my
> > query and the value is the 2nd column. For example right now I do:
> > 
> > <select id="getItemNameMap" resultClass="java.util.HashMap">
> >         select SKU, Description from Items
> > </select>
> > 
> > However this returns a list of maps with the key being the column name
> > and the value the column value which seems wasteful in term of space (I
> > am using remoting to retrieve that info).
> > 
> > I currently solve it by using a custom rowHandler in my DAO as follow:
> > 
> >     public Map getItemNames () throws DataAccessException {
> >         final KeyValueHandler rowHandler = new KeyValueHandler ();
> >         getSqlMapClientTemplate().queryWithRowHandler("getItemNameMap", null, rowHandler);
> >         return rowHandler.getMap();
> >     }
> > 
> >     private class KeyValueHandler implements RowHandler {
> >         final Map map = new HashMap ();
> > 
> >         public void handleRow(Object valueObject) {
> >             final Map row = (Map) valueObject;
> >             map.put (row.get("SKU"), row.get("Description"));
> >         }
> > 
> >         public Map getMap () {
> >             return map;
> >         }
> > 
> >     }
> > 
> > But I would like to move possibly that code out of my DAO code and into
> > iBatis SqlMap file.
> > 
> > How could I do that
> > 
> > TIA
> > 
> > Pascal
> > 
> >


Re: java.util.HashMap as resultClass

Posted by Larry Meadors <la...@gmail.com>.
Try executeQueryForMap() instead.


On Mon, 24 Jan 2005 08:42:25 -0800, Pascal DeMilly
<li...@newgenesys.com> wrote:
> Hi,
> 
> I would like to retrieve a Map with whose key is the 1st column of my
> query and the value is the 2nd column. For example right now I do:
> 
> <select id="getItemNameMap" resultClass="java.util.HashMap">
>         select SKU, Description from Items
> </select>
> 
> However this returns a list of maps with the key being the column name
> and the value the column value which seems wasteful in term of space (I
> am using remoting to retrieve that info).
> 
> I currently solve it by using a custom rowHandler in my DAO as follow:
> 
>     public Map getItemNames () throws DataAccessException {
>         final KeyValueHandler rowHandler = new KeyValueHandler ();
>         getSqlMapClientTemplate().queryWithRowHandler("getItemNameMap", null, rowHandler);
>         return rowHandler.getMap();
>     }
> 
>     private class KeyValueHandler implements RowHandler {
>         final Map map = new HashMap ();
> 
>         public void handleRow(Object valueObject) {
>             final Map row = (Map) valueObject;
>             map.put (row.get("SKU"), row.get("Description"));
>         }
> 
>         public Map getMap () {
>             return map;
>         }
> 
>     }
> 
> But I would like to move possibly that code out of my DAO code and into
> iBatis SqlMap file.
> 
> How could I do that
> 
> TIA
> 
> Pascal
> 
>

Re: java.util.HashMap as resultClass

Posted by Admin <ma...@friendVU.com>.
It's not that much space wasted.
.V

Pascal DeMilly wrote:

>Hi,
>
>I would like to retrieve a Map with whose key is the 1st column of my
>query and the value is the 2nd column. For example right now I do:
>
><select id="getItemNameMap" resultClass="java.util.HashMap">
>	select SKU, Description from Items
></select>
>
>However this returns a list of maps with the key being the column name
>and the value the column value which seems wasteful in term of space (I
>am using remoting to retrieve that info).
>
>I currently solve it by using a custom rowHandler in my DAO as follow:
>
>    public Map getItemNames () throws DataAccessException {
>	final KeyValueHandler rowHandler = new KeyValueHandler ();
>        getSqlMapClientTemplate().queryWithRowHandler("getItemNameMap", null, rowHandler);
>        return rowHandler.getMap();
>    }
>    
>    private class KeyValueHandler implements RowHandler {
>        final Map map = new HashMap ();
>
>        public void handleRow(Object valueObject) {
>            final Map row = (Map) valueObject; 
>            map.put (row.get("SKU"), row.get("Description"));
>        }
>        
>        public Map getMap () {
>            return map;
>        }
>        
>    }
>    
>But I would like to move possibly that code out of my DAO code and into
>iBatis SqlMap file.
>
>How could I do that
>
>TIA
>
>Pascal
>
>
>
>  
>