开发者社区> 问答> 正文

关于 Hibernate 中 update 不执行,不报错 求解!

最近调试的时候,发现 之前写好的 项目出了问题。

就是在执行 update、delete 的时候, 不执行,但是也不报错。

配置输出 SQL语句的时候,看不到 SQL语句。数据库中也没有任何变化。

爬文了两天没有结果, 但是发现 如果使用 原生 SQL语句,就可以 更新、删除。如

Query query=this.getSession().createQuery("update from "+c.getName()+" set keyword='123456' where id = '9'");
query.executeUpdate();

再贴上 BaseDao 的实现类

package com.soryin.dao.impl;
 
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
 
import com.soryin.dao.BaseDao;
 
/**
 * 数据库基本操作实现
 * @author Hello_海生
 * @date 2014年3月12日
 * @param <T>
 */
@SuppressWarnings("unchecked")
public class BaseDaoImpl<T> implements BaseDao<T> {
    //声明Session工厂
    @Resource
    private SessionFactory sessionFactory;
    protected Session getSession() {    //获取session
        return this.sessionFactory.openSession();
    }
     
    protected Class<T> c; //对象类
    public BaseDaoImpl() {
        ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
        c = (Class<T>) type.getActualTypeArguments()[0];
        System.out.println("初始化"+this.c.getName()+"DAO");
    }
     
     
     
    @Override   //持久化对象
    public Serializable save(T entity) {
        Serializable uid=0;
        Transaction tx=this.getSession().beginTransaction();
        try{
            uid=this.getSession().save(entity);
            tx.commit();
            System.out.println(this.c.getName()+"DAO"+"执行save 成功!");
        }catch(Exception e){
            System.out.println(this.c.getName()+"DAO"+"执行save 失败!!!!!!!!!!!!!");
            tx.rollback();
        }
        return uid;
    }
 
    @Override   //删除对象
    public boolean delete(Serializable id) {
        boolean rs= false;
        Transaction tx=this.getSession().beginTransaction();
        try{
            Query query=this.getSession().createQuery("delete from "+c.getName()+" where id = '"+id+"'");
            query.executeUpdate();
            tx.commit();
            rs=true;
            System.out.println(this.c.getName()+"DAO"+"执行 delete("+id+") 成功!");
        }catch(Exception e){
            System.out.println(this.c.getName()+"DAO"+"执行 delete("+id+") 失败!!!!!!!!!!!!!");
            tx.rollback();
        }
        return rs;
    }
 
    @Override   //根据ID 查询对象
    public T findById(Serializable id) {
        Object t = this.getSession().get(this.c, id);
        System.out.println(this.c.getName()+"DAO"+"执行 findById("+id+") 成功!");
        return (T) t;
    }
 
    @Override   //根据某项参数查询 列表
    public List<T> findEntityListByParams(String s,Object params) {
        Query query=this.getSession().createQuery("from "+c.getName()+" where "+s+" = '"+params+"'");
        System.out.println(this.c.getName()+"DAO"+"执行 "+s+"= "+params+" 查询 ");
        return (List<T>)query.list();
    }
     
    @Override   //查询所有对象
    public List<T> findAll() {
        Query query=this.getSession().createQuery("from "+c.getName());
        System.out.println(this.c.getName()+"DAO"+"执行  findAll");
        return (List<T>)query.list();
    }
     
    @Override   //查询分页
    public List<T> findByPage(int start,int count){  
        Criteria criteria = this.getSession().createCriteria(c);  
        criteria.setFirstResult(start);  
        criteria.setMaxResults(count);  
        return criteria.list();  
    }  
 
    @Override   //更新数据
    public boolean update(T entity) {
        boolean rs= false;
        this.getSession().update(entity);
        Transaction tx=this.getSession().beginTransaction();
        try{
            this.getSession().update(entity);
            tx.commit();
            rs=true;
            System.out.println(this.c.getName()+"DAO"+"执行update 成功!");
        }catch(Exception e){
            System.out.println(this.c.getName()+"DAO"+"执行update 失败!!!!!!!!!!");
            tx.rollback();
        }
        return rs;
    }
 
}

求大神指点,卡在这两天了。。。。

展开
收起
爵霸 2016-03-04 14:23:39 4470 0
1 条回答
写回答
取消 提交回答
  • 把 bseDao 的

    this.sessionFactory.OpenSession();

    改为
    this.sessionFactory.getCurrentSession();

    2019-07-17 18:52:15
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载