-->
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.  [ 6 posts ] 
Author Message
 Post subject: Single class mapped to two datasources in Hibernate?
PostPosted: Tue Jan 17, 2006 6:31 pm 
Newbie

Joined: Tue Jan 17, 2006 5:46 pm
Posts: 7
Hello,

I've got a situation that somebody must've solved before, but unfortunately can't find anywhere in these forums.

I have a persistant class with a large number of fields, between 20 and 25, that gets "derived" from one data source (think of this as a production DB), then persisted in another data source (a reporting DB). (I realize having the data in two places isn't ideal, but we need to insulate the production DB from internal reporting requests.)

Before switching over to Hibernate, I handled this with a home-baked AsymetricPersistantObject, which took two sets of DB fieldNames, one from Database A for use by a Factory.deriveAll() method, the other from Database B for use in a various CRUD operations (.load(), .find(), .store(), etc.). My thought for doing this with Hibernate was to define two mappings for my persistant class, one to the Production DB table, and a second to the Reporting DB table; then have a single DAO that held references to two DAOs (I'm using Spring), and handle the "derivation" something like so:

List allObjects = productionDAO.loadAll(PersisentClass.Class);
productionDAO.clear(); //detach allObjects, shouldn't be necessary
reportingDAO.storeOrUpdateAll(allObjects);

Problem is, Hibernate won't let me map a single POJO class twice. I tried getting around this by subclassing PersistantClass and mapping the subclass to the Production DB, then upcasting and saving as the new instances to the Reporting DB, but that shenanigan didn't work either.

Does anybody have a Hibernate solution for this? I know I could create two wholly independent classes, map them separately, and add a .createReportingClone() method to the "derived" version of the class, which just creates a new instance of the Reporting version and transfers over its contents; then loadAll(), loop-create the Reporting objects, and saveAll(); but that's awfully yucky.

Thanks in advance,
Rogers


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 10:16 pm 
Newbie

Joined: Tue Jan 10, 2006 1:30 pm
Posts: 14
Is this what you were hoping to do? (taken from the Hibernate docs)

5.3. Mapping a class more than once
It is possible to provide more than one mapping for a particular persistent class. In this case you must specify an entity name do disambiguate between instances of the two mapped entities. (By default, the entity name is the same as the class name.) Hibernate lets you specify the entity name when working with persistent objects, when writing queries, or when mapping associations to the named entity.

<class name="Contract" table="Contracts"
entity-name="CurrentContract">
...
<set name="history" inverse="true"
order-by="effectiveEndDate desc">
<key column="currentContractId"/>
<one-to-many entity-name="HistoricalContract"/>
</set>
</class>

<class name="Contract" table="ContractHistory"
entity-name="HistoricalContract">
...
<many-to-one name="currentContract"
column="currentContractId"
entity-name="CurrentContract"/>
</class>
Notice how associations are now specified using entity-name instead of class.

_________________
Brian


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 12:24 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If both databases are on the same server, you don't even need to set up another datasource. Just set the table name of the "other" class to otherdatasource.dbo.tablename.

So now you have two classes that are identical: write a copy method that creates otherdatasource objects from originaldatasourceobjects, then save the new objects. Hibernate will look after the rest.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 11:21 am 
Regular
Regular

Joined: Sat Nov 06, 2004 5:20 pm
Posts: 54
Location: Collierville, TN
In the hibernate-mapping-3.0.dtd it says that
Quote:
"No class may be
mapped more than once. "


see your hibernate-3.1/src/org/hibernate/hibernate-mapping-3.0.dtd


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 12:38 pm 
Senior
Senior

Joined: Wed Aug 17, 2005 12:56 pm
Posts: 136
Location: Erie, PA (USA)
I have solved this by creating multiple session factories (& multiple configurations). Each session factory references the same mapping but a different datasource.

Curtis ...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 3:11 pm 
Newbie

Joined: Tue Jan 17, 2006 5:46 pm
Posts: 7
Thanks Brian- this is exactly what I needed. The Hibernate docs ... who'd have thought to look there?! :)


cokerbl wrote:
Is this what you were hoping to do? (taken from the Hibernate docs)

5.3. Mapping a class more than once
It is possible to provide more than one mapping for a particular persistent class. In this case you must specify an entity name do disambiguate between instances of the two mapped entities. (By default, the entity name is the same as the class name.) Hibernate lets you specify the entity name when working with persistent objects, when writing queries, or when mapping associations to the named entity.


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