java 未使用休眠标准投影别名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2867994/
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
Hibernate criterion Projection alias not being used
提问by sbzoom
Do Hibernate Projection aliases even work? I could swear it just doesn't. At least, it doesn't do what I would expect it to do.
Hibernate Projection 别名是否有效?我可以发誓它只是没有。至少,它没有做我期望它做的事情。
Here is the java:
这是java:
return sessionFactory.getCurrentSession()
.createCriteria(PersonProgramActivity.class)
.setProjection(Projections.projectionList()
.add(Projections.alias(Projections.sum("numberOfPoints"), "number_of_points"))
.add(Projections.groupProperty("person.id")))
.setFirstResult(start)
.setFetchSize(size)
.addOrder(Order.desc("numberOfPoints"))
.list();
Here is the SQL that it generates:
这是它生成的 SQL:
select
sum(this_.number_of_points) as y0_,
this_.person_id as y1_
from
PERSON_PROGRAM_ACTIVITY this_
group by
this_.person_id
order by
this_.number_of_points desc
It doesn't seem to use the alias at all. I would think setting the alias would mean that sum(this_.number_of_points)would be aliased as number_of_pointsand not y0_. Is there some trick I am missing?
它似乎根本没有使用别名。我认为设置别名意味着sum(this_.number_of_points)将别名为 asnumber_of_points而不是y0_。有什么我想念的技巧吗?
Thanks.
谢谢。
采纳答案by sbzoom
You need to give the entire criteria an alias, then you can create other aliases that actually get used. What is strange is that the aliases get turned into y0_instead of the other way around.
您需要为整个条件指定一个别名,然后您可以创建其他实际使用的别名。奇怪的是别名变成了y0_而不是相反。
return sessionFactory.getCurrentSession()
.createCriteria(PersonProgramActivity.class, "ppa")
.setProjection(Projections.projectionList()
.add(Projections.alias(Projections.sum("numberOfPoints"), "ppa.numberOfPoints"))
.add(Projections.groupProperty("person.id")))
.setFirstResult(start)
.setFetchSize(size)
.addOrder(Order.desc("ppa.numberOfPoints"))
.list();
Generates the following SQL:
生成以下 SQL:
select
sum(this_.number_of_points) as y0_,
this_.person_id as y1_
from
PERSON_PROGRAM_ACTIVITY this_
group by
this_.person_id
order by
this_.y0_ desc
回答by webjockey
The query should be
查询应该是
return sessionFactory.getCurrentSession()
.createCriteria( PersonProgramActivity.class, "ppa" )
.setProjection(Projections.projectionList()
.add( Projections.alias( Projections.sum( **"ppa.numberOfPoints"** ), **"numberOfPoints"** ) )
.add( Projections.groupProperty( "person.id" ) ) )
.setFirstResult( start )
.setFetchSize( size )
.addOrder( Order.desc( "ppa.numberOfPoints" ) )
.list();

