HIBERNATE: how to mapping a class with several tables?

Discussions

EJB programming & troubleshooting: HIBERNATE: how to mapping a class with several tables?

  1. HIBERNATE: how to mapping a class with several tables? (6 messages)

    HOW can I make a class associate with several similar table ?
      
    It is a big problem for me.

    my project is a refactoring project to a old system.
    in the old system, we create a table by each year.

    such as: TBL_NEWS_2003, TBL_NEWS_2002

    so when we need search in years, we use UNION to select from all table.

    BUT how can i do this in hibernate??

    When one develop a website such as a news website.
    It its very easy to decide making a generic News.class to
    fit for all kinds of news.
    eg. A news may have title, content, date ...

    so we develop a News.class.
    but we have different news, like entertaiment, sport, government, techinical .....

    so, we must have dynamically mapping to News.class

    News.class should associate to several *.hbm.xml file, each file figure to a table.
    like News.sport.hbm.xml, News.entertaiment.hbm.xml

    (May be we can specify the different table name just in the filename)

    and the Configuaration can read all xml
    and one can query News.class in all different table or only one table.

    BUT actually I must make a News.hbm.xml for it.
    and i wrtie table name in the XML.

    OR when i query some news, can I specify a tablename or modualname parameter to the Session and get the same News.class from different table.

    can i make myself understand?

    Anyone who can give me the solution??

    Thank you!

    Threaded Messages (6)

  2. up
  3. You are right about making a generic News.class, this class can be the base class
    and you have your subclass like entertaiment, sport, government, techinical.

    Hibernate allows you have a base class and its subclass mapped in one News.hbm.
    This feature is enhanced by using a discriminator value.(Read the Documenation @hibernate.org).

    This will help you with the different types of news. so you can create an instance of and entertaiment news, sports etc and persist them, instead of having multiple hbm.xml files.

    One more thing, why do you have for each year of news a seperate table ?
    I dont this is a good database design.
    Does'nt each news item have a date, so your can query by date , or whatever you get the idea.
  4. Thanks for replying[ Go to top ]

    You are right about making a generic News.class, this class can be the base class

    > and you have your subclass like entertaiment, sport, government, techinical.
    >
    > Hibernate allows you have a base class and its subclass mapped in one News.hbm.
    > This feature is enhanced by using a discriminator value.(Read the Documenation @hibernate.org).
    >
    > This will help you with the different types of news. so you can create an instance of and entertaiment news, sports etc and persist them, instead of having multiple hbm.xml files.
    >
    > One more thing, why do you have for each year of news a seperate table ?
    > I dont this is a good database design.
    > Does'nt each news item have a date, so your can query by date , or whatever you get the idea.

    because the system is programming based on a exists system.
    the system has some bad smells.

    The problem is how can i make a class dynamic mapping with differents table?
    can you show me an example of custom discriminator ?

    I don't know much about it.
  5. by using JDO(java Data Objects) u can convert XML into objects and objects into XML files.By using jdo u can merge serveral tables,u don't worry abt forming XML data ,the JDO will take care of it.
  6. Thanks for reply[ Go to top ]

    I'd like to wait JDO 2.0 rather than use it soon.
  7. Here's an example[ Go to top ]

    I had to do this a while back and it really is helpful. I have a generic class<->table mapping called Voter. On this class' mapping I put

    <discriminator column="specific"></discriminator>

    which is a column in the generic class. For each subclass, the mapping is simply

     <subclass name="org.example.SubClass" extends="org.example.SuperClass" discriminator-value="dicriminating value" dynamic-update="true">
               <join table="sub_class">
                <key column="super_class.id" />
                <property name="birthDate" column="birth_date" type="timestamp" not-null="true"></property>
                <property name="socialSecurityNumber" column="social_security_number" type="string" not-null="true"></property>
            </join>
        </subclass>

    The properties in the SubClass mapping are the extra fields of the sub_class table. The discriminator-value is, in your case, the year (fo example).

    It's that simple (after you know, i mean).