Return-Path: Delivered-To: apmail-ibatis-user-java-archive@www.apache.org Received: (qmail 51804 invoked from network); 13 Aug 2008 14:52:11 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 13 Aug 2008 14:52:11 -0000 Received: (qmail 80092 invoked by uid 500); 13 Aug 2008 14:52:02 -0000 Delivered-To: apmail-ibatis-user-java-archive@ibatis.apache.org Received: (qmail 80075 invoked by uid 500); 13 Aug 2008 14:52:02 -0000 Mailing-List: contact user-java-help@ibatis.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user-java@ibatis.apache.org Delivered-To: mailing list user-java@ibatis.apache.org Received: (qmail 80064 invoked by uid 99); 13 Aug 2008 14:52:02 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 13 Aug 2008 07:52:02 -0700 X-ASF-Spam-Status: No, hits=2.0 required=10.0 tests=HTML_MESSAGE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of jeffgbutler@gmail.com designates 66.249.82.226 as permitted sender) Received: from [66.249.82.226] (HELO wx-out-0506.google.com) (66.249.82.226) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 13 Aug 2008 14:51:04 +0000 Received: by wx-out-0506.google.com with SMTP id r21so13390wxc.27 for ; Wed, 13 Aug 2008 07:51:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:in-reply-to:mime-version:content-type:references; bh=rBgIntJ2jh88KiD2SekMR2SWDF/mqD0zZKcnwOUw6VA=; b=baPXuOXpBrNrzy/X2Dib2dsPreHnsnpF0eR+iJ5MUNtB009jlRg1O/WJfwA8yzCD3r WTTonuwRezPhiRmBEDnLhSq7m8a8tnl8EBRr52kcApvWufX6zaiQ3NLDthpTkqv04eE6 QaOevYm7F+PinrmaKopHioDZaC+3go9qRhNeM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:in-reply-to:mime-version :content-type:references; b=UMB0/dLHwsxCho8vCod/vZNmPCcmIneJcTAuWCg5B1DoKxh37DGNgmRWAHG8m+6Erd yCwo3Of4Y8bNo2qBOa4rhnYyaA5rBLym6VNEJbElEc+7i+gATXQ562CXuhtafAEaeK6p jC2i0QQPvl4kl5G8BhPYqQ8VgOo2s62KuLamw= Received: by 10.90.30.2 with SMTP id d2mr14958413agd.118.1218639072944; Wed, 13 Aug 2008 07:51:12 -0700 (PDT) Received: by 10.90.90.1 with HTTP; Wed, 13 Aug 2008 07:51:12 -0700 (PDT) Message-ID: Date: Wed, 13 Aug 2008 09:51:12 -0500 From: "Jeff Butler" To: user-java@ibatis.apache.org Subject: Re: tablename in resultmap column In-Reply-To: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_112448_19230513.1218639072950" References: <42B38D7063743F46BA7333E01CBB13253AFE51@sirius.intern.bwso.de> X-Virus-Checked: Checked by ClamAV on apache.org ------=_Part_112448_19230513.1218639072950 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Sorry - just read your post a little closer and see that you know about alias already. Why won't alias work? Jeff Butler On Wed, Aug 13, 2008 at 9:43 AM, Jeff Butler wrote: > You can ask (a)ibator to generate an alias for the tables and column > names - I think that work for this situation. See the "alias" attribute on > the element: > > > http://svn.apache.org/repos/asf/ibatis/trunk/java/tools/ibator/core/htmldoc/configreference/table.html > > Jeff Butler > > On Wed, Aug 13, 2008 at 9:23 AM, BWSO, Bernd Behler wrote: > >> Is there a way to include the table name in the column resultmap >> definitions generated by ibator? >> >> This would be very useful when having joins with column name collisions: >> >> An example: >> >> - TABLE "PRODUCT" having ID and NAME columns >> - TABLE "CATEGORY" having ID and NAME columns >> >> >> Ibator generated resultmaps currently look like: >> >> >> >> >> >> >> >> >> >> >> >> >> >> Now I want to extend the generated sqlmap to add the category to every >> product as follows: >> >> > id="productWithCategoryResult"> >> >> >> >> >> The select would look like: >> >> >> >> >> >> Without having the tablenames in the resultmap, ibatis does not know >> which ID or NAME column to map to which object. So I end up having the >> value of PRODUCT.ID and PRODUCT.NAMEin my created Category instance. >> >> Serveral posts suggest to not use wildcards in the select statement and >> to define column aliases to avoid column name collisions. But this is >> very tedious and you have to write your own join resultMaps to match >> with the column defined aliases aswell. >> >> >> A working solution is to change the ibator generated resultmaps to >> include the table name in the column attribute: >> >> >> > jdbcType="INTEGER" property="id" /> >> > jdbcType="VARCHAR" property="name" >> /> >> >> >> >> > jdbcType="INTEGER" property="id" /> >> > jdbcType="VARCHAR" >> property="name" /> >> >> >> >> Works like a charm, but every time I re-generate the sqlmaps my changes >> are thrown away and I have to make them again by hand. I searched the >> mailing list and found out that you can define an "alias" for each >> table, but this isn't helping in my case either. >> >> If it can't already be done somehow, my suggestion would be to include >> an "includeTablenameInColumns" switch in ibator. >> >> Thanks for your help, >> B. Behler >> >> >> >> > ------=_Part_112448_19230513.1218639072950 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline
Sorry - just read your post a little closer and see that you know about alias already.  Why won't alias work?
 
