Java 使用 Hibernate Criteria 获取最大 id 的记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3900105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get record with max id, using Hibernate Criteria
提问by Amr Faisal
Using Hibernate's Criteria API, I want to select the record within a table with the maximum value for a given column.
使用Hibernate的 Criteria API,我想选择表中给定列的最大值的记录。
I tried to use Projections, creating an aliasfor max(colunName)
, then using it in restrictions.eq()
, but it keeps telling me "invalid number".
我试图用投影,创造一个别名为max(colunName)
,然后使用它restrictions.eq()
,但它不断告诉我“无效号码”。
What's the correct way to do that with Hibernate?
使用 Hibernate 执行此操作的正确方法是什么?
采纳答案by Pascal Thivent
You can use a DetachedCriteria
to express a subquery, something like this:
您可以使用 aDetachedCriteria
来表达子查询,如下所示:
DetachedCriteria maxId = DetachedCriteria.forClass(Foo.class)
.setProjection( Projections.max("id") );
session.createCriteria(Foo.class)
.add( Property.forName("id").eq(maxId) )
.list();
References
参考
- Hibernate Core Reference Guide
- Hibernate 核心参考指南
回答by meriton
HQL:
高品质:
from Person where person.id = (select max(id) from Person)
Untested. Your database needs to understand subselects in the where clause.
未经测试。您的数据库需要理解 where 子句中的子选择。
Too lazy to find out if/how such a subselect can be expressed with the criteria api. Of course, you could do two queries: First fetch the max id, then the entity with that id.
懒得找出是否/如何用标准 api 表达这样的子选择。当然,您可以执行两个查询:首先获取最大 id,然后获取具有该 id 的实体。
回答by JEEVA
For the max()
function in hibernate:
对于max()
休眠中的功能:
criteria.setProjection(Projections.max("e.encounterId"));
回答by WhyNotHugo
Use
用
addOrder(Order.desc("id"))
and fetch just the first result :)
并只获取第一个结果:)
回答by amacleod
I found that using addOrder
and setMaxResults
together worked for me.
我发现使用addOrder
和setMaxResults
一起工作对我有用。
Criteria c = session.createCriteria(Thingy.class);
c.addOrder(Order.desc("id"));
c.setMaxResults(1);
return (Thingy)c.uniqueResult();
Using the MySQL dialect, this generates a SQL prepared statement about like this (snipping out some of the fields):
使用 MySQL 方言,这会生成一条 SQL 准备好的语句,如下所示(剪掉一些字段):
select this_.id ... from Thingy this_ order by this_.id desc limit ?
I am not sure if this solution would be effective for dialects other than MySQL.
我不确定这个解决方案是否对 MySQL 以外的方言有效。
回答by code chimp
The cleaner solution would also be :
更清洁的解决方案也将是:
DetachedCriteria criteria = DetachedCriteria.forClass(Foo.class).setProjection(Projections.max("id"));
Foo fooObj =(Foo) criteria.getExecutableCriteria(getCurrentSession()).list().get(0);
回答by Kumar Abhishek
Date maxDateFromDB = null;
Session session = (Session) entityManager.getDelegate();
//Register is and Entity and assume maxDateFromDB is a column.
//Status is another entity with Enum Applied.
//Code is the Parameter for One to One Relation between Register and Profile entity.
Criteria criteria = session.createCriteria(Register.class).setProjection(Projections.max("maxDateFromDB") )
.add(Restrictions.eq("status.id", Status.Name.APPLIED.instance().getId()));
if(code != null && code > 0) {
criteria.add(Restrictions.eq("profile.id", code));
}
List<Date> list = criteria.list();
if(!CollectionUtils.isEmpty(list)){
maxDateFromDB = list.get(0);
}
回答by Daniel Patrick
To do it entirely with Detached Criteria (because I like to construct the detached criteria without a session)
完全使用分离标准来完成(因为我喜欢在没有会话的情况下构建分离标准)
DetachedCriteria maxQuery = DetachedCriteria.forClass(Foo.class)
.setProjection( Projections.max("id") );
DetachedCriteria recordQuery = DetachedCriteria.forClass(Foo.class)
.add(Property.forName("id").eq(maxId) );