Hibernate - SPLessons

Chapter 10

Hibernate Multiple Databases

Hibernate Multiple Databases

While dealing with big projects hibernate may need more databases then more configuration files will be needed. The driver names also be changed because of every database will consists of own driver names. Dialects also will be changed. Following are the some key points while dealing with hibernate multiple databases.

Example

Create one application, which insert the employee details in two databases, Oracle & MySQL. Create the project directory structure first.

Create the persistence class.

SpLessons.java

package com.itoolsinfo;

public class SpLessons 
{
	private int empId;
	private String empName;
	private String city;
	public int getEmpId() {
		return empId;
	}
	public void setEmpId(int empId) {
		this.empId = empId;
	}
	public String getEmpName() {
		return empName;
	}
	public void setEmpName(String empName) {
		this.empName = empName;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}

}

Set and Get methods are a pattern of data encapsulation. Instead of accessing class member variables directly, one can define get methods to access these variables, and set methods to modify them.

To map the persistence class in mapping file.
splessons.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  
  <hibernate-mapping>
    <class name="com.iquickinfo.Splessons" table="splessons">
      <id name="empId" column="eid">
         <generator class="assigned"/>
      </id>
     
      <property name="empName" column="ename"/>
     <property name="city" column="city"/>
   </class>
  </hibernate-mapping>

The generator classsubelement of id utilized to produce the unique identifier for the objects of persistence class. There are numerous generator classes characterized in the Hibernate Framework. All the generator classes actualizes the org.hibernate.id.IdentifierGenerator interface.

Configure the mapping file in Configuration file.
oracle.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="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
          <property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
          <property name="connection.username">root</property> 
         <property name="connection.password">root</property> 
       
         <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 
        <property name="hibernate.hbm2ddl.auto">create</property>
         <property name="show_sql">true</property>
       
         <mapping resource="splessons.hbm.xml"/>
      </session-factory> 
  </hibernate-configuration>  

 

mysql.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="connection.driver_class">com.mysql.jdbc.Driver</property>  
         <property name="connection.url">jdbc:mysql://localhost:3306/MySQL</property>
         <property name="connection.username">system</property>  
         <property name="connection.password">system</property> 
       
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
         <property name="hibernate.hbm2ddl.auto">create</property>
         <property name="show_sql">true</property>
       
         <mapping resource="splessons.hbm.xml"/>
   		
   	</session-factory>
 </hibernate-configuration>   
Properties Description
hibernate.connection.driver_class The JDBC driver class.
hibernate.dialect This property makes Hibernate generate the suitable SQL for the picked database.
hibernate.connection.url The JDBC URL to the database instance.
hibernate.connection.username The database username.
hibernate.connection.password The database password.
hibernate.connection.pool_size Limits the number of connections waiting in the Hibernate database connection pool.
hibernate.connection.autocommit Allows autocommit mode to be used for the JDBC connection.

Create the employee class and stores the persistence class object.

Employess.java

package com.iquickinfo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Employees 
{
	public static void main(String[] args)
	{
		
	    Configuration configuration1=new Configuration();
	      configuration1.configure("oracle.cfg.xml");
	     
	    Configuration configuration2=new Configuration();
	      configuration2.configure("mysql.cfg.xml");
	      
	    SessionFactory factory1=configuration1.buildSessionFactory(); 
	    SessionFactory factory2=configuration2.buildSessionFactory();
	      
	      Session oraclesession=factory1.openSession();
	      Session mysqlsession=factory2.openSession();
	      
	        Splessons splessons=new Splessons();
	           splessons.setEmpId(414);
	           splessons.setEmpName("  John   ");
	           splessons.setCity("Hyderabad");
	    
	           
	      Transaction transaction1=oraclesession.beginTransaction();
	            oraclesession.save(splessons);
	              transaction1.commit();
	     
	      Transaction transaction2=mysqlsession.beginTransaction();
		            mysqlsession.save(splessons);
		              transaction2.commit();
		              
		               oraclesession.close();
		               mysqlsession.close();
		             
		               factory1.close();
		               factory2.close();
	      
	          }   
}

SessionFactory is an interface, which is accessible in “org.hibernate” package.Session factory is long live multithreaded object. Typically one session manufacturing plant ought to be made for one database. When the developer have different databases in your application, developer ought to make numerous SessionFactory object.

Add all the jar files in build path apart from ojdbc14.jar, mysql-connector-5.2.jar and Hibernate jar files.Run the java application and see the output in command prompt.
Also enter the values into the database like the following.

Output:

Output can be seen in Database table by using the following command.

select * from splessons;

Output will be as follows with following column names.

Summary

  • Hibernate Multiple Databases – While using more databases, then more configuration files are needed.
  • Hibernate Multiple Databases – One configuration file will have one database information like driver name.
  • Hibernate Multiple Databases – Each database will have user name and password that should be mention in each configuration file.