-->
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.  [ 7 posts ] 
Author Message
 Post subject: Dynamic schema mapping
PostPosted: Mon Dec 27, 2004 8:21 pm 
Newbie

Joined: Mon Dec 27, 2004 8:10 pm
Posts: 7
Location: Redwood City, CA
Hibernate version:2.1.7

Name and version of the database you are using:
IBM DB2

I am developing an application for a legacy system where a schema exists for each customer. As new customers are added, new schemas are created to hold their data. I would like to have one class and one mapping file for each unique table, not one for each table in each schema. So, if there is a person table, for instance, in each customer schema then I would like to have one person.hbm.xml mapping, and one Person class. Then, at runtime, I would like to dynamically associate classes with schemas.

It seems that once you've created a SessionFactory, a given class is bound to a given table (within a given schema.) So, I'm guessing that I would have to create a SessionFactory for each customer, and somehow on the configuration object I would need to associate the appropriate schema just prior to building the SessionFactory. But, I'm having a lot of trouble doing this. I tried calling createMappings, then updating the schema on the returned Mappings object, then building the SessionFactory, but no luck. Perhaps I was to do something else with that Mappings object?

If anybody can give me some advice on this I would be very grateful. Also, I saw in another similar post that Hibernate3 might have some functionality for this, but it did not mention what functionality, classes, configurations or whatnot were used, so if anybody could point me in the direction of a specific feature in Hibernate3 that mkes this easier, please do, although my preference for now would be to stick with Hibernate2.x until Hibernate3 is released.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 27, 2004 9:23 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Interesting, I don't think thats easily doable. We should have something like Configuration.setSchemaName(), please open a feature request in JIRA if nobody has an answer in the next days.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 27, 2004 9:24 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Oh, you can set Properties on a Configuration. That would work fine if you use a global hibernate.cfg.xml for the list of mappings.


Top
 Profile  
 
 Post subject: Excellent
PostPosted: Tue Dec 28, 2004 11:58 am 
Newbie

Joined: Mon Dec 27, 2004 8:10 pm
Posts: 7
Location: Redwood City, CA
That works, and I also found that I can loop through the classMappings collection, setting the schema on each, just prior to building the SessionFactory. Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 11:49 am 
Beginner
Beginner

Joined: Fri Apr 15, 2005 3:08 pm
Posts: 26
I have a similar, but slightly more complicated issue with multiple schemas. Like sblaes, I too have a separate schema for each customer; for example, CUSTOMER_1, CUSTOMER_2...CUSTOMER_N. However, I also have a common schema which holds data common to all customers, such as lookup data.

From reading the above messages, it sounds like I can build a SessionFactory with a different schema for each customer schema. That's great. However, in some sessions I will need to access a customer schema and the commom schema at the same time. Furthermore, due to inheritance, the customer schemas and the common schema will have some of the same tables. For example, I have the following class hierarchy (this is greatly simplified for this discussion):

abstract class Person
class User extends Person
class CustomerContact extends Person

Person is mapped to COMMON_SCHEMA.PERSON and also CUSTOMER_X.PERSON (i.e. one PERSON table for each customer schema). User is mapped to COMMON_sCHEMA.USER using "joined-subclass" to Person (COMMON_SCHEMA.PERSON). CustomerContact is mapped to CUSTOMER_X.CUSTOMER_CONTACT using "joined-subclass" to Person (CUSTOMER_X.PERSON).

The problem is, User has a one-to-many association with Person. How can I load a CustomerContact instance that includes the associated User? Person would have to be SIMULTANEOUSLY mapped to two different tables in different schemas in the same session.

Suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 11:54 am 
Beginner
Beginner

Joined: Fri Apr 15, 2005 3:08 pm
Posts: 26
Perhaps I should add that I am using Hibernate2.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 19, 2006 3:32 pm 
Regular
Regular

Joined: Mon Jun 13, 2005 12:21 pm
Posts: 61
Location: Hannover
I know this post is pretty old, have you found a solution?


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