-->
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: Class mapping across multiple SessionFactory instances
PostPosted: Mon Jan 23, 2006 8:08 pm 
Newbie

Joined: Sun Jan 22, 2006 11:46 pm
Posts: 8
Hi all. I'm a bit of a newb to Hibernate and really like what I’m finding. However, I’ve got a bit stuck on the following issue using Hibernate 3.1.

I have two configuration files each initialised with their own SessionFactory (due to their being 2 separate databases). One .cfg is mapped to our static data database and has an object called CurrencyData in it. The other .cfg initialises my Asset.hbm.xml file which in turn describes the relationship with CurrencyData as an attribute in the following manner:
Code:
<many-to-one name="Currency" class="eg.currency.CurrencyData" column="CurrencyID" lazy="false" />


Issue I have is that because CurrencyData is mapped in my Static.cfg and Asset is in my Asset.cfg file I get binding errors when I try to reference the AssetData complaining that CurrencyData doesn’t exist. It does exist, but in a different SessionFactory.

Is what I’m attempting to do feasible? Is there some part of Hibernate I’m not understanding? Is there a “global” view of session factories which can resolve this?

Any help on this but would be really appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 23, 2006 8:54 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
There's no global view: SessionFactories are heavyweight objects designed to fully encapsulate the world view.

You have options:

- If the different databases are on the same server, just use one SessionFactory. You can fully qualify table names by database: you might have CurrencyData specify table="staticdata.dbo.CurrencyData" to show that the CurrencyData table isn't in the database to which the session connects.

- If the "static data database" contains only immutable types, you can define UserTypes for all those types, and not have a connection to that other DB at all. If your UserTypes are all enums, you even get a sort of referential integrity courtesy of java5's enums.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 10:45 pm 
Newbie

Joined: Sun Jan 22, 2006 11:46 pm
Posts: 8
Thought as much. My hope was that Hibernate would recognise the the CurrencyData was in another Session enabling me to Map the relationship via my AssetData.xml file while having Hibernate execute 2 calls - one on each factory - utlimately returning me a fully populated object.

Unfortunately we don't have control over the names of the databases so can't use the naming suggestion you made.

Anyways, due to all access being via EJB's, we simply put a completeAsset() method in place which populated the static data objects from the other SessionFactory. Bit hokey, but there you go.

Thanks for the prompt reply btw.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 11:13 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
No worries.

Not having control over the name of the database is irrelevant: all that matters is that the two databases are on the same server. If that's the case, then you can refer to tables in other databases by using the [databasename].dbo.[tablename] syntax, instead of the simple [tablename] syntax that you can use if the table is in the database which the session is using (has done a "use" on). However, your solution is equally good. It simply uses your cache instead of hibernate's.


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.