You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Robert J Celestino <Ro...@sas.com> on 2003/10/31 03:14:16 UTC

o/r mapping question

Hello all,

I am stumped by what is posibly a very simple mapping problem. 

Consider something like this
	Person is stored in the person table
	Person has-a AddressGroup. AddressGroup is not stored in the DB
	AddressGroup has a list of Addresses
	Address is stored in the address table. Has a column 
		called PersonId that indicates the person it belongs to

This is a little contrived but bear with me. 

Clearly Person could have a list of addresses, but for various reasons it has the intermediate class AddressGroup instead. 

I know how to solve this if AddressGroup was stored in the DB, but it is not. It is purely a domain class. 

How can I get ojb to create and write this class without actually writing it to the db? 
	
Thanks very much
Bob c

----------------------------------------------------------------
Bob Celestino
SAS Research and Development
919 - 531 - 9425
robertj.celestino@sas.com

SAS - The Power to Know

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: o/r mapping question

Posted by Brian McCallister <mc...@forthillcompany.com>.
Solution 1:
Implement AddressGroup as a custom collection (implementing Manageable  
Collection) used to hold the address instances. This muddies the domain  
model a bit as AddressGroup then needs to know about  
ManageableCollection.

Solution 2:
Map two classes to the same table (I used an existing application hence  
the names of tables):

kim=# \d users
\d               Table "public.users"
    Column   |          Type          | Modifiers
------------+------------------------+-----------
  id         | integer                | not null
  handle     | character varying(32)  |
  password   | character varying(255) |
  first_name | character varying(255) |
  last_name  | character varying(255) |
  aim_name   | character varying(255) |
  email      | character varying(255) |
Indexes: users_pkey primary key btree (id)

kim=# \d gifts
                  Table "public.gifts"
      Column      |          Type           | Modifiers
-----------------+-------------------------+-----------
  id              | integer                 | not null
  name            | character varying(255)  |
  description     | character varying(2000) |
  level_of_desire | integer                 | default 0
  where_to_buy    | character varying(2000) |
  cost            | character varying(255)  |
  for_user_id     | integer                 | not null
  from_user_id    | integer                 |
  suggest_user_id | integer                 |
Indexes: gifts_pkey primary key btree (id)
Foreign Key constraints: gifts_fk_1 FOREIGN KEY (suggest_user_id)  
REFERENCES users(id) ON UPDATE NO ACTION ON DELETE NO ACTION,
                          gifts_fk_2 FOREIGN KEY (for_user_id)  
REFERENCES users(id) ON UPDATE NO ACTION ON DELETE NO ACTION,
                          gifts_fk_3 FOREIGN KEY (from_user_id)  
REFERENCES users(id) ON UPDATE NO ACTION ON DELETE NO ACTION

kim=#


<class-descriptor class="org.skife.kim.model.Foo" table="USERS">
     <field-descriptor name="id" column="ID" jdbc-type="INTEGER"  
primarykey="true" autoincrement="true"/>
     <reference-descriptor name="bar"  
class-ref="org.skife.kim.model.Bar">
         <foreignkey field-ref="id"/>
     </reference-descriptor>
</class-descriptor>

<class-descriptor class="org.skife.kim.model.Bar" table="USERS">
     <field-descriptor name="id" column="ID" jdbc-type="INTEGER"  
primarykey="true" autoincrement="true"/>
     <collection-descriptor name="gifts"  
element-class-ref="org.skife.kim.model.Gift"
          
collection- 
class="org.apache.ojb.broker.util.collections.ManageableHashSet">
         <inverse-foreignkey field-ref="for_user_id"/>
     </collection-descriptor>
</class-descriptor>

<class-descriptor class="org.skife.kim.model.Bang" table="GIFTS">
     <field-descriptor name="id" column="ID" jdbc-type="INTEGER"  
primarykey="true" autoincrement="true"/>
     <field-descriptor name="for_user_id" column="FOR_USER_ID"  
jdbc-type="INTEGER" access="anonymous"/>
     <reference-descriptor name="bar"  
class-ref="org.skife.kim.model.User">
         <foreignkey field-ref="for_user_id"/>
     </reference-descriptor>
</class-descriptor>


public class Foo
{
     public Integer id;
     public Bar bar;
}

public class Bar
{
     public Integer id;
     public Set gifts;
}

public class Bang
{
     public Integer id;
     public Bar bar;
}

     public void testFooBar() throws Exception
     {
         foo = new Foo();
         bar = new Bar();
         bang = new Bang();

         PersistenceBroker broker =  
PersistenceBrokerFactory.defaultPersistenceBroker();
         foo.bar = bar;
         bar.gifts = new HashSet();
         bar.gifts.add(bang);
         bang.bar = bar;
         broker.store(foo);
         broker.store(bar);
         broker.store(bang);
         broker.clearCache();

         Criteria crit = new Criteria();
         crit.addEqualTo("id", this.foo.id);
         Query query = new QueryByCriteria(Foo.class, crit);
         Foo thing = (Foo) broker.getObjectByQuery(query);
         Assert.assertEquals(thing.id, foo.id);
         Assert.assertEquals(1, foo.bar.gifts.size());
     }

.
Time: 2.108

OK (1 test)

Neither one is ideal, but the ideal is having the data model and object  
model match up perfectly. The second leaves your object model  
independent of your persistence mechanism at least but sort of feels  
dirty to me.

-Brian

On Thursday, October 30, 2003, at 09:14 PM, Robert J Celestino wrote:

> Hello all,
>
> I am stumped by what is posibly a very simple mapping problem.
>
> Consider something like this
> 	Person is stored in the person table
> 	Person has-a AddressGroup. AddressGroup is not stored in the DB
> 	AddressGroup has a list of Addresses
> 	Address is stored in the address table. Has a column
> 		called PersonId that indicates the person it belongs to
>
> This is a little contrived but bear with me.
>
> Clearly Person could have a list of addresses, but for various reasons  
> it has the intermediate class AddressGroup instead.
>
> I know how to solve this if AddressGroup was stored in the DB, but it  
> is not. It is purely a domain class.
>
> How can I get ojb to create and write this class without actually  
> writing it to the db?
> 	
> Thanks very much
> Bob c
>
> ----------------------------------------------------------------
> Bob Celestino
> SAS Research and Development
> 919 - 531 - 9425
> robertj.celestino@sas.com
>
> SAS - The Power to Know
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>
>