<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>In this section, you will learn how to develop a CRUD application using hibernate annotation.<br />
<b>Follows the following steps for developing the CRUD application in hibernate annotation.</b><br />
<b>Step 1: Create Domain Entity Class</b></p>
<div id="ads-id" align="center"></div>
<p><b>Student.java</b></p>
</div>
<pre class="highlight">package com.sdnext.hibernate.tutorial.dto; 
 
import java.io.Serializable; 
 
import javax.persistence.Column; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
 
@Entity 
@Table(name="STUDENT") 
public class Student implements Serializable 
{ 
 
 /** 
 * serialVersionUID 
 */ 
 private static final long serialVersionUID = 8633415090390966715L; 
 @Id 
 @Column(name="ID") 
 @GeneratedValue(strategy=GenerationType.AUTO) 
 private int id; 
 @Column(name="STUDENT_NAME") 
 private String studentName; 
 @Column(name="ROLL_NUMBER") 
 private int rollNumber; 
 @Column(name="COURSE") 
 private String course; 
 public int getId() { 
 return id; 
 } 
 public void setId(int id) { 
 this.id = id; 
 } 
 public String getStudentName() { 
 return studentName; 
 } 
 public void setStudentName(String studentName) { 
 this.studentName = studentName; 
 } 
 public int getRollNumber() { 
 return rollNumber; 
 } 
 public void setRollNumber(int rollNumber) { 
 this.rollNumber = rollNumber; 
 } 
 public String getCourse() { 
 return course; 
 } 
 public void setCourse(String course) { 
 this.course = course; 
 } 
} 
</pre>
<p> ;</p>
</div>
<p><b>Step 2: Create Hibernate Configuration file </b><br />
<b>hibernate.cfg.xml</b><br />
file contains<br />
(a.) <b>database connection setting</b> (database driver (com.mysql.jdbc.Driver), url (jdbc:mysql://localhost:3306/hibernateDB2), username (root) and password (root)),<br />
(b.) <b>SQL dialect</b> (dialect &#8211; org.hibernate.dialect.MySQLDialect),<br />
(c.) <b>enable hibernate&#8217;s automatic session context management </b>(current_session_context_class &#8211; thread),<br />
(d.) <b>disable the second level cache </b>(cache.provider_class &#8211; org.hibernate.cache.NoCacheProvider),<br />
(e.) <b>print all executed SQL to stdout</b> (show_sql &#8211; true) and<br />
(f.) <b>drop and re-create the database schema on startup</b> (hbm2ddl.auto &#8211; none).</p>
</div>
<pre class="highlight"><;hibernate-configuration>; 
 <;session-factory>; 
 <;!-- Database connection settings -->; 
 <;property name="connection.driver_class">;com.mysql.jdbc.Driver<;/property>; 
 <;property name="connection.url">;jdbc:mysql://localhost:3306/hibernateDB2<;/property>; 
 <;property name="connection.username">;root<;/property>; 
 <;property name="connection.password">;root<;/property>; 
 
 <;!-- JDBC connection pool (use the built-in) -->; 
 <;property name="connection.pool_size">;1<;/property>; 
 
 <;!-- SQL dialect -->; 
 <;property name="dialect">;org.hibernate.dialect.MySQLDialect<;/property>; 
 
 <;!-- Enable Hibernate's automatic session context management -->; 
 <;property name="current_session_context_class">;thread<;/property>; 
 
 <;!-- Disable the second-level cache -->; 
 <;property name="cache.provider_class">;org.hibernate.cache.NoCacheProvider<;/property>; 
 
 <;!-- Echo all executed SQL to stdout -->; 
 <;property name="show_sql">;true<;/property>; 
 
 <;!-- Drop and re-create the database schema on startup -->; 
 <;property name="hbm2ddl.auto">;update<;/property>; 
 
 <;mapping class="com.sdnext.hibernate.tutorial.dto.Student">; 
 
 <;/mapping>;<;/session-factory>; 
 <;/hibernate-configuration>; 
</pre>
</div>
</div>
<p><b>Step 3: Create Hibernate Utility Class</b><br />
<b>HibernateUtil.java</b></p>
<pre class="highlight">package com.sdnext.hibernate.tutorial.utility; 
 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.AnnotationConfiguration; 
 
public class HibernateUtil 
{ 
 private static final SessionFactory sessionFactory; 
 static 
 { 
 try 
 { 
 sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); 
 } 
 catch(Throwable th){ 
 System.err.println("Enitial SessionFactory creation failed"+th); 
 throw new ExceptionInInitializerError(th); 
 } 
 } 
 public static SessionFactory getSessionFactory(){ 
 return sessionFactory; 
 } 
} 
</pre>
<p><b>Step 4: Create Student on the database.</b><br />
<b>CreateStudent.java</b></p>
<pre class="highlight">package com.sdnext.hibernate.tutorial; 
 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
 
import com.sdnext.hibernate.tutorial.dto.Student; 
import com.sdnext.hibernate.tutorial.utility.HibernateUtil; 
 
public class CreateStudent { 
 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) 
 { 
 //Create student entity object 
 Student student = new Student(); 
 student.setStudentName("Dinesh Rajput"); 
 student.setRollNumber(01); 
 student.setCourse("MCA"); 
 
 //Create session factory object 
 SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); 
 //getting session object from session factory 
 Session session = sessionFactory.openSession(); 
 //getting transaction object from session object 
 session.beginTransaction(); 
 
 session.save(student); 
 System.out.println("Inserted Successfully"); 
 session.getTransaction().commit(); 
 session.close(); 
 sessionFactory.close(); 
 } 
}</pre>
<div style="background-color: #ff99cc; border-width: thin; border: solid;"><b style="color: blue;">OUTPUT: </b><br />
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).<br />
log4j:WARN Please initialize the log4j system properly.<br />
Hibernate: insert into STUDENT (COURSE, ROLL_NUMBER, STUDENT_NAME) values (?, ?, ?)<br />
<b>Inserted Successfully </b></div>
</div>
<p> ;</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/04/create.jpg" border="0" /></div>
<p>Now the following code the reading the student data from database.<br />
<b>Step 5: Reading the Student data from the database table STUDENT</b><br />
<b>ReadStudent.java</b></p>
<pre class="highlight">package com.sdnext.hibernate.tutorial; 
 
import java.util.List; 
 
import org.hibernate.Query; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
 
import com.sdnext.hibernate.tutorial.dto.Student; 
import com.sdnext.hibernate.tutorial.utility.HibernateUtil; 
 
public class ReadStudent { 
 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) 
 { 
 //Create session factory object 
 SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); 
 //getting session object from session factory 
 Session session = sessionFactory.openSession(); 
 //getting transaction object from session object 
 session.beginTransaction(); 
 Query query = session.createQuery("from Student"); 
 List students = query.list(); 
 for(Student student : students) 
 { 
System.out.println("Roll Number: "+student.getRollNumber()+", Student Name: "+student.getStudentName()+", Course: "+student.getCourse()); 
 } 
 session.getTransaction().commit(); 
 sessionFactory.close(); 
 } 
} 
</pre>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<div style="color: blue;"><b>Output:</b></div>
<p>log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).<br />
log4j:WARN Please initialize the log4j system properly.<br />
Hibernate: select student0_.ID as ID0_, student0_.COURSE as COURSE0_, student0_.ROLL_NUMBER as ROLL3_0_, student0_.STUDENT_NAME as STUDENT4_0_ from STUDENT student0_<br />
Roll Number: 1, Student Name: Dinesh Rajput, Course: MCA<br />
Roll Number: 2, Student Name: Anamika Rajput, Course: PGDCP<br />
Roll Number: 3, Student Name: Adesh Rajput, Course: MA<br />
Roll Number: 4, Student Name: Vinesh Rajput, Course: BA</p>
</div>
</div>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/04/read.jpg" width="640" height="83" border="0" /></div>
<p> ;</p>
</div>
<p>The following code is for updating the data into the database table &#8220;STUDENT&#8221;.<br />
<b>Step 6: Update the Student Record in the Database.</b><br />
<b>UpdateStudent.java</b></p>
<pre class="highlight">package com.sdnext.hibernate.tutorial; 
 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
 
