Nearby lessons

4 of 19

Hibernate - Retrieving Records from the Database

📌 What You Will Learn
  • Understand Hibernate - Retrieving Records from the Database
  • See working code examples
  • Learn from common mistakes and Q&A

Learn Hibernate - Retrieving Records from the Database step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.

Retrieving Records from the Database

  • } catch (Exception e) {
  • e.printStackTrace();
  • }finally {
  • session_Factory.close();
  • session.close();

HIBERNATE with My SQL Database

If we want to use MySQL Database for Hibernate applications then we have provide the following changes.

In Hibernate Configuration File: : com.mysql.jdbc.Driver Driver Class Name : jdbc:mysql://localhost:3306/db_Name Driver URL : root DB User Name : root DB Password : org.hibernate.dialect.MySQLDialect Dialect

Note: We must add mysql-connector-java-5.0.8-bin.jar file to Hibernate3_Lib.

In Hibernate Applications, it is not suggestible to manage Configuration object and SessionFactory object in Client Application directly, because, these two objects are providing Hibernate Boot strapping. In futer if we want to change boot strapping mechanisms then we have to provide changes in Client Application, it may effect our persistence operations. To avoid this type of problems we have to use a separate Utility class to prepare Configuration and SessionFactory object.

Example02
JCode Cell
1 
2class HibernateUtil{
3private static SessionFactory sessionfactory;
4static{
5try{
6Configuration cfg = new Configuration();
7cfg.configure(---);
8sessionFactory = cfg.buildSessionFactory();
9}catch(Exception e){
10e.printStackTrace();
11}
12}
13public static SessionFactory getSessionFactory(){
14return sessionFactory;
15}
16public static void cleanUp(SessionFactory sf, Session s){
17s.close();
18sf.close();
19}
20}
21

HIBERNATE with My SQL Database — Employee.java

Example:

Example03
JCode Cell
1 
2package com.durgasoft.hbn.pojo;
3 
4public class Employee {
5private int eno;
6private String ename;
7private float esal;
8private String eaddr;
9 
10public int getEno() {
11return eno;
12}
13public void setEno(int eno) {
14this.eno = eno;
15}
16public String getEname() {
17return ename;
18}
19public void setEname(String ename) {
20this.ename = ename;
21}
22public float getEsal() {
23return esal;
24}
25public void setEsal(float esal) {
26this.esal = esal;
27}
28public String getEaddr() {
29return eaddr;
30}
31public void setEaddr(String eaddr) {
32this.eaddr = eaddr;
33}
34}
35

HIBERNATE with My SQL Database — Employee.hbm.xml

Example04
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

HIBERNATE with My SQL Database — hibernate.cfg.xml

Example05
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">com.mysql.jdbc.Driver</property>
9<property name="connection.url">jdbc:mysql://localhost:3306/durgadb</property>
10<property name="connection.user">root</property>
11<property name="connection.password">root</property>
12<property name="show_sql">true</property>
13<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
14<mapping resource="com/durgasoft/hbn/mappings/Employee.hbm.xml"/>
15</session-factory>
16</hibernate-configuration>
17

HIBERNATE with My SQL Database — HibernateUtil.java

Example06
JCode Cell
1 
2package com.durgasoft.hbn.util;
3 
4import org.hibernate.Session;
5import org.hibernate.SessionFactory;
6import org.hibernate.cfg.Configuration;
7 
8public class HibernateUtil {
9private static SessionFactory sessionFactory;
10static {
11try {
12 Configuration cfg = new Configuration();
13 cfg.configure("/com/durgasoft/hbn/cfgs/hibernate.cfg.xml");
14 sessionFactory = cfg.buildSessionFactory();
15} catch (Exception e) {
16 e.printStackTrace();
17}
18}
19 
20public static SessionFactory getSessionFactory() {
21return sessionFactory;
22}
23public static void cleanUp(SessionFactory sessionFactory, Session session) {
24session.close();
25sessionFactory.close();
26}
27}
28

HIBERNATE with My SQL Database — ClientApp.java

Example07
JCode Cell
1 
2package com.durgasoft.hbn.test;
3 
4import org.hibernate.Session;
5import org.hibernate.SessionFactory;
6 
7import com.durgasoft.hbn.pojo.Employee;
8import com.durgasoft.hbn.util.HibernateUtil;
9 
10public class ClientApp {
11 
12public static void main(String[] args) {
13try {
14 SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
15 Session session = sessionFactory.openSession();
16 Employee emp = (Employee)session.get("com.durgasoft.hbn.pojo.Employee", 111);
17 if(emp == null) {
18 System.out.println("Employee Not Existed");
19 }else {
20 System.out.println("Employee Details");
21 System.out.println("-----------------------");
22 System.out.println("Employee Number :"+emp.getEno());
23 System.out.println("Employee Name :"+emp.getEname());
24 System.out.println("Employee Salary :"+emp.getEsal());
25 System.out.println("Employee Address :"+emp.getEaddr());
26 }
27 HibernateUtil.cleanUp(sessionFactory, session);
28} catch (Exception e) {
29 e.printStackTrace();
30}
31 
32}
33 
34}
35

AWT/GUI-Hibernate Integration Application — Student.java

Example08
JCode Cell
1 
2package com.durgasoft.hbn.pojo;
3 
4public class Student {
5private String sid;
6private String sname;
7private String saddr;
8 
9public String getSid() {
10 return sid;
11}
12public void setSid(String sid) {
13this.sid = sid;
14}
15public String getSname() {
16return sname;
17}
18public void setSname(String sname) {
19this.sname = sname;
20}
21public String getSaddr() {
22return saddr;
23}
24public void setSaddr(String saddr) {
25this.saddr = saddr;
26}
27 
28 
29}
30

AWT/GUI-Hibernate Integration Application — Student.hbm.xml

Example09
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.Student" table="student">
8 <id name="sid" column="SID"/>
9 <property name="sname"/>
10 <property name="saddr"/>
11</class>
12</hibernate-mapping>
13

AWT/GUI-Hibernate Integration Application — hibernate.cfg.xml

Example10
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">com.mysql.jdbc.Driver</property>
9 <property name="connection.url">jdbc:mysql://localhost:3306/durgadb</property>
10 <property name="connection.user">root</property>
11<property name="connection.password">root</property>
12<property name="show_sql">true</property>
13<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
14<mapping resource="com/durgasoft/hbn/mappings/Student.hbm.xml"/>
15</session-factory>
16</hibernate-configuration>
17

AWT/GUI-Hibernate Integration Application — HibernateUtil.java

Example11
JCode Cell
1 
2package com.durgasoft.hbn.util;
3 
4import org.hibernate.SessionFactory;
5import org.hibernate.cfg.Configuration;
6 
7public class HibernateUtil {
8private static SessionFactory sessionFactory;
9static {
10 try {
11 Configuration cfg = new Configuration();
12 cfg.configure("/com/durgasoft/hbn/cfgs/hibernate.cfg.xml");
13 sessionFactory = cfg.buildSessionFactory();
14} catch (Exception e) {
15 e.printStackTrace();
16}
17}
18public static SessionFactory getSessionFactory() {
19return sessionFactory;
20}
21}
22

AWT/GUI-Hibernate Integration Application — StudentService.java

Example12
JCode Cell
1 
2package com.durgasoft.service;
3 
4import org.hibernate.Session;
5import org.hibernate.SessionFactory;
6 
7import com.durgasoft.hbn.pojo.Student;
8import com.durgasoft.hbn.util.HibernateUtil;
9 
10public class StudentService {
11Student std = null;
12public Student search(String sid) {
13try {
14 SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
15 Session session = sessionFactory.openSession();
16 std = (Student) session.get("com.durgasoft.hbn.pojo.Student", sid);
17} catch (Exception e) {
18 e.printStackTrace();
19}
20return std;
21}
22}
23

AWT/GUI-Hibernate Integration Application — SearchFrame.java

Example13
JCode Cell
1 
2package com.durgasoft.client;
3 
4import java.awt.Button;
5import java.awt.Color;
6import java.awt.FlowLayout;
7import java.awt.Font;
8import java.awt.Frame;
9import java.awt.Graphics;
10import java.awt.Label;
11import java.awt.TextField;
12import java.awt.event.ActionEvent;
13import java.awt.event.ActionListener;
14import java.awt.event.WindowAdapter;
15import java.awt.event.WindowEvent;
16 
17import com.durgasoft.hbn.pojo.Student;
18import com.durgasoft.service.StudentService;
19 
20public class SearchFrame extends Frame implements ActionListener {
21 
22Label l;
23TextField tf;
24Button b;
25Student std;
26 
27SearchFrame(){
28this.setVisible(true);
29this.setSize(500, 500);
30this.setTitle("Student Search Frame");
31this.setLayout(new FlowLayout());
32this.setBackground(Color.green);
33this.addWindowListener(new WindowAdapter() {
34 public void windowClosing(WindowEvent we) {
35 System.exit(0);
36 }
37});
38 
39l = new Label("Student ID");
40tf = new TextField(20);
41b = new Button("SEARCH");
42b.addActionListener(this);
43 
44Font f = new Font("arial", Font.BOLD, 20);
45l.setFont(f);
46tf.setFont(f);
47b.setFont(f);
48 
49this.add(l);
50this.add(tf);
51this.add(b);
52}
53@Override
54public void actionPerformed(ActionEvent ae) {
55 
56String sid = tf.getText();
57StudentService std_Serv = new StudentService();
58std = std_Serv.search(sid);
59repaint();
60}
61public void paint(Graphics g) {
62Font f = new Font("arial", Font.BOLD, 30);
63this.setForeground(Color.red);
64g.setFont(f);
65 
66if(std == null) {
67 g.drawString("Student Not Existed", 50, 300);
68}else {
69 g.drawString("Student ID :"+std.getSid(), 50, 300);
70 g.drawString("Student Name :"+std.getSname(), 50, 350);
71 g.drawString("Student Address :"+std.getSaddr(), 50, 400);
72}
73}
74}
75

AWT/GUI-Hibernate Integration Application — ClientApp.java

Example14
JCode Cell
1 
2package com.durgasoft.client;
3 
4public class ClientApp {
5 
6public static void main(String[] args) {
7 SearchFrame searchFrame = new SearchFrame();
8 
9}
10}
11

Servlet_Hibernate_Application — loginform.html

Example15
JCode Cell
1 
2<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Insert title here</title>
7</head>
8<body>
9<h2>Durga Software Solutions</h2>
10<h3>User Login Form</h3>
11<form method = "POST" action = "./login">
12<table>
13<tr>
14<td>User Name</td>
15<td><input type="text" name="uname"/></td>
16</tr>
17<tr>
18<td>Password</td>
19<td><input type="password" name="upwd"/></td>
20</tr>
21<tr>
22<td><input type="submit" value="Login"/></td>
23</tr>
24</table>
25</form>
26</body>
27</html>
28

Servlet_Hibernate_Application — success.html

Example16
JCode Cell
1 
2<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Insert title here</title>
7</head>
8<body>
9<h2>Durga Software Solutions</h2>
10<h3>User Login Status Page</h3>
11<font color="red" size="6">
12<b>
13User Login Success
14</b>
15</font>
16<h3>
17<a href="./loginform.html">|User Login Form|</a>
18</h3>
19</body>
20</html>
21

Servlet_Hibernate_Application — failure.html

Example17
JCode Cell
1 
2<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Insert title here</title>
7</head>
8<body>
9<h2>Durga Software Solutions</h2>
10<h3>User Login Status Page</h3>
11<font color="red" size="6">
12<b>
13User Login Failure
14</b>
15</font>
16<h3>
17<a href="./loginform.html">|User Login Form|</a>
18</h3>
19</body>
20</html>
21

Servlet_Hibernate_Application — hibernate.cfg.xml

Example18
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="hibernate.connection.driver_Class">oracle.jdbc.OracleDriver</property>
9 <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
10 <property name="hibernate.connection.username">system</property>
11<property name="hibernate.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/User.hbm.xml"/>
15</session-factory>
16</hibernate-configuration>
17

Servlet_Hibernate_Application — User.hbm.xml

Example19
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.User" table="Reg_Users">
8 <id name="uname" column="UNAME"/>
9 <property name="upwd" column="UPWD"/>
10</class>
11</hibernate-mapping>
12

Servlet_Hibernate_Application — HibernateUtil.java

Example20
JCode Cell
1 
2package com.durgasoft.hbn.util;
3 
4import org.hibernate.SessionFactory;
5import org.hibernate.cfg.Configuration;
6 
7public class HibernateUtil {
8private static SessionFactory sessionFactory;
9static {
10 try {
11 Configuration cfg = new Configuration();
12 cfg.configure("/com/durgasoft/hbn/cfgs/hibernate.cfg.xml");
13 sessionFactory = cfg.buildSessionFactory();
14 System.out.println(sessionFactory);
15} catch (Exception e) {
16 e.printStackTrace();
17}
18}
19public static SessionFactory getSessionFactory() {
20return sessionFactory;
21}
22}
23

Servlet_Hibernate_Application — User.java

Example21
JCode Cell
1 
2package com.durgasoft.hbn.pojo;
3 
4public class User {
5private String uname;
6private String upwd;
7public String getUname() {
8 return uname;
9}
10public void setUname(String uname) {
11this.uname = uname;
12}
13public String getUpwd() {
14return upwd;
15}
16public void setUpwd(String upwd) {
17this.upwd = upwd;
18}
19 
20}
21

Servlet_Hibernate_Application — UserService.java

Example22
JCode Cell
1 
2package com.durgasoft.service;
3 
4import org.hibernate.Session;
5import org.hibernate.SessionFactory;
6 
7import com.durgasoft.hbn.pojo.User;
8import com.durgasoft.hbn.util.HibernateUtil;
9 
10public class UserService {
11String status = "";
12public String checkLogin(String uname, String upwd) {
13try {
14 SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
15 Session session = sessionFactory.openSession();
16 System.out.println(session);
17 User user = (User)session.get(com.durgasoft.hbn.pojo.User.class, uname);
18 if(user == null) {
19 status = "failure";
20 }else {
21 if(user.getUpwd().equals(upwd)) {
22 status = "success";
23 }else {
24 status = "failure";
25 }
26 }
27} catch (Exception e) {
28 e.printStackTrace();
29}
30return status;
31}
32}
33

Servlet_Hibernate_Application — LoginServlet.java

Example23
JCode Cell
1 
2package com.durgasoft.servlets;
3 
4import java.io.IOException;
5 
6import javax.servlet.RequestDispatcher;
7import javax.servlet.ServletException;
8import javax.servlet.http.HttpServlet;
9import javax.servlet.http.HttpServletRequest;
10import javax.servlet.http.HttpServletResponse;
11 
12import com.durgasoft.hbn.util.HibernateUtil;
13import com.durgasoft.service.UserService;
14public class LoginServlet extends HttpServlet {
15public void init() {
16HibernateUtil.getSessionFactory();
17System.out.println("init()");
18}
19private static final long serialVersionUID = 1L;
20protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
21String uname = request.getParameter("uname");
22String upwd = request.getParameter("upwd");
23 
24UserService user_Service = new UserService();
25String status = user_Service.checkLogin(uname, upwd);
26 
27RequestDispatcher requestDispatcher = null;
28if(status.equals("success")) {
29 requestDispatcher = request.getRequestDispatcher("/success.html");
30 requestDispatcher.forward(request, response);
31}else {
32 requestDispatcher = request.getRequestDispatcher("/failure.html");
33 requestDispatcher.forward(request, response);
34}
35}
36}
37

