<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div style="color: black;">In this lesson we will show how to delete rows from the underlying database using the hibernate. Lets first write a java class to delete a row from the database.</div>
<div style="color: black;"><b>Create a JAVA Class for this: ;</b></p>
<div align='center' id="ads-id"></div>
<p>here<b> </b>we give the code of java class <b>HibernateTestDemo.java </b>which we will delete a row from the USER_TABLE table using the query ; ; ; ; ; &#8220;delete ;from USER_TABLE where USER_ID = ;2&#8221;</p>
<p><b>Following is USER_TABLE:</b> ;</p>
<div class="separator" style="clear: both; text-align: center;"></div>
<div class="separator" style="clear: both; text-align: center;"></div>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2017/04/table5.jpg" /></div>
</div>
<div style="background-color: #f2f9fc; border:1px solid #c9e6f2;border-radius:3px;padding:16px;line-height:1.45;"><b>Popular Tutorials</b></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/2012/06/spring-30-baby-step-to-learn.html"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-web-mvc-framework-chapter-38.html"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/2016/06/introduction-to-spring-boot-a-spring-boot-complete-guide.html"><em><strong>Spring Boot Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/02/spring-security-take-baby-step-to-secure.html"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint.html"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/using-spring-jdbc-framework-chapter-32.html"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/spring-hateoas-hypermedia-driven-restful-web-service.html"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/microservices-with-spring-boot.html"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/06/jax-rs-web-service-tutorial.html"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/01/core-java-baby-step-to-be-best-java-ian.html"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/03/hibernate-3-on-baby-steps.html"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-batch-process-with-example.html"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<div style="background-color: #ff99cc; border: medium solid; color: blue;">package com.sdnext.hibernate.tutorial;</p>
<p>import org.hibernate.Query;<br />
import org.hibernate.Session;<br />
import org.hibernate.SessionFactory;<br />
import org.hibernate.cfg.AnnotationConfiguration;</p>
<p>import com.sdnext.hibernate.tutorial.dto.UserDetails;</p>
<p>public class HibernateTestDemo {<br />
 ; ;  ;/**<br />
 ; ;  ; * @param args<br />
 ; ;  ; */<br />
 ; ;  ;public static void main(String[] args) <br />
 ; ;  ;{<br />
 ; ;  ; ; ; ; SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();<br />
 ; ;  ; ; ;  ;Session session = sessionFactory.openSession();<br />
 ; ;  ; ; ;  ;session.beginTransaction();<br />
 ; ;  ; ; ;  ;String sql = &#8220;DELETE FROM UserDetails WHERE USER_ID = 2&#8221;;<br />
 ; ;  ; ; ;  ;Query query = session.createQuery(sql);<br />
 ; ;  ; ; ;  ; int row = query.executeUpdate();<br />
 ; ;  ; ; ;  ; if (row == 0)<br />
 ; ;  ; ; ;  ; ; ;  ; System.out.println(&#8220;Doesnt deleted any row!&#8221;);<br />
 ; ;  ; ; ;  ; else<br />
 ; ;  ; ; ;  ; ; ;  ; System.out.println(&#8220;Deleted Row: &#8221; + row);<br />
 ;  ;  ;  ; session.getTransaction().commit(); <br />
 ; ;  ; ; ;  ;session.close();<br />
 ; ;  ;}<br />
}</p>
<p><b>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: delete from USER_TABLE where USER_ID=2<br />
Deleted Row: 1</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2017/04/output5.jpg" /></div>
<p>
Now we look the following table structure&#8212;</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2017/04/table6.jpg" /></div>
<p>
<b>Now in the <a href="https://dineshonjava.com/p/value-types-and-embedding-objects.html">Next Chapter </a>we will see how to work on </b><b><a href="https://dineshonjava.com/p/value-types-and-embedding-objects.html">Value Types and Embedding Objects.</a></b><b></b></p>
<p> ; ; ; ; ;  ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;<b> ; ;<;<;<a href="https://dineshonjava.com/p/hibernate-update-query.html">Previous Chapter 11</a><;<;  ; ; >;>;N<a href="https://dineshonjava.com/p/value-types-and-embedding-objects.html">ext Chapter13</a></b><br />
<b><br />
</b> <b><br />
</b></div>
</div>

View Comments
Can we insert or update entity using query.executeUpdate();