File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Connecting to two schemas using hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Connecting to two schemas using hibernate" Watch "Connecting to two schemas using hibernate" New topic
Author

Connecting to two schemas using hibernate

Hamsa Kumar
Greenhorn

Joined: Jun 22, 2008
Posts: 3
I am new to hibernate and Struts �. My application use spring , struts and hibernate mapping.

The requirement is to create a new schema and to fetch the data from both the schemas �.. I need to take data from both the schemas�. Both the schemas are having the same set of tables (Table names are same across the schemas)

In my dataAccessContext-jta.xml , datasource is defined as follows�.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc racle:thin:@someserver</value>
</property>
<property name="username">
<value>usrname</value>
</property>
<property name="password">
<value>passwd/value>
</property>
</bean>


My question is whether its possible to connect to two different schema s using hibernate � If so how can we go about it� Please help me in this�

Thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Yes it is, however you will need a SessionFactory per DataSource to do it. You can create a SessionFactory that spans more than one DataSource.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Hamsa Kumar
Greenhorn

Joined: Jun 22, 2008
Posts: 3
Hi,

Thanks for your reply,

Could you please explain how to do this .... As I am new to Spring and hibernate could you please explain the steps...

Thanks,
[ June 23, 2008: Message edited by: Hamsa Kumar ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Hibernate's documentation covers configuration. This is a good place to start.
Hamsa Kumar
Greenhorn

Joined: Jun 22, 2008
Posts: 3
Hi,

Could you please brief on the changes needs to be done. In the dataaccessContext.xml file we have dfined a data source which points to a single database. The Session factory and the transaction manager are mappedd to this data source. How can I define a new datasource and switch the mapping dynamically.



Here Hibernate transaction manager is used. Will this be ok.
Please explain .

Thanks

[ Edited to use code tags - Paul Sturrock ]
[ June 23, 2008: Message edited by: Paul Sturrock ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

I'm afraid I don't know much about Spring. Hibernate itself has a one to one mapping between a SessionFactory and a DataSource, and since Spring delegates to Hibernate I can only assume that you need more than one DataAccessContext? In a non-Sping application I would implement this in my DAO layer by switching between SessionFactories dependent on the object I was maintaining; presumably you know from the type which database it belong in? Sorry I can't be of much help how to do this in Spring.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connecting to two schemas using hibernate