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 Dan Bradley <de...@gmail.com> on 2005/10/06 16:06:13 UTC

Changing a sqlMap dynamically?

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.

Re: Changing a sqlMap dynamically?

Posted by Dan Bradley <de...@gmail.com>.
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 <lm...@apache.org> wrote:
> <select id="getWhatever" resultClass="java.util.HashMap" remapResults="true">
> $whateverSqlYouWant$
> </select>
>
> Larry
>
>
> On 10/6/05, Dan Bradley <de...@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.
> >
>

Re: Changing a sqlMap dynamically?

Posted by Larry Meadors <lm...@apache.org>.
<select id="getWhatever" resultClass="java.util.HashMap" remapResults="true">
$whateverSqlYouWant$
</select>

Larry


On 10/6/05, Dan Bradley <de...@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.
>