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)

package com.studyskymate.hib;

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

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

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

     private String firstName, lastName;

     public int getId() {
           return id;

     public void setId(int 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;


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());




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC 
          "-//Hibernate/Hibernate Configuration DTD 5.3//EN" 
  <property name="">update</property> 
    <property  name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

 <property name="connection.url">
     <property name="connection.username">hr</property> 
     <property name="connection.password">hr</property> 
     <property  name="connection.driver_class">
     <property name="show_sql">true</property>
     <mapping class="com.studyskymate.hib.Employees"/>       

Run TestHibernate2 Class:


INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select 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


Create a Digital Clock using HTML and JavaScript

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


Search This Blog

Popular Posts