Discussions
EJB programming & troubleshooting: HIBERNATE: how to mapping a class with several tables?
-
HIBERNATE: how to mapping a class with several tables? (6 messages)
- Posted by: mo li
- Posted on: December 06 2003 09:12 EST
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)
- HIBERNATE: how to mapping a class with several tables? by mo li on December 07 2003 02:42 EST
- HIBERNATE: how to mapping a class with several tables? by Howard Hill on December 07 2003 09:07 EST
- Thanks for replying by mo li on December 09 2003 08:41 EST
- HIBERNATE: how to mapping a class with several tables? by Balamurugan K on December 08 2003 23:19 EST
- Thanks for reply by mo li on December 09 2003 08:43 EST
- Here's an example by Pedro Santos on June 18 2010 05:47 EDT
-
HIBERNATE: how to mapping a class with several tables?[ Go to top ]
- Posted by: mo li
- Posted on: December 07 2003 02:42 EST
- in response to mo li
up -
HIBERNATE: how to mapping a class with several tables?[ Go to top ]
- Posted by: Howard Hill
- Posted on: December 07 2003 09:07 EST
- in response to mo li
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. -
Thanks for replying[ Go to top ]
- Posted by: mo li
- Posted on: December 09 2003 08:41 EST
- in response to Howard Hill
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. -
HIBERNATE: how to mapping a class with several tables?[ Go to top ]
- Posted by: Balamurugan K
- Posted on: December 08 2003 23:19 EST
- in response to mo li
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. -
Thanks for reply[ Go to top ]
- Posted by: mo li
- Posted on: December 09 2003 08:43 EST
- in response to Balamurugan K
I'd like to wait JDO 2.0 rather than use it soon. -
Here's an example[ Go to top ]
- Posted by: Pedro Santos
- Posted on: June 18 2010 05:47 EDT
- in response to mo li
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).