Servlet_Hibernate_Application — web.xml

Example24
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
4<display-name>app07</display-name>
5<welcome-file-list>
6<welcome-file>index.html</welcome-file>
7<welcome-file>index.htm</welcome-file>
8<welcome-file>index.jsp</welcome-file>
9<welcome-file>default.html</welcome-file>
10<welcome-file>default.htm</welcome-file>
11<welcome-file>default.jsp</welcome-file>
12</welcome-file-list>
13<servlet>
14<description></description>
15<display-name>LoginServlet</display-name>
16<servlet-name>LoginServlet</servlet-name>
17<servlet-class>com.durgasoft.servlets.LoginServlet</servlet-class>
18<load-on-startup>1</load-on-startup>
19</servlet>
20<servlet-mapping>
21<servlet-name>LoginServlet</servlet-name>
22<url-pattern>/login</url-pattern>
23</servlet-mapping>
24</web-app>
25

JSP_Hibernate_Application — searchform.html

Example25
JCode Cell
1 
2<!DOCTYPE html>
3<html>
4<head>
5<meta charset="ISO-8859-1">
6<title>Insert title here</title>
7</head>
8<body>
9<h2>Durga Software Solutions</h2>
10<h3>Customer Search Form</h3>
11<form method="POST" action="./search.jsp">
12<table>
13<tr>
14<td>Customer ID</td>
15<td><input type="text" name="cid"/></td>
16</tr>
17<tr>
18<td><input type="submit" value="SEARCH"/></td>
19</tr>
20</table>
21</form>
22</body>
23</html>
24

