我有一个实体组,该实体组要使用@Formula进行计算。
还有其他三个具有该实体外键的实体,而我要计算的是有多少实体依赖于每个Group实体,因此公式如下所示:
public class Group {
...
@Formula("(select count (distinct (s.id)) + count(distinct(ses.id)) + count (distinct(u.id))
from section s, session ses, user u, group_user gu
where s.group_id = id and ses.group_id = id and gu.user_id = u.id and
gu.group_id = id )")
private int dependencies;
}
但是当我在Group上执行findAll时,它会抛出异常:
ORA-00904: "GROUP0_"."COUNT": invalid identifier
生成的SQL看起来很奇怪,像这样:
Hibernate: select * from ( select distinct group0_.id as id1_8_, group0_.active as active2_8_, group0_.code as code3_8_,
...
(select group0_.count (distinct (s.id)) + count(distinct(ses.id)) + group0_.count (distinct(u.id)) from section s, session ses, user u, group_user gu where s.group_id = group0_.id and ses.group_id = group0_.id and gu.user_id = u.id and gu.group_id = group0_.id ) as formula1_
from group group0_ where 1=1 order by group0_.code asc ) where rownum <= ?
如何编写公式以使其起作用?
答案 0 :(得分:1)
最后,这只是一个愚蠢的问题:count和(之间的差距,如果我写公式:
@Formula("(select count(distinct (s.id)) + count(distinct(ses.id)) + count(distinct(u.id))
from section s, session ses, user u, group_user gu
where s.group_id = id and ses.group_id = id and gu.user_id = u.id and
gu.group_id = id )")
有效!