Interacting With Multiple Databases in Hibernate
For Explanation watch video ::
Directory Structure:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>OracleMySQLHB</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>OracleMySQLHB</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc8 -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>21.1.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
hibernate-mysql.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-5.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/new</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
<property name="hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>
hibernate-oracle.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-5.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="hibernate.connection.username">system</property>
<property name="hibernate.connection.password">tiger</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.Oracle12cDialect</property>
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
Person
package com.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "ptab")
public class Person {
@Id
@GeneratedValue
private Integer pid;
private String pname;
private String addrs;
private Double balance;
}
HibernateUtilMySql
package com.utility;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.entity.Person;
public class HibernateUtilMySql {
static SessionFactory factory;
static {
Configuration cfg = new Configuration();
cfg.configure("hibernate-mysql.cfg.xml");
cfg.addAnnotatedClass(Person.class);
factory = cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory() {
return factory;
}
public static Session getSession() {
return factory.openSession();
}
}
HibernateUtilOracle
package com.utility;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.entity.Person;
public class HibernateUtilOracle {
static SessionFactory factory;
static {
Configuration cfg = new Configuration();
cfg.configure("hibernate-oracle.cfg.xml");
cfg.addAnnotatedClass(Person.class);
factory = cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory() {
return factory;
}
public static Session getSession() {
return factory.openSession();
}
}
Save
package com.test;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.entity.Person;
import com.utility.HibernateUtilOracle;
public class Save {
public static void main(String[] args) {
//get the SessionFacotry
SessionFactory factory = HibernateUtilOracle.getSessionFactory();
//get The Session
Session ses = HibernateUtilOracle.getSession();
try(factory;ses){
//create the Object
Person p1 = new Person();
p1.setPname("Sam");
p1.setAddrs("Mumbai");
p1.setBalance(23000.0);
//begin the tx
ses.beginTransaction();
//save the obj
ses.save(p1);
//commit the tx
ses.getTransaction().commit();
}catch(HibernateException he) {
he.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}
}
Transfer
package com.test;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.entity.Person;
import com.utility.HibernateUtilMySql;
import com.utility.HibernateUtilOracle;
public class Transfer {
public static void main(String[] args) {
// get the SessionFacotry
SessionFactory mysqlFactory = HibernateUtilMySql.getSessionFactory();
SessionFactory oracleFactory = HibernateUtilOracle.getSessionFactory();
// get The Session
Session mysqlSes = HibernateUtilMySql.getSession();
Session oracleSes = HibernateUtilOracle.getSession();
try (mysqlFactory;oracleFactory;mysqlSes;oracleSes) {
//get the Object
Person p1 = oracleSes.get(Person.class, 21);
// begin the tx
mysqlSes.beginTransaction();
// save the obj
mysqlSes.save(p1);
// commit the tx
mysqlSes.getTransaction().commit();
} catch (HibernateException he) {
he.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Comments
Post a Comment