JSP_Hibernate_Application — notexisted.html

Example26
JCode Cell
1 
2<!DOCTYPE html>
3<html>
4<head>
5<meta charset="ISO-8859-1">
6<title>Insert title here</title>
7</head>
8<body>
9<h2>Durga Software Solutions</h2>
10<h3>Customer Search Status</h3>
11<font color="red" size="6">
12<b>
13Customer Not Existed
14</b>
15</font>
16<h3>
17<a href="./searchform.html">|Customer Search Form|</a>
18</h3>
19</body>
20</html>
21

JSP_Hibernate_Application — display.jsp

Example27
JCode Cell
1 
2<%@page import="com.durgasoft.hbn.pojo.Customer"%>
3<%!
4Customer customer = null;
5%>
6<%
7customer = (Customer)request.getAttribute("customer");
8%>
9<html>
10<body>
11<h2>Durga Software Solutions</h2>
12<h3>Customer Details</h3>
13<table border="1">
14<tr>
15<td>Customer ID</td>
16<td><%=customer.getCid() %> </td>
17</tr>
18<tr>
19<td>Customer Name</td>
20<td><%=customer.getCname() %> </td>
21</tr>
22<tr>
23<td>Customer Address</td>
24<td><%=customer.getCaddr() %> </td>
25</tr>
26<tr>
27<td>Customer Email</td>
28<td><%=customer.getCemail() %> </td>
29</tr>
30<tr>
31<td>Customer Mobile</td>
32<td><%=customer.getCmobile() %> </td>
33</tr>
34</table>
35<h3>
36<a href="./searchform.html">|Customer Search Form|</a>
37</h3>
38</body>
39</html>
40

