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 David Evans <ds...@berndtgroup.net> on 2008/07/01 20:16:06 UTC

abator and class hierarchy

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


Re: abator and class hierarchy

Posted by David Evans <ds...@berndtgroup.net>.
The only other thing I can think of is to use something like apache
commons-beanutils to convert the abator object to the main model by just
copying properties. That isn't particularly satisfying either. 

Thanks again for the idea, I think i'll probably go with it.

Dave


On Tue, 2008-07-01 at 15:36 -0500, Jeff Butler wrote:
> 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 <ds...@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
>         <ds...@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
>         >
>         >
>         
>         
> 


Re: abator and class hierarchy

Posted by Jeff Butler <je...@gmail.com>.
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 <ds...@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 <ds...@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
> >
> >
>
>

Re: abator and class hierarchy

Posted by David Evans <ds...@berndtgroup.net>.
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 <ds...@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
>         
> 


Re: abator and class hierarchy

Posted by Jeff Butler <je...@gmail.com>.
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 <ds...@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
>
>