File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Application Frameworks and the fly likes Hibernate delete and update Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Hibernate delete and update" Watch "Hibernate delete and update" New topic
Author

Hibernate delete and update

hari vallabh shukla
Greenhorn

Joined: Jan 31, 2013
Posts: 21
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

Joined: Jan 31, 2013
Posts: 21
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();

}

}
 
jQuery in Action, 2nd edition
 
subject: Hibernate delete and update
 
Similar Threads
error message
Problem in Login using Servlets
user authentication and dao
Request dispatcher doesn't redirect to the jsp page
Problem with doGet and doPost methods