This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
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 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 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();

}

}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate delete and update
 
Similar Threads
error message
Problem with doGet and doPost methods
Request dispatcher doesn't redirect to the jsp page
Problem in Login using Servlets
user authentication and dao