-->
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.  [ 4 posts ] 
Author Message
 Post subject: Multiple databases, multiple object mappings
PostPosted: Wed Aug 15, 2007 7:44 am 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
Apologies if this has been asked / answered somewhere, I've had a good trawl.

I'm currently evaluating whether NHibernate can do what we require, and have a few questions.

We need to be able to store & retrieve objects from separate databases (which frmo stuff on the web does seem to possible), but for each database the mappings for objects are slightly different. As a trivial example:

class Test
{
public int id;
}

// mapping 1 to database test_1
<class name="Test" lazy="false" table="test">
<id name="id" access="field">
<column name="id" not-null="true" sql-type="int(14)" />
<generator class="increment" />
</id>
</class>

// mapping 2 to database test_2
<class name="Test" lazy="false" table="[b]test_other_format[/b]">
<id name="id" access="field">
<column name="[b]test_id[/b]" not-null="true" sql-type="int(14)" />
<generator class="increment" />
</id>
</class>

Any help on whether this is possible / inadvisable / clear as mud would be greatly appreciated.

Thanks,

Steve


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 16, 2007 12:41 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
The only limitation is that you wouldn't be able to use the same ISessionFactory for both sets of mappings. In general an ISessionFactory can only support one database at a time. If it is the case that you'll need to access one database per application instance, but you only need to choose the schema at runtime, then what you need to do is certainly supported with NH. It isn't the base configuration, but not hard either.

A quick sketch of one possible approach for that: build two (or n) resource assemblies, one for each schema version. Then at the point in your application when NH is configured and the ISessionFactory built, you can determine which schema you need (either from an indicator in the DB or from your .config file), and call AddAssembly() with the approprate resource assembly.

Variations can also be acheived using AddFile() and AddResource() for your mappings.

I have done something like this on a smaller scale: I have one class that can vary between two table schemas. So, I named the two mapping resources for it with a '.<schema indicator>.ohbm.xml' extension. The 'ohbm' ensures that they aren't loaded with the rest with AddAssembly(), and then I can choose which to load using AddResource() and the schema indicator from my .config.

Good luck. I'm sure you'll find NH to be an invaluable tool.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 16, 2007 1:03 pm 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
Thanks. Yeah, after some poking around I came to the same conclusion and did get it to work. It's a little bit of a pain having to manually add the resource files but not too bad; perhaps in the future there'll be some way of specifying multiple configurations.

I'm definitely leaning towards using NH over a dataset solution.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 06, 2007 12:43 pm 
Newbie

Joined: Thu Dec 06, 2007 9:08 am
Posts: 2
I know the problem I ran into when trying a similar approach had to do with the Ids. Did you manage to figure out a good way of squeezing the ids from both tables into the one object?

Fred


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