Return-Path: Delivered-To: apmail-ibatis-user-java-archive@www.apache.org Received: (qmail 31022 invoked from network); 1 Jul 2008 20:36:48 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 1 Jul 2008 20:36:48 -0000 Received: (qmail 16965 invoked by uid 500); 1 Jul 2008 20:36:44 -0000 Delivered-To: apmail-ibatis-user-java-archive@ibatis.apache.org Received: (qmail 16952 invoked by uid 500); 1 Jul 2008 20:36:44 -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 16941 invoked by uid 99); 1 Jul 2008 20:36:43 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 01 Jul 2008 13:36:43 -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 64.233.182.186 as permitted sender) Received: from [64.233.182.186] (HELO nf-out-0910.google.com) (64.233.182.186) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 01 Jul 2008 20:35:53 +0000 Received: by nf-out-0910.google.com with SMTP id 30so13349nfu.46 for ; Tue, 01 Jul 2008 13:36:12 -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=ZxttY3HM8nZPRx8p5Y3bQ4q/SpG0e78TqjbqpmDoqMg=; b=DwiLfJ1BuxrBOHcG83csPr72eyr8eyhRTobSOKUGWyUxYswVclLjVOCn+M1YTR3Gya A8idOGlYXL/A57HzGZBp+1ktHz0sPUl1tufelZIsuaHEHm4M7GKnxTCcT0n6JSmXVaXg m25KEY3aGk6+HtvV94rBNYsOxk+qPSrgw7yBg= 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=j2tDEkwz1O4u9gru/hkG8nn0xoI8vl6XGweQtLyy3rHCLER5YgChNlMwXGKN6JX/cG YTVOo+FJ1EKMUMWgiZr4hIm5lc53O8x9Z8x74EZ1q/DMClHrPLOQubm+3TTuAa3D7Fi1 zRvR7YOh771Ehbq3OpxVJemAeUVsMzH5UV/L0= Received: by 10.210.142.6 with SMTP id p6mr5773055ebd.102.1214944572310; Tue, 01 Jul 2008 13:36:12 -0700 (PDT) Received: by 10.210.36.15 with HTTP; Tue, 1 Jul 2008 13:36:12 -0700 (PDT) Message-ID: Date: Tue, 1 Jul 2008 15:36:12 -0500 From: "Jeff Butler" To: user-java@ibatis.apache.org Subject: Re: abator and class hierarchy In-Reply-To: <1214943260.5705.734.camel@localhost.localdomain> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_2155_20290193.1214944572307" References: <1214936166.5705.711.camel@localhost.localdomain> <1214943260.5705.734.camel@localhost.localdomain> X-Virus-Checked: Checked by ClamAV on apache.org ------=_Part_2155_20290193.1214944572307 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Yep - that's the idea. I agree about the "magic" - comment heavily. iBATIS is instantiating the new objects here, so you have to do it the iBATIS specific way. As for the general question, I can't think of a more general way to do this - but I am far from the final authority on Java practices. This is partly what the FactoryMethod pattern is all about - you just have to make sure that you are using factories when creating objects. Jeff Butler On Tue, Jul 1, 2008 at 3:14 PM, David Evans wrote: > Hi Jeff, > > Thanks for the tip. Is this the general idea? > > public Object createInstance(String stmtId, Class clazz) { > String className = clazz.getName(); > if (className.startsWith("com.test.model.abator")) { > className = className.replaceFirst("\.abator",""); > Object obj = Resources.instantiate(className); > return obj; > } > > return null; > > } > > So the sqlMap file would still say: > resultMap class="com.test.model.abator.Account" > > but would actually return a com.test.model.Account object? > > This seems like a good solution, although the "magic" of that class > conversion seems a little opaque if someone else had to maintain the > code. I guess commenting heavily could help. Do you know of any > non-ibatis specific way to create a subclass from an instance of a > superclass? > > Thanks for the help, > > Dave > > > > On Tue, 2008-07-01 at 13:36 -0500, Jeff Butler wrote: > > You could use iBATIS' ResultObjectFactory. When iBATIS asks for a new > > com.test.model.abator.Thing, you could return com.test.model.Thing. > > > > Jeff Butler > > > > > > > > On Tue, Jul 1, 2008 at 1:16 PM, David Evans > > wrote: > > Hello, > > > > Apologies, This post is slightly off topic, as it has to do > > with java > > class hierarchy, but i'm hoping someone here will have solved > > the same > > problem. > > > > I have set up abator to create my sqlmaps, model and spring > > dao classes. > > I would like to use these generated model classes as base > > classes for > > more fully featured business objects. my situation looks like > > this: > > > > com.test.model.abator.Thing (generated by abator) > > com.test.model.Thing extends com.test.model.abator.Thing > > > > Now my Thing model never has to worry about the field getters > > and > > setters mirroring the database table. > > > > When using the dao classes, i have a problem. For updates and > > inserts, > > everything is fine, i can pass the com.test.model.Thing to the > > dao and > > it accepts it of course, because it extends > > com.test.model.abator.Thing. > > but on a select the dao returns me a > > com.test.model.abator.Thing, which > > I cannot cast to a com.test.model.Thing. I can't think of a > > way to > > create a new com.test.model.Thing from a > > com.test.model.abator.Thing > > without duplicating all of the fields. If i have to create a > > constructor > > which explicitly transfers all of the field values then i lose > > the > > entire point of my abator setup. > > > > Thanks, > > > > Dave > > > > > > ------=_Part_2155_20290193.1214944572307 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline
Yep - that's the idea.  I agree about the "magic" - comment heavily.
 
