日期:2014-05-18  浏览次数:20637 次

struts+hibernate的问题
我用hibernate更新三个整型值,第一次提交 hibernate总是很快的完成 并且前台立即更新到修改后的值,这里我使用request.getSession(true).setAttribute来保存更新数据,在页面之间传递数据的。为什么我在点一次修改,提交时hibernate运行超慢,过了一段时间后,出错了 报:org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update。我***.hbm.xml都是手工写的,不存在网上说的什么把catlog=“***”的语句啊。
还有一个问题是:我的另一个页面要修改个人信息,比如邮箱,昵称,我点修改,数据库保存了 那为什么前台还是显示原来的呢,我把hibernate的session Close后,运行,当要跳转到编辑个人信息页面时 报:session is closed。页面无法显示。郁闷 我session已经flush了
我已经在网上找了很多答案了 什么方法都有,都对照过 什么字段长度和数据库长度不符啊 什么的 我没有这个问题啊!
  郁闷啊 学java学到这份上 我都没耐心了 难道java非要在这种框架上折腾时间吗?

------解决方案--------------------
那你在hibernate.cfg.xml配置文件里面连接数据库改成
<property name="hibernate.connection.url">
jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=HR;user=aaa;password=aaa;
SelectMethod=cursor;
</property>
主要是加上SelectMethod=cursor;试试看
------解决方案--------------------
public void update(TPeople peoInfo){
Transaction tx=null;
session=sf.openSession();
try{
tx=session.beginTransaction();
session.update(peoInfo);
tx.commit();
}catch(Exception ex){
if(tx!=null){tx.rollback();}
ex.printStackTrace();
}finally{

session.flush();

}
}

你写程序怎么不关的啊,关掉就没事了
------解决方案--------------------
session.flush();

----》
session.close();
------解决方案--------------------
楼主不要急躁,你现在遇到问题总比你以后工作了再遇到同样的问题好。
------解决方案--------------------
Java code

package com.sunyard.DBO;

import java.util.Iterator;
import java.util.List;

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

import com.sunyard.bean.Person;

/*
 * 具体操作Hibernate的类
 * 增加、删除、修改、按ID查询、模糊查询、查询全部操作
 */
public class PersonDBO {
    
    //在Hibernate中,所有的操作都是在Session中完成
    //此Session不同于JSP中的Session
    private Session session=null;
     
    //在构造方法中实例化Session对象
    public PersonDBO(){
        //找到Hibernate配置
        Configuration config=new Configuration().configure();
        //从配置中取出SessionFactory
        SessionFactory factory=config.buildSessionFactory();
        //从SessionFactory中取出一个Session
        this.session=factory.openSession();
    }
    
    //所有的操作都是通过session进行的
    //插入
    public void insert(Person p){
        //打开事务
        Transaction tran=this.session.beginTransaction();
        //执行插入语句
        this.session.save(p);
        //提交事务
        tran.commit();
        this.session.close();
    }
    
    //更改
    public void update(Person p){
        //打开事务
        Transaction tran=this.session.beginTransaction();
        //执行插入语句
        this.session.update(p);
        //提交事务
        tran.commit();
        this.session.close();
    }
    
    //按ID查询 推荐用HQL
    public Person queryById(String id){
        Person p=null;
        //使用Hibermate查询语句
        String hql="from Person as p where p.id=?";
        //通过Query接口查询
        Query q=this.session.createQuery(hql);
        q.setString(0,id);
        List l=q.list();
        Iterator iter=l.iterator();
        if(iter.hasNext()){
            p=(Person)iter.next();
        }
        this.session.close();
        return p;
    }
    
    //删除1
    //使用此方法删除数据之前,必须先查找到数据对象,性能会打折
    public void delete(Person p){
        //打开事务
        Transaction tran=this.session.beginTransaction();
        //执行插入语句
        this.session.delete(p);