hibernate查询某一个对象后,执行createSQLQuery查询出现的问题
2014-10-31 10:20
791 查看
最近工作中碰到了一个奇怪问题,特别做下笔记:
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方法的时候,也是查询不到修改的对象的,从打印语句可以看出来:
先查询了,然后再更新了哦,所以会出现问题的
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方法的时候,也是查询不到修改的对象的,从打印语句可以看出来:
先查询了,然后再更新了哦,所以会出现问题的
相关文章推荐
- hibernate查询某一个对象后,执行createSQLQuery查询出现的问题
- hibernate查询某一个对象后,执行createSQLQuery查询出现的问题
- "执行SQL语句时出现问题操作必须使用一个可更新的查询"错误的解决方法
- Hibernate的createSQLQuery查询的问题
- Hibernate 使用createSQLQuery查询时,缓存问题
- hibernate的createSQLQuery与createQuery的返回数组与对象问题
- Hibernate createSQLQuery 查询Oracle char类型结果为一个字符解决方法汇总
- 使用Hibernate 5.0、4.0、3.0 createSQLQuery执行原生Sql语句 遇到问题及解决办法集锦
- 执行SQL语句时出现问题操作必须使用一个可更新的查询错误的解决方法
- 关于hibernate执行createSQLQuery时字段重名的问题
- 使用hibernate的this.getSession().createSQLQuery(sql).list();方法查询数据时出现查到的数据和想象的不一致,很是郁闷,诡异
- Hibernate的createSQLQuery查询的问题
- Hibernate SQL查询时有重名字段出现的问题
- Hibernate SQLQuery 查询Oracle char类型结果为一个字符的解决方法
- 使用hibernate3的createSQLQuery遇到的问题
- 当WHERE子句、GROUP BY子句和HAVING子句同时出现在一个查询中时,SQL的执行顺序
- Hibernate中关于多表连接查询hql 和 sql 返回值集合中对象问题
- Spring Hibernate sqlquery 查询结果 缓存问题
- Hibernate的createSQLQuery 查询数据库类型为char的字段时要注意
- 对原生SQL查询执行的控制是通过SQLQuery接口进行的,通过执行Session.createSQLQuery()