ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dan Bradley <debrad...@gmail.com>
Subject Re: Changing a sqlMap dynamically?
Date Wed, 12 Oct 2005 17:43:39 GMT
Thanks for your reply Bryan. I don't see in your example how I would
set the resultMap dynamically for the same query. Here's more detail
of what I'm hoping to be able to do.

<typeAlias alias="organization" type="com.foo.Organization" />
<typeAlias alias="orgType" type="com.foo.OrganizationType" />
<typeAlias alias="user" type="com.foo.User" />

	<resultMap id="organization-result" class="organization">
		<result property="id" column="id" />
		<result property="name" column="name" />
		<result property="description" column="description" />
		<result property="type" column="{table=org_types,map=orgType}"
			select="getById" />
	</resultMap>

	<resultMap id="user-result" class="user">
		<result property="id" column="id" />
		<result property="username" column="username" />	
        </resultMap>

    <statement id="getById" resultMap="$map$">
         select * from $table$
    </statement>

String table = users;
User user = (User) sqlMap.select("getById", table, "user-result");

table = organizations;
Organization org = (Organization) sqlMap.select("getById", table, "org-result");

I know this isn't all legal syntax, but you maybe get the gist of what
I'm trying to accomplish. I'd appreciate any further tips you might
have. Thanks.

On 10/12/05, Shannon, Bryan <BShannon@tribune.com> wrote:
> The resultmap can still be dynamically set...
>
> Do so the usual way; by specifying it like this:
>
> <statement id="mystatement" resultMap="map">
>   select * from $tablename$
> </statement>
>
>
> Then execute it as
>
> Map result = sqlMap.select("mystatement", "mytablename");
>
>
> If you need a different resultClass or resultMap (other than just map, which
> is an alias for 'java.util.HashMap') then you can specify that as well.
>
> -----Original Message-----
> From: Dan Bradley [mailto:debradley@gmail.com]
> Sent: Wednesday, October 12, 2005 1:21 PM
> To: user-java@ibatis.apache.org
> Subject: Re: Changing a sqlMap dynamically?
>
>
> Are you suggesting that, using Larry's synax:
>
> <select id="getWhatever" resultClass="java.util.HashMap"
> remapResults="true">
> $whateverSqlYouWant$
> </select>
>
> From Java I could do something like:
>
> Map result = select("getWhatever", "SELECT * FROM "+tablename);
> Long id = (Long) result.get("id");
>
> If so, I didn't get that (obviously) from the original comment. Also,
> I've never run across the $ syntax, which threw me. Does that mean
> 'include the passed in object as a string'?
>
> One limitation with this approach that springs to mind is that the
> resultMap couldn't be dynamically set. An example I'm thinking of -
> every DAO I write has a getById(Long id) method which executes a sql
> query of get * from table where id=#id#. The only thing that varies
> from one entity to the next is the tablename (which could be
> dynamically set in the above example) and the result map, which could
> not be in this case.
>
> Am I still misunderstanding?
>
> Thanks.
>
>
> On 10/12/05, Shannon, Bryan <BShannon@tribune.com> wrote:
> > It looks like the original response from Larry *is* the way you want to do
> > it.
> >
> > It is essentially an "on the fly" sql map.  I use this construct in a few
> > select cases (searches with arbitrarily complex arguments, etc.)
> >
> > I use it basically when I need to write my own SQL, but still have iBatis
> do
> > the black magic of turning rows and ResultSets into actual objects and
> > lists.  Works like a charm.  :-)
> >
> > You still have to create a map; and give the map a name.  The VALUE that
> you
> > pass into it is SQL code you wrote arbitrarily.
> >
> > Even using something like session.select("my arbitrary sql") won't work
> > because you'd still have to tell iBatis (somehow) what resultmap/class you
> > need to use, etc. etc.
> >
> > Hope this helps!
> >
> > -Bryan
> >
> >
> > -----Original Message-----
> > From: Dan Bradley [mailto:debradley@gmail.com]
> > Sent: Thursday, October 06, 2005 12:09 PM
> > To: lmeadors@apache.org
> > Cc: user-java@ibatis.apache.org
> > Subject: Re: Changing a sqlMap dynamically?
> >
> >
> > Hi Larry -
> >
> > Thanks for the reply, but I think you misunderstood the question. I
> > understand that I can put arbitrary SQL into a named select element
> > within a sqlMap file. That then gets called, by name, from
> > SqlMapExecutor/Session.
> >
> > What I'm wondering is if there's either:
> >
> > - a way to (essentially) call session.select("my arbitrary sql")
> > instead of session.select("namedQuery")
> > - or to created new named mappings at runtime such that
> > select("namedQuery") is calling a query that doesn't exist in the XML
> > file on disk.
> >
> > My goal is, if I have 10 entities, and they all have a query that
> > looks like "select * from tablename" and the only that that varies is
> > the tablename, I'd like to be able to have my Java create that query
> > on the fly. I'm looking to build a general framework that handles some
> > of the plumbing for me instead of requiring a lot of hand work to be
> > done building the XML files.
> >
> > Thanks.
> >
> > On 10/6/05, Larry Meadors <lmeadors@apache.org> wrote:
> > > <select id="getWhatever" resultClass="java.util.HashMap"
> > remapResults="true">
> > > $whateverSqlYouWant$
> > > </select>
> > >
> > > Larry
> > >
> > >
> > > On 10/6/05, Dan Bradley <debradley@gmail.com> wrote:
> > > > Using iBATIS you normally create an XML file with a sqlMap element
> > > > that defines SQL queries that can be referenced by name. Is it also
> > > > possible to create these queries dynamically, through Java, instead of
> > > > hard-coding them all in XML? I'm hoping to build some framework code
> > > > that will programatically create some common queries based on the
> > > > properties of an object, instead of writing a "getAll" or "getById"
> > > > query for every entity which varies only in the table name.
> > > >
> > > > SqlMapExecutor's query/insert/delete methods require the name of a
> > > > mapped statement and don't appear to allow arbitrary SQL. If iBATIS
> > > > supports what I'm looking for, where would you hook into the system in
> > > > order to create new mappings on the fly?
> > > >
> > > > Thanks.
> > > >
> > >
> >
>

Mime
View raw message