分了100张表,tb_00~tb_99,
使用HibernateTemplate,用远程sql查询,每次传入表名
代码如下:
SlUserPageInfo pageInfos = hibernateTemplateR.execute(new HibernateCallback<SlUserPageInfo>() {
@Override
public SlUserPageInfo doInHibernate(Session session) throws HibernateException {
SlUserPageInfo pageInfo = new SlUserPageInfo();
String tableName = "tb_";
if (tableNumber < 10) {
tableName += "0";
}
tableName += tableNumber;
System.out.println(tableName);
String andEndTime = "";
if (!StringUtils.isEmpty(endTimeStamp)) {
andEndTime = " and TimeStamp<=:endTimeStamp";
}
totalPage = (int) Math.ceil(((double) count) / pageSize);
pageInfo.setTotalPage((long) totalPage);
String sql = "select * from " + tableName + " where TimeStamp>:startTimeStamp"
+ andEndTime + " order by TimeStamp desc offset " + ((pageIndex - 1) * 10)
+ " rows fetch next " + pageSize + " rows only";
SQLQuery sqlQuery = session.createSQLQuery(sql).addEntity(User.class);
sqlQuery.setBinary("startTimeStamp", startBytes);
if (!StringUtils.isEmpty(endTimeStamp)) {
sqlQuery.setBinary("endTimeStamp", endBytes);
}
@SuppressWarnings("unchecked")
List<User> listQuery = (List<User>) sqlQuery.list();
if (listQuery != null && listQuery.size() > 0) {
pageInfo.setUsers(listQuery);
}
}
return pageInfo;
}
});
return pageInfos;
我拼写的sql语句是:
select * from tb_00 where TimeStamp>:startTimeStamp order by TimeStamp desc offset 0 rows fetch next 1 rows only
但是发送的却是:
Hibernate:
select
*
from
tb_01
where
TimeStamp>?
order by
TimeStamp desc offset 0 rows fetch next 1 rows only
传入sql语句查询tb_00表,但是发送sql语句却是查询tb_01表。
之前查过tb_01表,是不是sql语句被缓存了?怎么清除sql语句缓存?
晚上来看看!
知道什么回事吗?
@moyuguixiong:hibernateTemplateR.execute这个是在 tableNumber循环下做的吗?hibernate的缓存一般都是指数据在一条sql查询得到了,第二次查询时,不会再发sql。但是不会发错sql啊。。能给多点代码吗?我帮你调试一下!
@David5201:
这个是我调试的时候打印的sql语句,就是我通过变量生成表名,然后拼接的sql语句:
select * from tb_00 where TimeStamp>:startTimeStamp order by TimeStamp desc offset 0 rows fetch next 1 rows only
查看SQLQuery 变量,里面存的也是上述sql语句,但是调用SQLQuery 的list方法进行查询后,发送的sql语句,却是下面的,表名变了:
select
*
from
tb_01
where
TimeStamp>?
order by
TimeStamp desc offset 0 rows fetch next 1 rows only
@David5201:
而且不是每次都这样,而是当你正常调用方法查了tb_01后,再去查tb_00,sql偶尔会查tb_01,过一段时间,又正常的去查tb_00
@moyuguixiong:不是每次都这样? 这个调用不会在多线程中吧?你的tableNumber是不是共享变量,出现了多线程的竟态?