所以今天我遇到了一个问题,即@Formula无法正常工作。让我们说:
数据库
year | X | Y
-----+-----+-----
2010 | A | 10
2011 | A | 20
2011 | B | 99
2009 | C | 30
2010 | C | 40
我想获得最高的“ Y”年份并按“ X”分组,所以我编写了这样的查询
SELECT year
FROM (SELECT
year,
MAX(y) OVER (PARTITION BY x ) max_y
FROM TableB) q
WHERE year = '2011' and x = 'A';
所以结果是
year |
-----+
2011 |
然后我想在实体中使用它,所以我用@Formula创建实体
@Formula("(SELECT year FROM (SELECT b.year, MAX(b.y) OVER(PARTITION BY b.x) max_y FROM TableB b) q WHERE year = '2011' AND x = 'A')")
private String yearWithHighestScore;
但是Hibernate会处理:
(select entity0_.year as year1_8_
from (select b.year as name, max(b.y) over(partition by b.year) entity0_.max_y from TableB b) entity0_.q
where entity0_.year=? and entity0_.x=?) as formula0_1_
您看到Hibernate将其转换为“ entity0_.max_y”,这是不正确的,因为max_y不是表TableB的列;它只是计算列的别名
我的问题:我们可以通过任何方式告诉Hibernate这是列的别名,而不是列名本身吗?
谢谢
答案 0 :(得分:0)
以'_'开头的别名别名:
@Formula("(SELECT year FROM (SELECT b.year, MAX(b.y) OVER(PARTITION BY b.x) _max_y FROM TableB b) _q WHERE year = '2011' AND x = 'A')")