<resultMap class="test.Product" id="productResult">
       <result column="A_ID" jdbcType="INTEGER" property="id" />
       <result column="A_NAME" jdbcType="VARCHAR" property="name" />
</resultMap>

<resultMap class="test.Category" id="categoryResult">
       <result column="B_ID" jdbcType="INTEGER" property="id" />
       <result column="B_NAME" jdbcType="VARCHAR" property="name" />
</resultMap>

 
<resultMap class="test.Product" extends="productResult"
id="productWithCategoryResult">
   <result property="category" resultMap="CATEGORY.categoryResult " />
</resultMap>

 
<select id="selectProductsWithCategory"
  resultMap="productWithCategoryResult">
  select a.id as a_id, a.name as a_name, b.id as b_id, b.name as b_name
  from product a join category b on
  where a.id = b.id
</select>
 
Jeff Butler

 
On Wed, Aug 13, 2008 at 9:43 AM, Jeff Butler <jeffgbutler@gmail.com> wrote:
You can ask (a)ibator to generate an alias for the tables and column names - I think that work for this situation.  See the "alias" attribute on the <table> element:
 
 
Jeff Butler

On Wed, Aug 13, 2008 at 9:23 AM, BWSO, Bernd Behler <bb@bwso.de> wrote:
Is there a way to include the table name in the column resultmap
definitions generated by ibator?

This would be very useful when having joins with column name collisions:

An example:

- TABLE "PRODUCT" having ID and NAME columns
- TABLE "CATEGORY" having ID and NAME columns


Ibator generated resultmaps currently look like:

<resultMap class="test.Product" id="productResult">
       <result column="ID" jdbcType="INTEGER" property="id" />
       <result column="NAME" jdbcType="VARCHAR" property="name" />
</resultMap>

<resultMap class="test.Category" id="categoryResult">
       <result column="ID" jdbcType="INTEGER" property="id" />
       <result column="NAME" jdbcType="VARCHAR" property="name" />
</resultMap>



Now I want to extend the generated sqlmap to add the category to every
product as follows:

<resultMap class="test.Product" extends="productResult"
id="productWithCategoryResult">
   <result property="category" resultMap="CATEGORY.categoryResult " />
</resultMap>


The select would look like:

<select id="selectProductsWithCategory"
resultMap="productWithCategoryResult">
  select PRODUCT.*, CATEGORY.*
  where CATEGORY.ID = PRODUCT.CATEGORY_ID
</select>



Without having the tablenames in the resultmap, ibatis does not know
which ID or NAME column to map to which object. So I end up having the
value of PRODUCT.ID and PRODUCT.NAME in my created Category instance.

Serveral posts suggest to not use wildcards in the select statement and
to define column aliases to avoid column name collisions. But this is
very tedious and you have to write your own join resultMaps to match
with the column defined aliases aswell.


A working solution is to change the ibator generated resultmaps to
include the table name in the column attribute:

<resultMap class="test.Product" id="productResult">
       <result column="PRODUCT.ID" jdbcType="INTEGER" property="id" />
       <result column="PRODUCT.NAME" jdbcType="VARCHAR" property="name"
/>
</resultMap>

<resultMap class="test.Category" id="categoryResult">
       <result column="CATEGORY.ID" jdbcType="INTEGER" property="id" />
       <result column="CATEGORY.NAME" jdbcType="VARCHAR"
property="name" />
</resultMap>


Works like a charm, but every time I re-generate the sqlmaps my changes
are thrown away and I have to make them again by hand. I searched the
mailing list and found out that you can define an "alias" for each
table, but this isn't helping in my case either.

If it can't already be done somehow, my suggestion would be to include
an "includeTablenameInColumns" switch in ibator.

Thanks for your help,
B. Behler





------=_Part_112448_19230513.1218639072950--