JSP_Hibernate_Application — search.jsp

Example28
JCode Cell
1 
2<%@page import="com.durgasoft.hbn.pojo.Customer"%>
3<%@page import="org.hibernate.Session"%>
4<%@page import="org.hibernate.SessionFactory"%>
5<%@page import="org.hibernate.cfg.Configuration"%>
6<%!
7Configuration cfg = null;
8SessionFactory sessionFactory = null;
9Session ses = null;
10Customer customer = null;
11%>
12<%
13try{
14String cid = request.getParameter("cid");
15cfg = new Configuration();
16cfg.configure("/com/durgasoft/hbn/cfgs/hibernate.cfg.xml");
17sessionFactory = cfg.buildSessionFactory();
18ses = sessionFactory.openSession();
19customer = (Customer)ses.get(Customer.class, cid );
20if(customer == null){
21%>
22<jsp:forward page="notexisted.html"/>
23<%
24}else{
25request.setAttribute("customer", customer);
26%>
27<jsp:forward page="display.jsp"/>
28<%
29}
30}catch(Exception e){
31e.printStackTrace();
32}
33%>
34

JSP_Hibernate_Application — Customer.java

Example29
JCode Cell
1 
2package com.durgasoft.hbn.pojo;
3 
4public class Customer {
5private String cid;
6private String cname;
7private String caddr;
8private String cemail;
9private String cmobile;
10 
11public String getCid() {
12return cid;
13}
14public void setCid(String cid) {
15this.cid = cid;
16}
17public String getCname() {
18return cname;
19}
20public void setCname(String cname) {
21this.cname = cname;
22}
23public String getCaddr() {
24return caddr;
25}
26public void setCaddr(String caddr) {
27this.caddr = caddr;
28}
29public String getCemail() {
30return cemail;
31}
32public void setCemail(String cemail) {
33this.cemail = cemail;
34}
35public String getCmobile() {
36return cmobile;
37}
38public void setCmobile(String cmobile) {
39this.cmobile = cmobile;
40}
41 
42 
43}
44

