hibernate查询某一个对象后,执行createSQLQuery查询出现的问题

最近工作中碰到了一个奇怪问题,特别做下笔记:

user表里的数据,是1,1和2,2,分别为id和name字段。

代码如下:

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




public class Test {
private static final SessionFactory sessionFactory;
   static {
       try {
           // Create the SessionFactory from hibernate.cfg.xml
           sessionFactory = new Configuration().configure().buildSessionFactory();
       } catch (Throwable ex) {
           // Make sure you log the exception, as it might be swallowed
           System.err.println("Initial SessionFactory creation failed." + ex);
           throw new ExceptionInInitializerError(ex);
       }
   }
   public static SessionFactory getSessionFactory() {
       return sessionFactory;
   }
   public static void main(String[] args) {
Session session = sessionFactory.openSession();
Transaction tr = session.beginTransaction();
tr.begin();
User user = (User) session.load(User.class, 1);
user.setName("2");
session.saveOrUpdate(user);
int num = (Integer) session.createSQLQuery("select count(*) num from user where name = 2 ").addScalar("num", Hibernate.INTEGER).uniqueResult();
System.out.println(num);
tr.commit();
}

打印结果:

Hibernate: select user0_.id as id0_0_, user0_.name as name0_0_ from user user0_ where user0_.id=?
Hibernate: select count(*) num from user where name = 2 
1
Hibernate: update user set name=? where id=?

也就是说通过session.createSQLQuery这个方法并没有先去更新user对象,所以结果还是1,而我想要的结果是2,所以会出现这样错误

修改代码 :





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




public class Test {
private static final SessionFactory sessionFactory;
   static {
       try {
           // Create the SessionFactory from hibernate.cfg.xml
           sessionFactory = new Configuration().configure().buildSessionFactory();
       } catch (Throwable ex) {
           // Make sure you log the exception, as it might be swallowed
           System.err.println("Initial SessionFactory creation failed." + ex);
           throw new ExceptionInInitializerError(ex);
       }
   }
   public static SessionFactory getSessionFactory() {
       return sessionFactory;
   }
   public static void main(String[] args) {
Session session = sessionFactory.openSession();
Transaction tr = session.beginTransaction();
tr.begin();
User user = (User) session.load(User.class, 1);
user.setName("2");
session.saveOrUpdate(user);
int num = (Integer) session.createQuery("from User where name = 2 ").list().size();
System.out.println(num);
tr.commit();
}
}

执行结果:

Hibernate: select user0_.id as id0_0_, user0_.name as name0_0_ from user user0_ where user0_.id=?
Hibernate: update user set name=? where id=?
Hibernate: select user0_.id as id0_, user0_.name as name0_ from user user0_ where user0_.name=2
2

这里就是先更新了user对象后,再来查询了,所以这种情况是正确的

总结:如果跟新了查出来的对象,saveorupdate了,然后用session.createSQLQuery方法的时候,也是查询不到修改的对象的,从打印语句可以看出来:

先查询了,然后再更新了哦,所以会出现问题的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值