首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

Hibernate的有关问题,配置好了,但是不能真正实现删除操作

2012-01-31 
Hibernate的问题,配置好了,但是不能真正实现删除操作!HibernateUtil的代码:packagecom.studorm.daoimport

Hibernate的问题,配置好了,但是不能真正实现删除操作!
HibernateUtil的代码:
package   com.studorm.dao;

import   org.hibernate.HibernateException;
import   org.hibernate.Session;
import   org.hibernate.SessionFactory;
import   org.hibernate.Transaction;
import   org.hibernate.cfg.Configuration;

public   class   HibernateUtil   {
private   static   final   SessionFactory   sessionFactory;

static   {
try   {
//   Create   the   SessionFactory
sessionFactory   =   new   Configuration().configure()
.buildSessionFactory();
}   catch   (Throwable   ex)   {
ex.printStackTrace();
System.out.println( "Initial   SessionFactory   creation   failed. ");
throw   new   ExceptionInInitializerError(ex);
}
}

public   static   final   ThreadLocal   tLocalsess   =   new   ThreadLocal();

public   static   final   ThreadLocal   tLocaltx   =   new   ThreadLocal();

/*
  *   getting   the   thread-safe   session   for   using
  */
public   static   Session   currentSession()   {
Session   session   =   (Session)   tLocalsess.get();

//   open   a   new   one,   if   none   can   be   found.
try   {
if   (session   ==   null   ||   !session.isOpen())   {
session   =   openSession();
tLocalsess.set(session);
}
}   catch   (HibernateException   e)   {
//   throw   new   HibernateException(e);
e.printStackTrace();
}
return   session;
}

/*
  *   closing   the   thread-safe   session
  */
public   static   void   closeSession()   {

Session   session   =   (Session)   tLocalsess.get();
try   {
if   (session!=   null   &&   session.isOpen())   {
session.close();
}
tLocalsess.set(null);
}   catch   (HibernateException   e)   {
//   throw   new   InfrastructureException(e);
}
}

/*
  *   begin   the   transaction
  */
public   static   void   beginTransaction()   {
System.out.println( "begin   tx ");
Transaction   tx   =   (Transaction)   tLocaltx.get();
try   {
if   (tx   ==   null)   {
tx   =   currentSession().beginTransaction();
tLocaltx.set(tx);
System.out.println( "Tx   is   End! ");
}
}   catch   (HibernateException   e)   {
//   throw   new   InfrastructureException(e);
}
}

/*
  *   close   the   transaction
  */
public   static   void   commitTransaction()   {
Transaction   tx   =   (Transaction)   tLocaltx.get();
try   {
if   (tx   !=   null   &&   !tx.wasCommitted()   &&   !tx.wasRolledBack())
tx.commit();
System.out.println( "commit   tx ");
tLocaltx.set(null);
}   catch   (HibernateException   e)   {


//   throw   new   InfrastructureException(e);
}
}

/*
  *   for   rollbacking
  */
public   static   void   rollbackTransaction()   {
Transaction   tx   =   (Transaction)   tLocaltx.get();
try   {
tLocaltx.set(null);
if   (tx   !=   null   &&   !tx.wasCommitted()   &&   !tx.wasRolledBack())   {
tx.rollback();
}
}   catch   (HibernateException   e)   {
//   throw   new   InfrastructureException(e);
}
}

private   static   Session   openSession()   throws   HibernateException   {
return   getSessionFactory().openSession();
}

private   static   SessionFactory   getSessionFactory()   throws   HibernateException   {
return   sessionFactory;//   SingletonSessionFactory.getInstance();
}
}

实现删除操作的Action代码如下:
/*
  *   Generated   by   MyEclipse   Struts
  *   Template   path:   templates/java/JavaClass.vtl
  */
package   com.studorm.struts.action;

import   javax.servlet.http.HttpServletRequest;
import   javax.servlet.http.HttpServletResponse;
import   org.apache.struts.action.Action;
import   org.apache.struts.action.ActionForm;
import   org.apache.struts.action.ActionForward;
import   org.apache.struts.action.ActionMapping;
import   org.hibernate.Session;

import   com.studorm.dao.Students;
import   com.studorm.dao.HibernateUtil;

/**  
  *   MyEclipse   Struts
  *   Creation   date:   04-17-2007
  *  
  *   XDoclet   definition:
  *   @struts.action   validate= "true "
  *   @struts.action-forward   name= "deletestudentsuccess "   path= "/stumain.jsp "
  */
public   class   DeletestudentAction   extends   Action   {
/*
  *   Generated   Methods
  */

/**  
  *   Method   execute
  *   @param   mapping
  *   @param   form
  *   @param   request
  *   @param   response
  *   @return   ActionForward
  */
public   ActionForward   execute(ActionMapping   mapping,   ActionForm   form,
HttpServletRequest   request,   HttpServletResponse   response){
             
//   TODO   Auto-generated   method   stub

        int   id   =new   Integer(request.getParameter( "student_id "));

Session   s   =   HibernateUtil.currentSession();
HibernateUtil.beginTransaction();
        Students   stu   =   (Students)   s.load(Students.class,id);
//System.out.println(stu.getId());
s.delete(stu);
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();

        return   (mapping.findForward( "deletestudentsuccess "));

}
}
我感觉还是HibernateUtil中的问题,请那位高手给看看!

[解决办法]
public class LoginAction extends Action {



// --------------------- Instance Variables

// --------------------- Methods

/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
Session session=HibernateSessionFactory.currentSession();
Transaction tx=session.beginTransaction();
String hql= "delete Admin where id=106 ";
Query query=session.createQuery(hql);
query.executeUpdate();
tx.commit();
HibernateSessionFactory.closeSession();

}
}
beginTransaction() 不需要自己写,用引入就可以
[解决办法]
能不能详细一点呢!谢谢了!
[解决办法]
我正准备学Hibernate关注中
[解决办法]
看看这个, 可以运行
///////////////////////////////////////////////////////////////////
public static int executeUpdate(String sql, Object [] params)
throws HibernateException 
{
Session session = currentSession(); 
Transaction tx = session.beginTransaction();
try {
Query query = session.createQuery(sql);
if (params != null)
{
for(int i=0; i<params.length; i++)
query.setParameter(i, params[i]); 
}
int result = query.executeUpdate();
tx.commit();
return result ;

}catch (HibernateException e) {
tx.rollback();
throw e ;
}finally {
HibernateUtil.closeSession();
}
}
[解决办法]
删除完了后 session.flush()没?

热点排行