Showing posts sorted by date for query Hibernate. Sort by relevance Show all posts
Showing posts sorted by date for query Hibernate. Sort by relevance Show all posts
Wednesday, November 6, 2019
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();
}
}
Thursday, June 27, 2019
Hibernate demo by using XML
Hibernate Demo by using XML
Source Code: https://github.com/studyskymate/HibernateDemo
Hibernate is ORM (Object Relational Mapping) tool which makes your task easy to interact with the database.
You can control the Database manipulation tasks from Hibernate.
Subscribe to:
Posts (Atom)
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
-
MG-CEIT Course Feedback Form Step1: Like below three pages for the updated course information ...
-
Software Development Tools Presentation From the moment you begin developing software, whether as a freelancer for a startup or wo...
-
कोरोना को जाने प्रश्न (1) :- क्या कोरोना वायरस को ख़त्म किया जा सकता है उत्तर:- नहीं! कोरोना वायर...
-
CDAC Certifications Courses List IT Applications Certificate Course in Business Computing Certificate Course in Global su...
-
Most Important Java Interview Questions Core Java , JDBC , Spring , Hibernate , Servlet , JSP Interview Questions
-
PL-SQL Concepts Presentations. PL SQL is procedural language or structured language. The main feature of SQL (non-procedural)...
-
Car2Go Website Comment if you need code
-
Use Request Dispatcher Servlet in creating simple Login Form We are going to create a Login Form Here. At the end of this section, y...
-
Welcome to the world of Java Programming What is Java? Java is programming language and platform first released in 1995. Jav...
-
Remove/add Fontawesome npm uninstall angular-font-awesome ng add @fortawesome/angular-fontawesome@0.6.0 Add Fontawesome to the proj...