iBATIS is instantiating the new objects here, so you have to do it the iBATIS specific way.
 
As for the general question, I can't think of a more general way to do this - but I am far from the final authority on Java practices.  This is partly what the FactoryMethod pattern is all about - you just have to make sure that you are using factories when creating objects.
 
Jeff Butler
 

 
On Tue, Jul 1, 2008 at 3:14 PM, David Evans <dsevans@berndtgroup.net> wrote:
Hi Jeff,

Thanks for the tip. Is this the general idea?

public Object createInstance(String stmtId, Class clazz) {
 String className = clazz.getName();
 if (className.startsWith("com.test.model.abator")) {
    className = className.replaceFirst("\.abator","");
    Object obj = Resources.instantiate(className);
    return obj;
 }

 return null;

}

So the sqlMap file would still say:
resultMap class="com.test.model.abator.Account"

but would actually return a com.test.model.Account object?

This seems like a good solution, although the "magic" of that class
conversion seems a little opaque if someone else had to maintain the
code. I guess commenting heavily could help. Do you know of any
non-ibatis specific way to create a subclass from an instance of a
superclass?

Thanks for the help,

Dave



On Tue, 2008-07-01 at 13:36 -0500, Jeff Butler wrote:
> You could use iBATIS' ResultObjectFactory.  When iBATIS asks for a new
> com.test.model.abator.Thing, you could return com.test.model.Thing.
>
> Jeff Butler
>
>
>
> On Tue, Jul 1, 2008 at 1:16 PM, David Evans <dsevans@berndtgroup.net>
> wrote:
>         Hello,
>
>         Apologies, This post is slightly off topic, as it has to do
>         with java
>         class hierarchy, but i'm hoping someone here will have solved
>         the same
>         problem.
>
>         I have set up abator to create my sqlmaps, model and spring
>         dao classes.
>         I would like to use these generated model classes as base
>         classes for
>         more fully featured business objects. my situation looks like
>         this:
>
>         com.test.model.abator.Thing (generated by abator)
>         com.test.model.Thing extends com.test.model.abator.Thing
>
>         Now my Thing model never has to worry about the field getters
>         and
>         setters mirroring the database table.
>
>         When using the dao classes, i have a problem. For updates and
>         inserts,
>         everything is fine, i can pass the com.test.model.Thing to the
>         dao and
>         it accepts it of course, because it extends
>         com.test.model.abator.Thing.
>         but on a select the dao returns me a
>         com.test.model.abator.Thing, which
>         I cannot cast to a com.test.model.Thing. I can't think of a
>         way to
>         create a new com.test.model.Thing from a
>         com.test.model.abator.Thing
>         without duplicating all of the fields. If i have to create a
>         constructor
>         which explicitly transfers all of the field values then i lose
>         the
>         entire point of my abator setup.
>
>         Thanks,
>
>         Dave
>
>


------=_Part_2155_20290193.1214944572307--