-->
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.  [ 11 posts ] 
Author Message
 Post subject: mapping-problem: dynamic table names (1_users, 2_users, ...)
PostPosted: Sun Mar 06, 2005 8:24 pm 
Newbie

Joined: Sun Mar 06, 2005 7:57 pm
Posts: 4
Hi!

Just started working with Hibernate: I'm wondering if (and how) it's possible to map the same class (here: Users) to different tables, depending on a member variable's value (here: int applicationId).

I've found quite some postings in that direction, but no obvious solution so far:
http://forum.hibernate.org/viewtopic.php?t=938904
http://forum.hibernate.org/viewtopic.php?t=933049

I guess what I am looking for is a way to construct the <class' table attribute dynamically, in my case based on a simple String concatination. So instances of User which belong to application #1 (i.e. having applicationId=1) should be mapped to table "1_users" etc.

Any ideas, hints, examples are highly appreciated! :-)

Thanks for your help,
Matthias


P.S.: Some more background information: We currently have a DB structure like this in place with a proprietary application server which is used for running various projects from different clients, which was one of the reasons to keep the same types of data in separate tables. The RDBMS in use is MySQL. Currently we don't use any O/R mapping but work with custom SQL queries.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 06, 2005 8:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You can map the class twice, with different entity-names, and implement Interceptor.getEntityName()


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 07, 2005 6:04 am 
Newbie

Joined: Sun Mar 06, 2005 7:57 pm
Posts: 4
Thanks for your quick response, gavin! Is there a way to put this even one step further, so I don't need to define 20 entity-names if the server uses "1_users" ... "20_users"? I'll look into the Interceptor implementation now...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 23, 2005 9:36 pm 
Newbie

Joined: Wed Mar 23, 2005 9:25 pm
Posts: 4
I'd also be interested in finding a solution to this problem. I'm working on a highly-transactional system. Each user will generate roughly 10k rows per year, which equates to 10M rows per year for 1000 users. To improve concurrency we had to split each user's data out into a separate table.

I stared at the docs, the FAQs and forum posts for a couple of hours today and can't seem to find a way to determine the table name for a class at runtime. Is it impossible? It would be pretty tedious to map the same class 1000 times. In addition, we need to be able to create user accounts at runtime.

Am I asking too much? :-)

Boris


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 24, 2005 5:15 am 
Newbie

Joined: Sun Mar 06, 2005 7:57 pm
Posts: 4
Oups, and I thought that mapping twenty or so different tables is quite a lot already. :-)

Mapping the same class multiple times with different entity names and implementing Interceptor.getEntityName() worked in my case. However, it's still kind of a hack, because when building queries i need to change the table name manually instead of going with the attributes, e.g.:

session.createQuery("select u from user_" + appid + " as u where u.id = :id");


So, being able to determining the table name for a class at runtime in a more elegant way (without the need to write multiple mappings "manually") would be really great :-)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 24, 2005 6:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
if the mappings are exactly the same and only differ on some strings that follows a certain patttern then I know that computers are pretty good at copy, search and replace, save ,)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 24, 2005 1:47 pm 
Newbie

Joined: Wed Mar 23, 2005 9:25 pm
Posts: 4
max wrote:
if the mappings are exactly the same and only differ on some strings that follows a certain patttern then I know that computers are pretty good at copy, search and replace, save ,)


Two people point out a useful feature that's missing from the product and the Hibernate team responds with a snide remark? Great. Feel free to tell us why what we're suggesting is a bad idea, but don't talk to us like we're morons.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 24, 2005 2:47 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
It was a friendly comment as I saw it as the much better solution than to complicate the mapping process.

Hibernate would still need to a runtime generate the same thing for the Configuration to process which you just as easily could do with a simple ant task or a String.replace() call.

Sorry if the failed humor offended you....but please next gime give people the benefit of the doubt before being offended! (If I didn't do that I wouldn't survive these forums :)

-max

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 24, 2005 3:34 pm 
Newbie

Joined: Wed Mar 23, 2005 9:25 pm
Posts: 4
max wrote:
Sorry if the failed humor offended you....but please next gime give people the benefit of the doubt before being offended! (If I didn't do that I wouldn't survive these forums :)


Ugh, I'm sorry as well for being a too highly-strung. Shouldn't have had that extra cup of coffee this morning.

I appreciate you guys taking the time to help someone who hasn't worked with the product yet. I'm worried that this thing with our data split into multiple tables might be a deal-breaker for me. I'm trying to save myself some time before I start the proof-of-concept with Hibernate.

If I were to go with this idea of having thousands of mappings for the same class, would I be able to create these mappings at runtime? The way that our system is set up, when a new user account is created, we dynamically create a new set of tables for the user data.

Is Configuration.addFile() the right thing to look at? Is it thread-safe? Can it be called on a running system?

Any replies would be greatly appreciated, with or without humor. :-)

Boris


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 24, 2005 3:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Everything can be created at runtime if you want.

But remember the following:

A Configuration is basically what is built up when hibernate parses a hbm.xml file. And here you can add any amount of mappings you want. You can even add a xml document as a dom4j object if you want to - so lots of possibilities.

A SessionFactory is a *compiled* version of what is in a Configuration. That means that after a SF have been built changees to the configuration does not affect the SF - you will have to rebuilt a new SessionFactory to use the new mappings. (and that is not a cheap operation)

I dont know how you app is built up, but maybe you could work by having multiple SessionFactories - one per customer ? It sounds like they are pretty separate anyway ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 24, 2005 4:22 pm 
Newbie

Joined: Wed Mar 23, 2005 9:25 pm
Posts: 4
max wrote:
I dont know how you app is built up, but maybe you could work by having multiple SessionFactories - one per customer ? It sounds like they are pretty separate anyway ?


That could work. I could have one global SessionFactory for the global data and a separate SessionFactory for each user account. It's great that these objects don't have static context.

I suspect I'd lose the ability to have transactional operations that update both global data and user data. I'll have to do some more thinking to determine if I can work around this limitation.

Thanks again for your help!

Boris


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