Nearby lessons

3 of 19

Hibernate - Your First Application

📌 What You Will Learn
  • Understand Hibernate - Your First Application
  • See working code examples
  • Learn from common mistakes and Q&A

Learn Hibernate - Your First Application step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.

Example To retrieve Record from DB

Employee.java

Example To retrieve Record from DB

Example02
JCode Cell
1 
2package com.durgasoft.hbn.pojo;
3

Example To retrieve Record from DB

Example03
JCode Cell
1 
2public class Employee {
3private int eno;
4private String ename;
5private float esal;
6private String eaddr;
7

Example To retrieve Record from DB

Example04
JCode Cell
1 
2public int getEno() {
3return eno;
4}
5public void setEno(int eno) {
6this.eno = eno;
7}
8public String getEname() {
9return ename;
10}
11public void setEname(String ename) {
12this.ename = ename;
13}
14public float getEsal() {
15return esal;
16}
17public void setEsal(float esal) {
18this.esal = esal;
19}
20public String getEaddr() {
21return eaddr;
22}
23public void setEaddr(String eaddr) {
24this.eaddr = eaddr;
25}
26

Example To retrieve Record from DB

Employee.hbm.xml

Example05
JCode Cell
1 
2}
3

Example To retrieve Record from DB

hibernate.cfg.xml

Example06
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<!DOCTYPE hibernate-mapping PUBLIC
4"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
5"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
6<hibernate-mapping>
7<class name="com.durgasoft.hbn.pojo.Employee" table="emp1">
8<id name="eno"/>
9<property name="ename"/>
10<property name="esal"/>
11<property name="eaddr"/>
12</class>
13</hibernate-mapping>
14

Example To retrieve Record from DB

ClientApp.java

Example07
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<!DOCTYPE hibernate-configuration PUBLIC
4"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
5"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
6<hibernate-configuration>
7<session-factory>
8<property name="connection.driver_Class">oracle.jdbc.OracleDriver</property>
9<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
10<property name="connection.user">system</property>
11<property name="connection.password">durga</property>
12<property name="show_sql">true</property>
13<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
14<mapping resource="com/durgasoft/hbn/mappings/Employee.hbm.xml"/>
15</session-factory>
16</hibernate-configuration>
17

Example To retrieve Record from DB

Example08
JCode Cell
1 
2package com.durgasoft.hbn.test;
3

Example To retrieve Record from DB

Example09
JCode Cell
1 
2import org.hibernate.Session;
3import org.hibernate.SessionFactory;
4import org.hibernate.cfg.Configuration;
5

Example To retrieve Record from DB

Example10
JCode Cell
1 
2import com.durgasoft.hbn.pojo.Employee;
3

Example To retrieve Record from DB

Example11
JCode Cell
1 
2public class ClientApp {
3

Example To retrieve Record from DB

Example12
JCode Cell
1 
2public static void main(String[] args) {
3SessionFactory session_Factory = null;
4Session session = null;
5

Example To retrieve Record from DB

Example13
JCode Cell
1 
2try {
3Configuration cfg = new Configuration();
4cfg.configure("/com/durgasoft/hbn/cfgs/hibernate.cfg.xml");
5session_Factory = cfg.buildSessionFactory();
6session = session_Factory.openSession();
7Employee emp = (Employee)session.get("com.durgasoft.hbn.pojo.Employee",222 );
8if(emp == null) {
9System.out.println("Employee Not Existed");
10}else {
11System.out.println("Employee Details");
12System.out.println("---------------------");
13System.out.println("Employee Number :"+emp.getEno());
14System.out.println("Employee Name :"+emp.getEname());
15System.out.println("Employee Salary :"+emp.getEsal());
16System.out.println("Employee Address :"+emp.getEaddr());
17}
18} catch (Exception e) {
19e.printStackTrace();
20}finally {
21session_Factory.close();
22session.close();
23}
24

Example To retrieve Record from DB

Example14
JCode Cell
1 
2}
3

Example To retrieve Record from DB

Example15
JCode Cell
1 
2}
3
📝 Key Takeaways
  • Key ideas of Hibernate - Your First Application explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3