-->
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.  [ 5 posts ] 
Author Message
 Post subject: Hibernate support for dynamic table names
PostPosted: Wed Apr 14, 2010 3:30 pm 
Newbie

Joined: Wed Apr 14, 2010 3:22 pm
Posts: 2
Hi guys,

I'm new to hibernate, so I need some advice to decide if I can
implement it for my system.
I have a huge database, containing lots of articles in different
tables. All the table structures are the same, except the table names,
because each table belongs to a different magazine. I know many would
say that it is better to keep one article table with a distinguishing
column for a magazine, but now every table may contain thousands of
records, and it was done for performance reasons.
So, the tables are named `1`, `2`, `3`, `4`, etc, and has columns like
article_id, title, lead, body, etc.

Now the question is, I was wondering if I would be able to map those
tables in Java using hibernate, say, having an entity called
Article.java, but somehow dynamically map it to a necessary table in
the database during the run. And moreover, what would happen if I
needed two articles from two different tables, say
magazines.`1`.article_id=23 and magazines.`323`.article_id=12
Is it even feasible?

I have implemented it all manually now, using jdbc and simple sql
select statements, and in my selects I just supply the table names on
the run.
But as far as I know, Hibernate uses a hibernate-mapping file, where
the table names are hardcoded.

Highly appreciate any advice!
Thanks a lot in advance

/progre55


Top
 Profile  
 
 Post subject: Re: Hibernate support for dynamic table names
PostPosted: Thu Apr 15, 2010 2:10 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
but now every table may contain thousands of
records, and it was done for performance reasons.


Thousands of records should not be any problem for any decent database to handle. Not even millions if properly indexed should be any problem. I suggest you use one table only and add a magazine_id column to it.


Top
 Profile  
 
 Post subject: Re: Hibernate support for dynamic table names
PostPosted: Thu Apr 15, 2010 5:02 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
but now every table may contain thousands of
records, and it was done for performance reasons

also clever databases can split such a tables by DB configuration, like you can set a rule "split on magazine_id" to have physically separated tables/stores/disks/... and you can still query them all as one in the rare occasions you would like to, or it will be clever in handling a "where magazine_id"..

If this is really huge and your database doesn't support this clever options (don't remember the list now) you might have a look into Hibernate Shards which provides a similar solution at a higher level. Never a good idea to have to deal with these details in your code :)

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Hibernate support for dynamic table names
PostPosted: Mon May 10, 2010 12:00 pm 
Newbie

Joined: Wed Apr 14, 2010 3:22 pm
Posts: 2
so, if I'm not changing the database structure, are you saying it is not possible to implement hibernate? having the current database structure..


Top
 Profile  
 
 Post subject: Re: Hibernate support for dynamic table names
PostPosted: Mon May 10, 2010 1:43 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
It is possible to use Hibernate, but it may not be as smooth as if you only had one table. It is possible to map a single class to multiple tables using the entity-name attribute as described by http://docs.jboss.org/hibernate/stable/ ... entityname. As far as I know you are limited to use XML mappings files, but you'll need one mapping file for each table with articles. You can probably write some kind of script to automatically generate those. It should even be possible to generate the proper XML when your application starts and then register with Configuration.addDocument().

When you are handling Article objects, you'll also need to keep track of which entity-name they belong to. I don't think Hibernate does this since you need to use the two-argument methods like Session.get(entityName, id), Session.save(entityName, object), etc. You may want to have a look at the org.hibernate.Interceptor interface. There are some entity-name-related methods that may be useful. Note that I don't have much experience with this so I am not really aware of what may pop up or any smart solutions.


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