-->
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.  [ 10 posts ] 
Author Message
 Post subject: Map abstract class using table-per-subclass approach
PostPosted: Fri Feb 20, 2009 6:34 am 
Newbie

Joined: Fri Feb 20, 2009 6:25 am
Posts: 10
Hi all,

I want to map class hierarchy that has abstract class at top level. Can I map it using table-per-subclass approach?

For example,

Code:

abstract class A{

      long id;
     
      //getter & setter for id

}

class B extends A{

     String value;

      //getter & setter for value
}



Please help to map this hierarchy and how should I design tables.

Thanks in advance...

_________________
Thanks & Regards,

Vishal Shukla


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 7:34 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Yes you can. I dont see any complications in your example. Your tables would be:

TABLE_A
------------
ID <PK>

TABLE_B
------------
A_ID <PK> <FK refer TABLE_A.ID>
VALUE

Mapping would be:
Code:
<class name="A" table="TABLE_A">
   <id name="id" column="ID" type="long">
      <generator class="native"/>
   </id>
   
   <joined-subclass name="B" table="TABLE_B">
      <key column="A_ID">
      <property name="value" column="VALUE"/>
   </joined-subclass>
</class>

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 21, 2009 8:35 am 
Newbie

Joined: Fri Feb 20, 2009 6:25 am
Posts: 10
Hi Letty,

Thanks for the reply.. it certainly workd.. now I had one more addition to it. I hav list of A inside B. and I hav prepared following table for it..

A_B_Map
___________________

id <PK>
a_id <FK_A>
b_id <FK_B>
position

is this table fine to represent that relationship? Can it work without introducing third table?? And I tried to map it but didn't succeed in any... it would be gr8 if sm1 can help me out to map it.

_________________
Thanks & Regards,

Vishal Shukla


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2009 8:12 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
What is the type of relation b/w A and B. Is it Many to one or many to many?

If its many to one then you dont need a new a table. Table A can have a FK to table B.

If its many to one then you ll need a separate link table to link A and B.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 2:35 am 
Newbie

Joined: Fri Feb 20, 2009 6:25 am
Posts: 10
Hi litty,

Its many-to-many relationship. and B has many instances of A(List). Can you please help me in creating mapping file for this hierarchy? I am not getting exactly how to map this kind of list.

_________________
Thanks & Regards,

Vishal Shukla


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 3:05 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Hey... wait...

At first you said, B is subclass of A. And now you are saying A and B has a many2many relation. This is not possible, remember your table B's primary key is a foreign key referencing table A. So the relation b/w the two is one2one and it can never be many2many.

I think there is some issue with your domain model. Can you give the scenario which you are dealing with.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 4:56 am 
Newbie

Joined: Fri Feb 20, 2009 6:25 am
Posts: 10
Hi Litty,

Actually I am not doing this for some particular domain, but its a kind of generalized pattern which can be used in any specific domains.

For instance, consider that I want to store some table structure with values in the database.

Here consider Cell as Component, Row & Table as Containers.

Try to understand following pattern. I verified this in "in memory" form. But I want to store this table in the database. But I am just blocked at one point, just breaking down the problem, and will post as soon as I identify the same.

Finally, I just want to map this hierarchy in the xml. How do I do that... As its verified "in memory", I believe that it shouldn't be impossible, if it is, then its a bug of Hibernate.

Code:

abstract class Component{

       protected long componentID;
       protected List containers = new ArrayList();

       //Getter and Setters
}


abstract class Container extends Component{
       
       protected List components = new ArrayList();

       //Getter and Setters
}

class Cell extends Component{

      String value;

      //Getter and setters.
}

class Row extends Container{

     
     
}

class Table extends Container{

     
     
}

class Driver{
     
      public static void main(String[] args){
     
      Cell cell = new Cell();
      cell.setValue("3");

      Row row = new Row();
      row.addComponent(cell);
         
      Table table = new Table();
      table.addComponent(row);
      table.addComponent(row);
     
      Table t2 = new Table();
      t2.addComponent(row);
      t2.addComponent(row);

      }
}


_________________
Thanks & Regards,

Vishal Shukla


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 4:59 am 
Newbie

Joined: Fri Feb 20, 2009 6:25 am
Posts: 10
I am trying following mapping for this.

Code:
<hibernate-mapping>
  <class abstract="true" name="Component" table="component">
    <id column="ComponentID" name="componentID" type="long">
      <generator class="native"/>
    </id>
    <list name="containers" table="container_component_map">
        <key column="ComponentID"/>
        <index column="position"/>
        <many-to-many class="Container" column="ContainerID"/>
    </list>
    <joined-subclass abstract="true" name="Container" table="container">
        <key column="ContainerID"/>
        <list name="components" table="container_component_map">
            <key column="ContainerID"/>
            <index column="position"/>
            <many-to-many class="Component" column="ComponentID"/>
        </list>
        <joined-subclass name="Row" table="row">
            <key column="rowID"/>
           
        </joined-subclass>
        <joined-subclass name="Table" table="table">
            <key column="tableID"/>

        </joined-subclass>
    </joined-subclass>
    <joined-subclass name="Cell" table="cell">
      <key column="cellID"/>
     
      <property column="value" name="value"/>
    </joined-subclass>
  </class>
</hibernate-mapping>


_________________
Thanks & Regards,

Vishal Shukla


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 5:12 am 
Newbie

Joined: Fri Feb 20, 2009 6:25 am
Posts: 10
Hey Litty,

Thanks for reply...

Finally solved the problem... actually the xml file and classes send above works perfectly fine..

only problem was adding "cascade" property for list and the I had put ArrayList(not List) inside Setter of the components & containers.

Thanks again....

_________________
Thanks & Regards,

Vishal Shukla


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 6:58 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
May be I didn't really understood ur domain model... But isn't it invalid that the same component can be present in more than one container? Also is it really a valid condition that the same row can occur more than once in a table?

Anyways you have different tables for TABLE, ROW, CELL etc. So rather than making MTM relation b/w container and component isnt it better to have MTO relation from table2row and row2cell?

_________________
Regards,
Litty Preeth


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.