import com.sdnext.hibernate.tutorial.dto.Student; 
import com.sdnext.hibernate.tutorial.utility.HibernateUtil; 
 
public class UpdateStudent { 
 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) 
 { 
 //Create session factory object 
 SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); 
 //getting session object from session factory 
 Session session = sessionFactory.openSession(); 
 //getting transaction object from session object 
 session.beginTransaction(); 
 
 Student student = (Student)session.get(Student.class, 2); 
 student.setStudentName("Sweety Rajput"); 
 System.out.println("Updated Successfully"); 
 session.getTransaction().commit(); 
 sessionFactory.close(); 
 } 
}</pre>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<div style="color: blue;"><b>Output:</b></div>
<p> ;</p>
<div class="MsoNormal" style="mso-layout-grid-align: none; mso-pagination: none; text-autospace: none;">log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).<br />
log4j:WARN Please initialize the log4j system properly.<br />
Hibernate: select student0_.ID as ID0_0_, student0_.COURSE as COURSE0_0_, student0_.ROLL_NUMBER as ROLL3_0_0_, student0_.STUDENT_NAME as STUDENT4_0_0_ from STUDENT student0_ where student0_.ID=?<br />
Hibernate: update STUDENT set COURSE=?, ROLL_NUMBER=?, STUDENT_NAME=? where ID=?<br />
<b>Updated Successfully</b></div>
</div>
</div>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/04/updated.jpg" border="0" /></div>
<p><b>Step 7: Delete the student data from the database.</b><br />
<b>DeleteStudent.java</b></p>
<pre class="highlight">package com.sdnext.hibernate.tutorial; 
 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
 
