Hibernate Application Using Oracle Database

Develop Hibernate Application, in which we can perform account retrieve operation from Account table on Oracle Database?

Account Table in Oracle Database:


Structure of Hibernate Application:

public class Account {
 private int accNumber;
 private String name;
 private double balance;

 public int getAccNumber() {
  return accNumber;
 }

 public void setAccNumber(int accNumber) {
  this.accNumber = accNumber;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 @Override
 public String toString() {
  return "Account [accNumber=" + accNumber + ", name=" + name
    + ", balance=" + balance + "]";
 }

 public double getBalance() {
  return balance;
 }

 public void setBalance(double balance) {
  this.balance = balance;
 }
}
The Account Persistence class shown in the preceding code declares three persistence fields accNumber, name, balance of types int, String, double respectively.
As discussed earlier, Hibernate requires a small amount of meta data description for persistent class, which is specified using the Account.hbm.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<!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.gurugubelli.pojo.Account" table="account">
  <id name="accNumber">
   <column name="accountNumber" />
  </id>
  <property name="name" length="20" />
  <property name="balance" column="balance" />
 </class>
</hibernate-mapping>

The Account.hbm.xml file describes the mappings for Account persistence class. As discussed earlier, it is recommended to save the mapping file name <persistent class name>.hbm.xml. However, it is not mandatory to follow this convention. We can use any filename.

Note:

  • Xml is a case –sensitive mark-up language.
  • Every Xml file contains the root tag. And whatever tag  we  are  opened in the xml compulsory we can close.
  • Xml file contains either DTD (Document Type Definition) or XSD (Xml Schema Definition).
  • DTD and XSD are techniques used to frame the rules that are required to construct an xml document.
  • Now, let us configure the Hibernate configuration file by specifying the configuration parameters such as connection details, hibernate properties and mapping information.
<?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.url">jdbc:oracle:thin:@localhost:1521:XE</property>
  <property name="connection.username">gurugubelli</property>
  <property name="connection.password">gurugubelli</property>
  <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver </property>
  <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
  <property name="show_sql">true</property>
  <property name="hbm2ddl.auto">create</property>
  <mapping resource="Account.hbm.xml" />
 </session-factory>
</hibernate-configuration> 

The hibernate.cfg.xml configuration file is using oracle Jdbc thin driver to connect to database for persistence operation. Here we have configured Hibernate properties dialect, show_sql and hbm2ddl.auto properties.
 show-sql: if the 'show_sql' value is true we can view all the hibernate generated SQL queries in the console.
 hbm2ddl.auto: It has the following values
a. create
b. update
c.  create-drop
a) create: If its value is create while running the application
Case 1: table does not exist
Hibernate creates a new table based on the mapping file configurations.
Case 2: table exists
Hibernate drops the existing table and creates a new table based on the mapping file configurations
b) Update: If its value is update while running the application
Case 1 : table does n't exist
Create a new table based on the mapping file configurations
Case 2 : table exists
Hibernate Uses the existing table
c)  create-drop :
if hbm2ddl.auto property value is create-drop then hibernate creates the a new table when the session-factory object is created. And drops the table when the sessionFactory is closed.

package com.gurugubelli.client;

import java.io.Serializable;

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

import com.gurugubelli.pojo.Account;

public class TestApp {
 public static void main(String[] args) {
  Configuration configuration = new Configuration()
    .configure("hibernate.cfg.xml");
  SessionFactory factory = configuration.buildSessionFactory();
  Session session = factory.openSession();

  TestApp testApp = new TestApp();

  Account accountObj = new Account();
  accountObj.setAccNumber(1234);
  accountObj.setName("Gurugubelli");
  accountObj.setBalance(30000);

  int accountNumber = testApp.createAccount(session, accountObj);
  System.out.println("Account number created with: " + accountNumber);

  Account account = testApp.getAccountDetails(session, accountNumber);
  if (account.getAccNumber() >= 0) {
   System.out.println("\n===============");
   System.out.println("Account Details");
   System.out.println("===============");
   System.out.println("Name   :" + account.getName());
   System.out.println("Number :" + account.getAccNumber());
   System.out.println("Balance:" + account.getBalance());
  } else {
   System.out.println("Account " + accountNumber + " Not Found");
  }

  session.close();
 }

 public int createAccount(Session session, Account accountObj) {
  Serializable number = session.save(accountObj);
  session.beginTransaction().commit();
  return (int) number;
 }

 public Account getAccountDetails(Session session, int accountNumber) {
  Object accountObj = session.get("com.gurugubelli.pojo.Account",
    accountNumber);
  Account account = (Account) accountObj;
  return account;
 }
}

To execute the above application we need to set the following jar files in the class path: (download hibernate-release-x.x.x.Final.zip file and extract, In that folder add the .jar libraries under lib\required folder):



Remember: Along with the hibernate jars we must include one more jar file, which is nothing but related to our database, this is depending on your database.
Ex:  ojdbc14.jar/ojdbc6.jar/ojdbc7.jar file


Hibernate Application Using Oracle Database Hibernate Application Using Oracle Database Reviewed by Gurugubelli Technologies on January 01, 2017 Rating: 5

1 comment:

  1. Learned a lot of new things from your post!Good creation ,thanks for good info Oracle SOA Online Training

    ReplyDelete

Powered by Blogger.