• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Hibernate delete and update

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written hibernate code for insert and delete in database but in insert button , delete function are executing My code has given below
package registration;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Transaction ;

@WebServlet(name = "RegistrationServlet", urlPatterns = {"/rs"})
public class RegistrationServlet extends HttpServlet {



@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// doPost();
// processRequest(request, response);
}


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// processRequest(request, response);


// response.setContentType("text/html;charset=UTF-8");
//PrintWriter out = response.getWriter();
String name= request.getParameter("name");
String email= request.getParameter("email");
String password= request.getParameter("password");

SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
//SessionFactory sessionFactory1=new Configuration().configure().buildSessionFactory();


if(request.getParameter("delete")!=null);

{


Session sessionDelete=sessionFactory.openSession();
deleteData(email,sessionDelete);



}
if (request.getParameter("submit")!=null )
{


Session session=sessionFactory.openSession();
insertData(name,email,password,session);





}
/* try {
/* TODO output your page here. You may use following sample code. */
/* out.println("<html>");
out.println("<head>");
out.println("<title>Servlet RegistrationServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet RegistrationServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
finally
{
out.close();
*/


}
public void insertData(String name, String email, String password, Session sessionInsert)
{

Transaction transactionInsert= sessionInsert.beginTransaction();
Rgdata rgdata=new Rgdata();
rgdata.setName(name);
rgdata.setEmail(email);
rgdata.setPassword(password);
sessionInsert.save(rgdata);

transactionInsert.commit();
sessionInsert.close();

}


public void deleteData(String email,Session sessionDelete)
{

Transaction transactionDelete=sessionDelete.beginTransaction();
Rgdata updateRgdata=(Rgdata) sessionDelete.get(Rgdata.class,email);
//updateRgdata.setName();
//session.update(updateRgdata);
sessionDelete.delete(updateRgdata);
transactionDelete.commit();
sessionDelete.close();

}

}




 
hari vallabh shukla
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hari shukla wrote:I have written hibernate code for insert and delete in database but in insert button , delete function are executing My code has given below



String name= request.getParameter("name");
String email= request.getParameter("email");
String password= request.getParameter("password");

SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();


if(request.getParameter("delete")!=null);

{


Session sessionDelete=sessionFactory.openSession();
deleteData(email,sessionDelete);



}
if (request.getParameter("submit")!=null )
{


Session session=sessionFactory.openSession();
insertData(name,email,password,session);





}

public void insertData(String name, String email, String password, Session sessionInsert)
{

Transaction transactionInsert= sessionInsert.beginTransaction();
Rgdata rgdata=new Rgdata();
rgdata.setName(name);
rgdata.setEmail(email);
rgdata.setPassword(password);
sessionInsert.save(rgdata);

transactionInsert.commit();
sessionInsert.close();

}


public void deleteData(String email,Session sessionDelete)
{

Transaction transactionDelete=sessionDelete.beginTransaction();
Rgdata updateRgdata=(Rgdata) sessionDelete.get(Rgdata.class,email);
sessionDelete.delete(updateRgdata);
transactionDelete.commit();
sessionDelete.close();

}

}

 
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!