4

I am working on an application where it requires to connect to two Postgres databases using hibernate. The details have been given below :-

Software Details :- Java 1.6, Hibernate 4.1.0, Postgres 9.1.

Problem Scenario :-The application needs to connect to two databases TEDA and TEMET on my localhost on the run-time and perform basic CRUD operations (for the initial phase).

Approaches Tried :- 1. Added the catalog attribute to the <class> tag of the hibernate mapping file. But it did not work. The catalog was being fetched as null always. 2. Create two different data sources and session factories in my Hibernate context and use them in different DAO objects. This approach worked for me and I was able to connect to different databases.

Problem Statement and references :- I am looking for a way where the 'catalog' approach mentioned in point (1) may work.

I searched stack-overflow and other forums like Connect to multiple databases in Hibernate and what is "catalog" used in hibernate annotated class, but most of them had a discussion on the approach (2) that uses multiple data sources. I also had a look in the hibernate community documentation in the section 5.1.3, point 6 which does mention that the catalog attribute overrides the database name in the db Url, but may be I am missing something here.

Please assist. The properties file and hibernate mapping file that I had modified are given below.

The properties file -

driverClassName=org.postgresql.Driver
urlDB=jdbc\:postgresql\://localhost\:5432/TEDA    
username=****
password=****
dialect=org.hibernate.dialect.PostgreSQLDialect

The hibernate mapping file

<hibernate-mapping>
 <class name="somepackage.PartnerPoc" catalog="TEMET" table="partner_poc">
  <!-- Table specific hibernate implementation and HQL queries-->
</hibernate-mapping>

Please let me know if you need any more information from my end.

Thanks in advance.

Community
  • 1
  • 1
Amal Gupta
  • 452
  • 4
  • 13

0 Answers0