Hibernate 中 update 不执行,不报错

hai5987158 2014-05-08 10:00:53
最近调试的时候,发现 之前写好的 项目出了问题。

就是在执行 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;
}

}



求大神指点,卡在这两天了。。。。
...全文
2535 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxwgame12 2017-01-12
  • 打赏
  • 举报
回复
这个问题是导入HibernateDaoSupport的时候,注意引入版本的问题,看看你的jar包用的是什么版本就引用什么版本,假如引用错误,就会导致不报错,但是不执行任何HQL操作,切记,3天的血泪史
hwjv5 2015-12-18
  • 打赏
  • 举报
回复
引用 15 楼 jieksd 的回复:
1、DAO中的 使用getCurrentSession()获取session对象,不要使用openSession(),spring事务管理的要求 2、Spring MVC+Hibernate注解事务问题 将事务注解 <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> 放入到spring mvc servlet配置文件中,这时候服务启动时,事务也可以同时被加载进去。 参考文献:http://fengzhiyin.iteye.com/blog/714686
遇到同样的问题 这个方法解决了我的问题,谢谢
奕辰杰 2015-10-27
  • 打赏
  • 举报
回复
猿人林克 2015-10-26
  • 打赏
  • 举报
回复
把hibernate配置贴出来,调试时候hibernate的运行日志打出来,info和debug级别的都贴出来看看,看到信息以后这个问题很好锁定。
jieksd 2015-10-25
  • 打赏
  • 举报
回复
1、DAO中的 使用getCurrentSession()获取session对象,不要使用openSession(),spring事务管理的要求 2、Spring MVC+Hibernate注解事务问题 将事务注解 <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> 放入到spring mvc servlet配置文件中,这时候服务启动时,事务也可以同时被加载进去。 参考文献:http://fengzhiyin.iteye.com/blog/714686
  • 打赏
  • 举报
回复
我也遇到了同样的情况:不执行,不报错。估计只是大家出现这种情况的其中之一。 是因为为ssh整合时,在spring中声明事务,没有配置update方法,添加上就好了。我猜是hibernate更新时没有事务不执行,只是猜测。 <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method name="save*" /> <tx:method name="add*" /> <tx:method name="delete*" /> <tx:method name="update*" /> </tx:attributes> </tx:advice>
wuys 2015-01-21
  • 打赏
  • 举报
回复
这个问题解决了 吗,是什么原因呢,我也出现这个问题了
hai5987158 2014-05-08
  • 打赏
  • 举报
回复
引用 7 楼 suciver 的回复:
[quote=引用 6 楼 hai5987158 的回复:] [quote=引用 4 楼 xiaming08240316 的回复:] update 表名 set 字段 where 条件
这个我知道 我那是测试代码。 测试发现那 两句代码 放在 update 方法中 代替 “ this.getSession().update(entity);” 可以执行 。。。。。。。。。我的目标是 要 执行 “ this.getSession().update(entity);” 的时候,Hibernate 自动生成SQL语句来 修改数据。 [/quote] 在this.getSession().update(entity);这句前面少了tx.begin();[/quote] 不行呢。。还有 我去除所有事务管理的 语句, 也还是不行~~
suciver 2014-05-08
  • 打赏
  • 举报
回复
引用 6 楼 hai5987158 的回复:
[quote=引用 4 楼 xiaming08240316 的回复:] update 表名 set 字段 where 条件
这个我知道 我那是测试代码。 测试发现那 两句代码 放在 update 方法中 代替 “ this.getSession().update(entity);” 可以执行 。。。。。。。。。我的目标是 要 执行 “ this.getSession().update(entity);” 的时候,Hibernate 自动生成SQL语句来 修改数据。 [/quote] 在this.getSession().update(entity);这句前面少了tx.begin();
hai5987158 2014-05-08
  • 打赏
  • 举报
回复
引用 4 楼 xiaming08240316 的回复:
update 表名 set 字段 where 条件
这个我知道 我那是测试代码。 测试发现那 两句代码 放在 update 方法中 代替 “ this.getSession().update(entity);” 可以执行 。。。。。。。。。我的目标是 要 执行 “ this.getSession().update(entity);” 的时候,Hibernate 自动生成SQL语句来 修改数据。
hai5987158 2014-05-08
  • 打赏
  • 举报
回复
引用 3 楼 suciver 的回复:
楼主你这是sql语法错误,update的from去掉
额,我那是测试代码。 测试发现那 两句代码 放在 update 方法中 代替 “ this.getSession().update(entity);” 可以执行 。。。。。。。。。我的目标是 要 执行 “ this.getSession().update(entity);” 的时候,Hibernate 自动生成SQL语句来 修改数据。
木炎2019 2014-05-08
  • 打赏
  • 举报
回复
update 表名 set 字段 where 条件
suciver 2014-05-08
  • 打赏
  • 举报
回复
楼主你这是sql语法错误,update的from去掉
hai5987158 2014-05-08
  • 打赏
  • 举报
回复
引用 1 楼 sunbo624 的回复:
update哪有from
意思是,当我传个 对象 entity 进 update 的时候, 执行 “ this.getSession().update(entity);” 这行代码 它没有报错,但是也没有任何反应。 如果正常情况不是会 直接更新对象。? Hibernate 自动生成 sql语句执行?
sunbo624 2014-05-08
  • 打赏
  • 举报
回复
update哪有from
hai5987158 2014-05-08
  • 打赏
  • 举报
回复
引用 7 楼 suciver 的回复:
[quote=引用 6 楼 hai5987158 的回复:] [quote=引用 4 楼 xiaming08240316 的回复:] update 表名 set 字段 where 条件
这个我知道 我那是测试代码。 测试发现那 两句代码 放在 update 方法中 代替 “ this.getSession().update(entity);” 可以执行 。。。。。。。。。我的目标是 要 执行 “ this.getSession().update(entity);” 的时候,Hibernate 自动生成SQL语句来 修改数据。 [/quote] 在this.getSession().update(entity);这句前面少了tx.begin();[/quote]额,是我觉得配置文件有问题。 麻烦你看一下,谢了。 http://git.oschina.net/hais/Test_Server 就是执行 com.soryin.controller.UserController.java 里面的 update 方法的时候,不执行。
hai5987158 2014-05-08
  • 打赏
  • 举报
回复
引用 9 楼 u014655465 的回复:
entity数据没改变试着改变entity的属性值然后在执行update操作。sql语句就会有了。记得showsql设置显示啊。
额,是我觉得配置文件有问题。 麻烦你看一下,谢了。 http://git.oschina.net/hais/Test_Server 就是执行 com.soryin.controller.UserController.java 里面的 update 方法的时候,不执行。
hai5987158 2014-05-08
  • 打赏
  • 举报
回复
引用 9 楼 u014655465 的回复:
entity数据没改变试着改变entity的属性值然后在执行update操作。sql语句就会有了。记得showsql设置显示啊。
把项目拆开,突然发现 自己搭建的 SpringMVC+Hibernate+Spring 框架 有问题。 所有 update、delete操作都执行不了。。。突然好迷茫~~~
华州一霸 2014-05-08
  • 打赏
  • 举报
回复
entity数据没改变试着改变entity的属性值然后在执行update操作。sql语句就会有了。记得showsql设置显示啊。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