• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Hibernate delete and update  RSS feed

 
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();

}

}

 
If I'd had more time, I would have written a shorter letter. -T.S. Eliot such a short, tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!