Wednesday, October 16, 2019

Fetch the Data from Database using Hibernate Query


Fetch the Data from Database using Hibernate Query


Here we are going to fetch the data of table emp1000 from the database using hibernate Query


Prerequisite: Hibernate libraries and ojdbc.jar already linked to the project.(Hibernate save data example)





 Employees.java


package com.studyskymate.hib;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

//POJO
@Entity
@Table(name = "emp1000")
public class Employees {

     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     private int id;

     private String firstName, lastName;

     public int getId() {
           return id;
     }

     public void setId(int id) {
           this.id = id;
     }

     public String getFirstName() {
           return firstName;
     }

     public void setFirstName(String firstName) {
           this.firstName = firstName;
     }

     public String getLastName() {
           return lastName;
     }

     public void setLastName(String lastName) {
           this.lastName = lastName;
     }
}



2.        TestHibernate2.java

package com.studyskymate.hib;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.query.Query;

public class TestHibernate2 {

      public static void main(String[] args) {


StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();

Metadata meta = newMetadataSources(ssr).getMetadataBuilder().build();

SessionFactory factory = meta.getSessionFactoryBuilder().build();

            Session session = factory.openSession();

            Transaction t = session.beginTransaction();
        
           
            //Here this query will fetch data from Employees Table
            Query q = session.createQuery("from Employees");

            //fetching list of Employees class objects
            List<Employees> list = q.list();

            //if list has data then iterate
            if (list != null && !list.isEmpty()) {

                  //iterate over list and fetch Employee Data
      
            for (Employees e : list) {

                System.out.println("Id: " + e.getId());
                System.out.println("Name: " +
                e.getFirstName() + " " +e.getLastName());

                  }
            }

           factory.close();
           session.close();
     }
}



Hibernate.cfg.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC 
          "-//Hibernate/Hibernate Configuration DTD 5.3//EN" 
          "http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd"> 
 
<hibernate-configuration> 
 
    <session-factory> 
        
  <property name="hbm2ddl.auto">update</property> 
    <property  name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

 <property name="connection.url">
jdbc:oracle:thin:@CEIT-SRV-1.fnu.local:1521/orcl.fnu.local</property> 
        
     <property name="connection.username">hr</property> 
     <property name="connection.password">hr</property> 
     <property  name="connection.driver_class">
     oracle.jdbc.driver.OracleDriver</property>     
     <property name="show_sql">true</property>
     <mapping class="com.studyskymate.hib.Employees"/>       
      
    </session-factory> 
 
</hibernate-configuration> 




Run TestHibernate2 Class:

Output:

INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select employees0_.id as id1_0_, employees0_.firstName as firstName2_0_, employees0_.lastName as lastName3_0_ from emp1000 employees0_
Id: 1
Name: Dinesh Kumar
Id: 2
Name: Rahul Verma



1 comment:

  1. As Internet is shrinking our world into a Global Village, E-commerce is growing by leaps and bounds. In fact in today's techno-savvy world, Internet and E-commerce are the two sides of the same coin pohick org

    ReplyDelete

Create a Digital Clock using HTML and JavaScript

Create a Digital Clock using HTML and JavaScript  <! DOCTYPE html> < html > < head > ...

Followers

Search This Blog

Popular Posts