【发布时间】:2016-12-03 17:56:31
【问题描述】:
在我的 Web 应用程序中,我试图删除数据库中的一行,但它似乎不起作用,我试图找出原因但我做不到。这是我的 dao 类:
public class userDao {
private SessionFactory factory, session;
public userDao(SessionFactory factory) {
this.factory = factory;
}
public void delete(long id){
Session session1 = factory.openSession();
Transaction tx = null;
List<User> u = null;
try {
tx = session1.beginTransaction();
Query query = session1.createQuery("SELECT * FROM user_table WHERE id = :id");
query.setParameter("id", id);
System.out.println("aaaaa" + id);
u = query.list();
session1.delete(u.get(0));
tx.commit();
} catch (HibernateException e) {
if (tx != null) {
tx.rollback();
}
} finally {
session1.close();
}
}
}
在我的 servlet 中我有:
public class DeleteServlet extends HttpServlet{
userDao u = new userDao(new Configuration().configure().buildSessionFactory());
User a = new User();
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
System.out.println("asa");
response.sendRedirect("/A1.2/home.jsp");
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
String s = request.getParameter("id");
long id = Long.parseLong(s);
u.delete(id);
response.sendRedirect("/A1.2/home.jsp");
}
}
我认为问题出在 dao 中,因为我试图从数据库中获取用户,但出现空指针异常。
我收到的警告: log4j:WARN 找不到记录器 (org.hibernate.type.BasicTypeRegistry) 的附加程序。 log4j:WARN 请正确初始化 log4j 系统。 log4j:WARN 请参阅http://logging.apache.org/log4j/1.2/faq.html#noconfig 了解更多信息。
【问题讨论】:
-
是否有错误消息或完整的堆栈跟踪?如果有,请将其包含在问题中。
-
没有错误信息,但有一些警告。