Connect Multiple Database using hibernate



1.Create 2 hibernate config xml for each database connection

here i have 2 database studentDetails and Collages

the hibernate config xml files are given below

collage.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/collageDetails</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">qwerty</property>
<!-- Data table objects used in project -->
<mapping class="com.inzane.example.dto.Collage"/>
</session-factory>
</hibernate-configuration>


student.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/studentDetails</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">qwerty</property>
<!-- Data table objects used in project -->
<mapping class="com.inzane.example.dto.Details"/>
</session-factory>
</hibernate-configuration>


2.Connect these Both xml file in HibernateUtil.java

HibernateUtil


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.inzane.example.util;

import org.hibernate.Session;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;

/**
* Hibernate Utility class with a convenient method to get Session Factory
* object.
*
* @author Krishnadas V A
*/
public class HibernateUtil {

private static final SessionFactory student;
private static final SessionFactory collage;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
student = new AnnotationConfiguration().configure("student.cfg.xml").buildSessionFactory();
collage = new AnnotationConfiguration().configure("collage.cfg.xml").buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getStudent() {
return student;
}
public static Session getSessionStudent() {
return student.openSession();
}
public static SessionFactory getCollage() {
return collage;
}
public static Session getSessionCollage() {
return collage.openSession();
}
}

Comments

  1. SERIOUSLY?! HIBERNATE?! Wow that is pretty awesome!!! It surely excites the geeky inside of me… Cheers!
    DCLand.info

    ReplyDelete

Post a Comment

Popular posts from this blog

Python mechanize For Browsing

Django Dynamic Formsets with Jquery

Editor TinyMCE in Django admin