下表,如何查询time的最大值? hql如何写?
create table _MaterialLoad(
id int IDENTITY(1,1) primary key not null,
mateCode char(12) not null,
contId char(30) not null,
time bigint not null
)
这句HQL返回的直接就是一个long,应该这样:
[code="java"]
public Object doInHibernate(Session session) {
Query query = session.createQuery(sql);
Long maxTime = (Long) query.uniqueResult();
System.out.println("dao matecode:"+maxTime);
return maxTime;
}
[/code]
如果你想返回time最大的那条完整记录,那HQL就不是这么写了,可以有几种写法:
1.子查询:(记得hql好像支持子查询的,不行的话考虑第二种)
[code="java"]hql="select o from MaterialLoad o where o.time=(select max(o.time) from MaterialLoad o)";[/code]
2.用排序:(接收仍用query.uniqueResult();)
[code="java"]hql="select o from MaterialLoad o order by o.time desc[/code]
3.子查询+表连接:(同1)
[code="java"]hql="select o from MaterialLoad o inner join (select max(o.time) as mixTime from MaterialLoad) m on o.time=m.mixTime [/code]
用max函数
假设table_MaterialLoad映射的类为MaterialLoad:
[code="java"]String hql = "select max(o.time) from MaterialLoad o";[/code]
[quote]mjsjx 写道
假设table_MaterialLoad映射的类为MaterialLoad:
Java代码 收藏代码
String hql = "select max(o.time) from MaterialLoad o";
饿是这句:Hibernate: select max(materiallo0_.time) as col_0_0_ from MaterialLoad materiallo0 [/quote]
最好贴一下你的代码(就是执行hibernate query的那一段)。