JSP_Hibernate_Application — Customer.hbm.xml

Example30
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.Customer" table="customer">
8 <id name="cid" column="CID"/>
9 <property name="cname" column="CNAME"/>
10 <property name="caddr" column="CADDR"/>
11<property name="cemail" column="CEMAIL"/>
12<property name="cmobile" column="CMOBILE"/>
13</class>
14</hibernate-mapping>
15

JSP_Hibernate_Application — hibernate.cfg.xml

Example31
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">com.mysql.jdbc.Driver</property>
9<property name="connection.url">jdbc:mysql://localhost:3306/durgadb</property>
10<property name="connection.username">root</property>
11<property name="connection.password">root</property>
12<property name="show_sql">true</property>
13<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
14<mapping resource="com/durgasoft/hbn/mappings/Customer.hbm.xml"/>
15</session-factory>
16</hibernate-configuration>
17

JSP_Hibernate_Application — web.xml

Example32
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
4<display-name>app08</display-name>
5<welcome-file-list>
6<welcome-file>index.html</welcome-file>
7<welcome-file>index.htm</welcome-file>
8<welcome-file>index.jsp</welcome-file>
9<welcome-file>default.html</welcome-file>
10<welcome-file>default.htm</welcome-file>
11<welcome-file>default.jsp</welcome-file>
12</welcome-file-list>
13</web-app>
14
📝 Key Takeaways
  • Key ideas of Hibernate - Retrieving Records from the Database explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end