-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Refactoring and mapping an existing table
PostPosted: Wed Sep 28, 2005 10:04 am 
Newbie

Joined: Thu Aug 04, 2005 9:10 am
Posts: 4
hello,
I have to rework an application, so database tables are already existing.
I need some advice on a refactoring.
As the business/domain objects was linked by their database identifiants ( in best case, sometime there's no link although there's a relationship ), i need to add some reference to the convenient objects.
i have to deal with 3 tables.

The table batch and lot are simplified, the table batchlot is as the existant. notice the quantity field in batchlot

Code:

create table batch(
    id BIGINT not null,
    name VARCHAR(255),
    primary key (id)
)

create table batchlot (
    batch_id BIGINT not null,
    lot_id BIGINT not null,
    quantity INTEGER,
    primary key (batch_id),
    primary key (lot_id)
)

create table lot(
    id BIGINT not null,
    name VARCHAR(255), 
    primary key (id)
)



There are 3 classes corresponding to the tables. Batch, Lot and BatchLot. Batch has no attribut that link to lot, either database id or object reference.
Lot has no attribut that link to batch, either database id or object reference.
BatchLot has 2 attribut id, one for a batch and one for lot, and a quantity attribut.

Something like:
Code:
class batch
{
   int id;
   String name;
   // ...

}

class lot
{
    int id;
    String name;
    // ...
}

class batchlot
{
    int batch_id;
    int lot_id;
    int quantity;
}


So there's a many-to-many relationship between batch and lots.

I thought adding a List of Lot in Batch and a List of Batch in Lot, using the batchlot table. But in this case what become the quantity attribut of the batchlot table ?

Should i create 3 mapping files :
batch.hbm.xml : a one-to-many to batchlotcorrel
batchlotcorrel.hbm.xml: a many-to-one to batch and a many-to-one to lot
lot.hbm.xml: a one-to-many to batchlotcorrel
and so adding a list of batchlotcorrel in batch and lot, and replacing batch id and lot id by reference on batch and lot objects ?

what do you think of this ?


Top
 Profile  
 
 Post subject: ternary mapping
PostPosted: Thu Sep 29, 2005 4:18 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Well, you may need composite element mapping.
Let me give an example:
Ent and Address have corresponding tables 'ent' and 'address' and they linked by third table 'ent_address' that has additional field description.


Code:
CREATE TABLE address (
  aid int PRIMARY KEY,
  street varchar(20),
  zip varchar(20)
);


CREATE TABLE ent (
  id int PRIMARY KEY,
  name varchar(50)
);


CREATE TABLE ent_address (
  ent_id int,
  address_id int,
  descr varchar(200)
);


Then we create component class AddressDescription that look like this:
Code:
public class AddressDescription {
  String descr;
  Address address;

  public String getDescr(){
    return descr;
  }

  public void setDescr( String descr ){
    this.descr = descr;
  }

  public Address getAddress(){
    return address;
  }

  public void setAddress( Address address ){
    this.address = address;
  }
}



And mapping like this:

Code:
<class table="ent" name="Ent">
    <id name="id">
       <generator class="assigned"/>
    </id>
    <property name="name"/>
   <!-- <set name="linkedObjects" table="ent_relations" lazy="true" >
      <key column="id1"/>
      <many-to-many class="Ent" column="id2"/>
    </set>-->
    <set name="linkedObjects" table="ent_address" lazy="false">
      <key column="ent_id"/>
      <composite-element class="AddressDescription" >
        <many-to-one name="Address" column="address_id" />
        <property name="descr"/>
      </composite-element>
    </set>
  </class>

  <class table="address" name="Address">
    <id name="id">
       <generator class="assigned"/>
    </id>
    <property name="street"/>
    <property name="zip"/>
  </class>


hope that helps

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.