You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-cs@ibatis.apache.org by Per Magne Bjornerud <PM...@interlect.co.jp> on 2006/10/12 08:37:06 UTC

Mapping data to subclasses

Hello,

I have a database where several subclasses are mapped to the same table.

I'll stick to the standard example here. Assume
Base class: Person
Subclass: Employee
Subclass: Customer

In my application, I mostly use instances of Employee or Customer
objects. But in the DB, they are both stored to the Person table. In
addition to the object properties, they have a PersonType id to know
which is which.

Is there any way to set up iBatis mapping so the objects I get returned
will be instances of the different subclasses, depending on the
PersonType field?

I want to do a very simple "SELECT * FROM Person WHERE Id = X" and map
it to an object.

Obviously identical <select> elements for each subclass and an
additional lookup to know the type of each object before I select them
isn't really an option. Is there a way for iBatis to handle this? 

I guess the alternative might be to create instances of Person objects
and add a GetCorrectInstance() method that will return objects of
correct subclass. If iBatis cannot help me do what I want, I'd also be
happy for some tips, patterns or keywords to help me find the best
possible workaround.

-- 
Per Magne Bjornerud

ATLAS/AJAX with iBatis

Posted by Brian Kierstead <br...@fluidmedia.com>.
Hi everyone,

Is anyone using iBatis with ATLAS or other AJAX technologies?  In our 
current project, under .Net 2.0, I'm using ATLAS on the front-end to do 
in-place editing.

I'm using an architecture similar to that of the NPetShop application - 
Web, Presentation, Service and Persistence, plus a Domain.   I've also 
broken my pages down into user controls which are passed  domain objects 
to show/return.  When a change is made to a field, it raises an event 
which then bubbles up (maybe through a couple of user controls) to the 
parent page.  The parent page contains the main object, which is then 
saved to the database.

For example, I have a Drive which contains an Appointment which contains 
a Location.  When I change the street address it of the Location, it 
raises an event in Location, which raises an event in Appointment, which 
raises and event in Drive.  Drive then persists itself to the database 
via the other layers.

The problem is that while I'm saving the entire Drive object (which also 
contains other objects, not just those I mentioned)  to the database 
only 1 field has changed.  So every time a user updates a field, I save 
a lot of data that hasn't changed.

I don't want persistence details in my front-end, so I don't want the 
changed field to know which database field it corresponds to.

Has anyone done this or have any suggestions about what to do?

Thanks,
Brian


Re: Mapping data to subclasses

Posted by Gilles Bayon <ib...@gmail.com>.
Online doc,

http://ibatis.apache.org/docs/dotnet/datamapper/ch03s05.html#id358764


On 10/12/06, Per Magne Bjornerud <PM...@interlect.co.jp> wrote:
> Hello,
>
> I have a database where several subclasses are mapped to the same table.
>
> I'll stick to the standard example here. Assume
> Base class: Person
> Subclass: Employee
> Subclass: Customer
>
> In my application, I mostly use instances of Employee or Customer
> objects. But in the DB, they are both stored to the Person table. In
> addition to the object properties, they have a PersonType id to know
> which is which.
>
> Is there any way to set up iBatis mapping so the objects I get returned
> will be instances of the different subclasses, depending on the
> PersonType field?
>
> I want to do a very simple "SELECT * FROM Person WHERE Id = X" and map
> it to an object.
>
> Obviously identical <select> elements for each subclass and an
> additional lookup to know the type of each object before I select them
> isn't really an option. Is there a way for iBatis to handle this?
>
> I guess the alternative might be to create instances of Person objects
> and add a GetCorrectInstance() method that will return objects of
> correct subclass. If iBatis cannot help me do what I want, I'd also be
> happy for some tips, patterns or keywords to help me find the best
> possible workaround.
>
> --
> Per Magne Bjornerud
>


-- 
Cheers,
Gilles

<a href="http://www.amazon.com/gp/registry/6JCP7AORB0LE">Wish List</a>

Re: Mapping data to subclasses

Posted by Anderson Forteleza <af...@gmail.com>.
Hello there Per Magne...

...check section 3.5.6 - Inheritance Mapping, of the DataMapper Developer
Guide [
http://www.apache.org/dist/ibatis/binaries/ibatis.net/Doc-DataMapper-1.5.1.zip],
i think there's an explanation there that covers your case :)

[]s
Anderson Fortaleza

On 10/12/06, Per Magne Bjornerud <PM...@interlect.co.jp> wrote:
>
> Hello,
>
> I have a database where several subclasses are mapped to the same table.
>
> I'll stick to the standard example here. Assume
> Base class: Person
> Subclass: Employee
> Subclass: Customer
>
> In my application, I mostly use instances of Employee or Customer
> objects. But in the DB, they are both stored to the Person table. In
> addition to the object properties, they have a PersonType id to know
> which is which.
>
> Is there any way to set up iBatis mapping so the objects I get returned
> will be instances of the different subclasses, depending on the
> PersonType field?
>
> I want to do a very simple "SELECT * FROM Person WHERE Id = X" and map
> it to an object.
>
> Obviously identical <select> elements for each subclass and an
> additional lookup to know the type of each object before I select them
> isn't really an option. Is there a way for iBatis to handle this?
>
> I guess the alternative might be to create instances of Person objects
> and add a GetCorrectInstance() method that will return objects of
> correct subclass. If iBatis cannot help me do what I want, I'd also be
> happy for some tips, patterns or keywords to help me find the best
> possible workaround.
>
> --
> Per Magne Bjornerud
>