二哈大叔 2017-02-18 13:59 采纳率: 0%
浏览 2144

SSH框架的getSession().update(entity)不能实现保存

我用的是通用Dao,但是保存方法可以执行,可以保存到数据库,但是更新方法虽然执行了,但是数据却没有更新,这是怎么回事啊,控制台也已经输出了更新语句,就是数据库的数据不更新,代码见下图,求教啊

  • 写回答

8条回答 默认 最新

  • 二哈大叔 2017-02-18 14:00
    关注

    这是我dao层实现类的代码
    package com.ffour.yixing.dao.impl;

    import java.io.Serializable;
    import java.lang.reflect.ParameterizedType;
    import java.util.List;

    import javax.annotation.Resource;

    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;

    import com.ffour.yixing.dao.interfaces.BaseDao;

    @SuppressWarnings("unchecked")

    public class BaseDaoImpl implements BaseDao{

    private Class<T> clazz;
    
    /** 
     * 通过构造方法指定DAO的具体实现类 
     */  
    public BaseDaoImpl() {
        ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();  
        clazz = (Class<T>) type.getActualTypeArguments()[0];  
        System.out.println("DAO的真实实现类是:" + this.clazz.getName());  
    }
    
    /** 
     * 向DAO层注入SessionFactory 
     */  
    @Resource  
    private SessionFactory sessionFactory; 
    
    /** 
     * 获取当前工作的Session 
     */  
    protected Session getSession() {  
        return this.sessionFactory.getCurrentSession();  
    }
    
    @Override
    public void save(T entity) {
        // TODO Auto-generated method stub
        this.getSession().save(entity);
        this.getSession().flush();
    }
    
    @Override
    public void update(T entity) {
        // TODO Auto-generated method stub
         this.getSession().update(entity);
         System.out.println("111");
         this.getSession().flush();
    }
    
    @Override
    public void delete(Serializable id) {
        // TODO Auto-generated method stub
         this.getSession().delete(this.findById(id));
         this.getSession().flush();
    }
    
    @Override
    public T findById(Serializable id) {
        // TODO Auto-generated method stub
         return (T) this.getSession().get(this.clazz, id);
    }
    
    @Override
    public List<T> findByHQL(String hql, Object... params) {
        // TODO Auto-generated method stub
        Query query = this.getSession().createQuery(hql);  
        for (int i = 0; params != null && i < params.length; i++) {  
            query.setParameter(i, params[i]);
        }
        return query.list();
    }
    

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画