import com.sdnext.hibernate.tutorial.dto.Student; 
import com.sdnext.hibernate.tutorial.utility.HibernateUtil; 
 
public class DeleteStudent { 
 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) 
 { 
 //Create session factory object 
 SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); 
 //getting session object from session factory 
 Session session = sessionFactory.openSession(); 
 //getting transaction object from session object 
 session.beginTransaction(); 
 Student student = (Student)session.load(Student.class, 4); 
 session.delete(student); 
 System.out.println("Deleted Successfully"); 
 session.getTransaction().commit(); 
 sessionFactory.close(); 
 } 
} 
</pre>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<div style="color: blue;"><b>Output:</b></div>
<p>log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).<br />
log4j:WARN Please initialize the log4j system properly.<br />
Hibernate: select student0_.ID as ID0_0_, student0_.COURSE as COURSE0_0_, student0_.ROLL_NUMBER as ROLL3_0_0_, student0_.STUDENT_NAME as STUDENT4_0_0_ from STUDENT student0_ where student0_.ID=?<br />
<b>Deleted Successfully</b><br />
Hibernate: delete from STUDENT where ID=?</p>
</div>
</div>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/04/deleted.jpg" border="0" /></div>
</div>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/04/crud.jpg" border="0" /></div>
<p> ;</p>
<h3 class="post-title entry-title" style="background-color: #f6eeee; color: #9900ff; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 30px; font-weight: normal; margin: 0.75em 0px 0px; position: relative; text-align: center;"><a href="https://dineshonjava.com/2013/07/how-to-solve-querysyntaxexception-table.html#.UdVkAReps2h" target="_blank" rel="noopener">How to solve QuerySyntaxException (table is not mapped) in hibetnate?</a></h3>
<p><b>In the <a href="https://dineshonjava.com/p/transient-persistent-and-detached.html">Next Chapter</a> we will discuss about the <a href="https://dineshonjava.com/p/transient-persistent-and-detached.html">Object states in the Hibernate</a>.</b><br />
<b><br />
</b> <b> <;<;<a style="font-family: Times, 'Times New Roman', serif;" href="https://dineshonjava.com/p/implementing-inheritance-in-hibernate.html">Previous Chapter 22</a><;<; >;>;N<a href="https://dineshonjava.com/p/transient-persistent-and-detached.html">ext Chapter 24</a>>;>;</b><br />
<b><br />
</b> <b><br />
</b> <b><br />
</b> <b><br />
</b></p>
</div>