I am a freshman of hibernate. Today
I configured the tutorial hibernate application following the hibernate
reference(3.2). By the way, the refenrence is rather excellent.
Everything went well until I came to the first Servlet Page. Error
printed as followed:
hibernate Transaction not successfully started
I then checked the codes and configurations. The former program ran well on console, but when it came to Servlet it failed. Refering to some articles found, I refactoried the code. The diffrence is that I stated a Transaction variable tx. It then works. The file EventManagerServlet.java is as below:
package com.CarlElit.HelloWorld.events;
import java.util.Date;
import com.CarlElit.HelloWorld.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
public class EventManagerServlet extends HttpServlet {
static final private String CONTENT_TYPE = "text/html,charset=utf-8";
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
Transaction tx = null;
try
{
PrintWriter out = response.getWriter();
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
response.setContentType(CONTENT_TYPE);
out.println("<html><head><title>Event Manager</title></head><body>");
if ("store".equals(request.getParameter("action")))
{
String eventTitle = request.getParameter("eventTitle");
String eventDate = request.getParameter("eventDate");
if ("".equals(eventTitle) || "".equals(eventDate))
{
out.println("<b><i>Please enter event title and date.</i></b>");
}else
{
createAndStoreEvent(eventTitle, dateFormatter.parse(eventDate));
out.println("<b><i>Added event.</i></b>");
}
}
printEventForm(out);
listEvents(out, dateFormatter);
out.println("</body></html>");
out.flush();
out.close();
tx.commit();
}
catch (Exception ex)
{
if (tx != null)
{
tx.rollback();
}
throw new ServletException(ex);
}
}
private void printEventForm(PrintWriter out) {
out.println("<h2>Add new event:</h2>");
out.println("<form>");
out.println("Title: <input name='eventTitle' length='50'/><br/>");
out.println("Date (e.g. 2008-11-18): <input name='eventDate' length='10'/><br/>");
out.println("<input type='submit' name='action' value='store'/>");
out.println("</form>");
}
private void listEvents(PrintWriter out, SimpleDateFormat dateFormatter) {
List result = HibernateUtil.getSessionFactory().getCurrentSession().createCriteria(Event.class).list();
if (result.size() > 0)
{
out.println("<h2>Events in database:</h2>");
out.println("<table border='1'>");
out.println("<tr>");
out.println("<th>Event Title</th>");
out.println("<th>Event Date</th>");
out.println("</tr>");
for (Iterator it = result.iterator(); it.hasNext(); )
{
Event event = (Event) it.next();
out.println("<tr>");
out.println("<td>" + event.getTitle() + "</td>");
out.println("<td>" + dateFormatter.format(event.getDate()) + "</td>");
out.println("</tr>");
}
out.println("</table>");
}
}
protected void createAndStoreEvent(String title, Date theDate) {
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
HibernateUtil.getSessionFactory().getCurrentSession().save(theEvent);
}
}
For all code please email to : caishaojiang@gmail.com
posted @
2008-03-05 18:13 Carl 阅读(3530) |
评论 (1) |
编辑 收藏